#!/usr/bin/env python3 """CPU-only, fixed-wheel replay. python3 replay.txt --output-dir /tmp/NEW_DIR No ledger edits. CPython 3.14 GIL / macOS arm64. Temporary inputs, wheels and pip-free venv are outside the workspace and removed even on failure. """ import argparse from datetime import datetime, timezone import gc import hashlib import importlib.metadata import json import os from pathlib import Path import platform import shutil import statistics import subprocess import sys import tempfile import time import venv import zipfile PREFIX = 'pyyaml-ruamel-yaml-local-2026-11-19-req065-a1' TOOLS = ('pyyaml', 'ruamel') WHEELS = [ {'name':'PyYAML','version':'6.0.3','filename':'pyyaml-6.0.3-cp314-cp314-macosx_11_0_arm64.whl','url':'https://files.pythonhosted.org/packages/bd/9c/4d95bb87eb2063d20db7b60faa3840c1b18025517ae857371c4dd55a6b3a/pyyaml-6.0.3-cp314-cp314-macosx_11_0_arm64.whl','sha256':'34d5fcd24b8445fadc33f9cf348c1047101756fd760b4dacb5c3e99755703310','bytes':173809}, {'name':'ruamel.yaml','version':'0.19.1','filename':'ruamel_yaml-0.19.1-py3-none-any.whl','url':'https://files.pythonhosted.org/packages/b8/0c/51f6841f1d84f404f92463fc2b1ba0da357ca1e3db6b7fbda26956c3b82a/ruamel_yaml-0.19.1-py3-none-any.whl','sha256':'27592957fedf6e0b62f281e96effd28043345e0e66001f97683aa9a40c667c93','bytes':118102} ] def utc(): return datetime.now(timezone.utc).isoformat() def sha(data): return hashlib.sha256(data).hexdigest() def save(path, obj): with Path(path).open('x', encoding='utf-8') as f: json.dump(obj, f, ensure_ascii=False, allow_nan=False, indent=2) f.write('\n') def corpus(n, style): # Only list, str and int; no mappings, bool, null, float, dates or tags. strings = ['日本語とemoji🌸', 'yes', 'on', '2026-11-19', '0012', 'quote" slash\\ newline\n tab\t', '', 'café'] expected = [[strings[i % len(strings)], i-5, ['alpha', strings[(i+1) % len(strings)]], [-i, 0, i+1]] for i in range(n)] q = lambda s: json.dumps(s, ensure_ascii=False) if style == 'flow': text = json.dumps(expected, ensure_ascii=False, separators=(',', ':')) + '\n' else: lines = [] for s, i, words, ints in expected: lines.extend(['-', ' - ' + q(s), ' - ' + str(i), ' -', *[' - ' + q(w) for w in words], ' -', *[' - ' + str(v) for v in ints]]) text = '\n'.join(lines) + '\n' return text, expected def typed_equal(actual, expected): if type(actual) is not type(expected): return False if type(expected) is list: return len(actual) == len(expected) and all(typed_equal(a, b) for a, b in zip(actual, expected)) if type(expected) not in (str, int): raise TypeError('unexpected fixture type') return actual == expected def batch(fn, text, loops): gc.collect() enabled = gc.isenabled() gc.disable() try: start = time.perf_counter_ns() for _ in range(loops): result = fn(text) del result elapsed = time.perf_counter_ns() - start finally: if enabled: gc.enable() return elapsed def worker(out): import yaml import ruamel.yaml from ruamel.yaml import YAML assert importlib.metadata.version('PyYAML') == '6.0.3' assert importlib.metadata.version('ruamel.yaml') == '0.19.1' parser = YAML(typ='safe', pure=True) assert parser.pure is True assert parser.Parser.__module__.startswith('ruamel.yaml.parser') assert yaml.SafeLoader.__module__ == 'yaml.loader' # PyYAML safe_load explicitly constructs SafeLoader (never CSafeLoader). loaders = {'pyyaml': yaml.safe_load, 'ruamel': parser.load} clock = time.get_clock_info('perf_counter') raw = { 'request_id':'req-20261022-065', 'topic_id':'pyyaml-ruamel-yaml-local-hikaku', 'started_utc':utc(), 'environment':{'os':platform.platform(), 'machine':platform.machine(), 'python':sys.version, 'logical_cpu_count':os.cpu_count(), 'device':'CPU only', 'metal_initialized':False, 'loadavg_before':list(os.getloadavg()), 'versions':{'PyYAML':yaml.__version__, 'ruamel.yaml':ruamel.yaml.__version__}, 'pyyaml_loader':'yaml.SafeLoader', 'pyyaml_loader_module':yaml.SafeLoader.__module__, 'pyyaml_with_libyaml_installed':bool(yaml.__with_libyaml__), 'ruamel_parser':parser.Parser.__module__+'.'+parser.Parser.__name__, 'ruamel_pure':parser.pure, 'clock':{'implementation':clock.implementation, 'resolution_seconds':clock.resolution, 'monotonic':clock.monotonic}}, 'conditions':{'repeats':7, 'warmup_batches_per_case_tool':1, 'input':'identical UTF-8-derived Python str for both APIs; RAM generated lists, quoted strings, decimal integers', 'apis':{'pyyaml':'yaml.safe_load(text)', 'ruamel':'YAML(typ="safe", pure=True).load(text), one instance reused'}, 'backends':'both pure Python; installed PyYAML LibYAML unused', 'order':'case start alternates; each repeat alternates order', 'gc':'collect before batch; cyclic GC disabled only during timing; refcounts active', 'timing_includes':'loop, API, allocation, parse, result release', 'timing_excludes':'download, venv, imports, parser instance construction, corpus generation, validation, hash, IO', 'cpu_only':'no GPU-capable framework imported or initialized'}, 'unit_map':{'trials[].elapsed_ns':'ns','trials[].loops':'回','series[].elapsed_ns[]':'ns','series[].ms_per_load[]':'ms','cases[].input_utf8_bytes':'bytes','cases[].validation_after[].expected_typed_match':'件','cases[].validation_after[].cross_typed_match':'件'}, 'cases':[], 'warmups':[], 'trials':[], 'series':[], 'summary':[]} for case_index, (style,n,loops) in enumerate((style,n,loops) for style in ('block','flow') for n,loops in ((10,10),(100,2),(1000,1))): case_id = style+str(n) text, expected = corpus(n, style) (out / (PREFIX+'-'+case_id+'-input.txt')).write_text(text, encoding='utf-8') save(out / (PREFIX+'-'+case_id+'-expected.json'), expected) case = {'id':case_id,'style':style,'records':n,'loops':loops,'input_utf8_bytes':len(text.encode('utf-8')),'input_sha256':sha(text.encode('utf-8')),'expected_canonical_sha256':sha(json.dumps(expected,ensure_ascii=False,separators=(',',':')).encode('utf-8'))} def validate(phase): outputs = {tool:loaders[tool](text) for tool in TOOLS} checks = [] for tool in TOOLS: assert typed_equal(outputs[tool],expected), (case_id,tool,phase) assert typed_equal(outputs[tool],outputs[TOOLS[1-TOOLS.index(tool)]]), (case_id,tool,phase,'cross') checks.append({'tool':tool,'expected_typed_match':1,'cross_typed_match':1}) if phase == 'after': save(out / (PREFIX+'-'+case_id+'-'+tool+'-output.json'),outputs[tool]) return checks case['validation_before'] = validate('before') order = TOOLS if case_index%2 == 0 else TOOLS[::-1] for tool in order: raw['warmups'].append({'id':case_id+'-'+tool+'-warmup','case':case_id,'tool':tool,'loops':loops,'elapsed_ns':batch(loaders[tool],text,loops)}) for repeat in range(7): for position,tool in enumerate(order if repeat%2 == 0 else order[::-1]): elapsed = batch(loaders[tool],text,loops) raw['trials'].append({'id':case_id+'-'+tool+'-r'+str(repeat+1),'case':case_id,'tool':tool,'repeat':repeat+1,'position':position+1,'loops':loops,'elapsed_ns':elapsed}) case['validation_after'] = validate('after') raw['cases'].append(case) for tool in TOOLS: times = [t['elapsed_ns'] for t in raw['trials'] if t['case']==case_id and t['tool']==tool] ms = [t/loops/1_000_000 for t in times] raw['series'].append({'id':case_id+'-'+tool,'elapsed_ns':times,'ms_per_load':ms}) raw['summary'].append({'case':case_id,'tool':tool,'median_batch_ns':statistics.median(times),'median_ms_per_load':statistics.median(ms),'min_ms_per_load':min(ms),'max_ms_per_load':max(ms)}) print(case_id+' passed values/types, timed both APIs',flush=True) raw['environment']['loadavg_after'] = list(os.getloadavg()) raw['ended_utc'] = utc() assert len(raw['trials']) == 84 save(out / (PREFIX+'-raw.json'),raw) print('SUCCESS: 6 cases, 84 measured batches, 12 warmup batches',flush=True) def main(): ap=argparse.ArgumentParser() ap.add_argument('--output-dir',required=True) ap.add_argument('--worker',action='store_true',help=argparse.SUPPRESS) args=ap.parse_args() out=Path(args.output_dir).resolve() if args.worker: worker(out) return if out.exists(): raise FileExistsError('output-dir must be new: '+str(out)) if sys.version_info[:2] != (3,14) or platform.machine() != 'arm64' or sys.platform != 'darwin' or sysconfig_gil_disabled(): raise RuntimeError('Requires regular GIL CPython 3.14 on macOS arm64') with tempfile.TemporaryDirectory(prefix='pyyaml-ruamel-cpu-',dir='/tmp') as tmp: temp=Path(tmp) venv_dir=temp/'venv' venv.EnvBuilder(with_pip=False).create(venv_dir) py=venv_dir/'bin/python' site=venv_dir/'lib/python3.14/site-packages' deps=[] for info in WHEELS: wheel=temp/info['filename'] subprocess.run(['curl','-q','--fail','--silent','--show-error','--location',info['url'],'--output',str(wheel)],check=True) data=wheel.read_bytes() assert len(data)==info['bytes'] and sha(data)==info['sha256'],info['filename'] with zipfile.ZipFile(wheel) as z: assert all(not Path(n).is_absolute() and '..' not in Path(n).parts for n in z.namelist()) z.extractall(site) deps.append({**info,'actual_sha256':sha(data),'actual_bytes':len(data),'retrieved_utc':utc(),'metadata_source':'https://pypi.org/pypi/'+info['name']+'/'+info['version']+'/json'}) stage=temp/'stage' stage.mkdir() env={k:os.environ[k] for k in ('PATH','TMPDIR','LANG','LC_ALL','SYSTEMROOT') if k in os.environ} env['PYTHONDONTWRITEBYTECODE']='1' result=subprocess.run([str(py),'-I','-B',str(Path(__file__).resolve()),'--worker','--output-dir',str(stage)],cwd=temp,env=env,text=True,capture_output=True) print(result.stdout,end='') if result.returncode: print(result.stderr,file=sys.stderr) raise RuntimeError('measurement worker failed; no persistent outputs created') save(stage/(PREFIX+'-dependencies.json'),deps) (stage/(PREFIX+'-run-log.txt')).write_text(result.stdout+result.stderr,encoding='utf-8') shutil.copyfile(Path(__file__).resolve(),stage/(PREFIX+'-replay.txt')) shutil.copytree(stage,out) def sysconfig_gil_disabled(): import sysconfig return bool(sysconfig.get_config_var('Py_GIL_DISABLED')) if __name__=='__main__': main()