#!/usr/bin/env python3 """Reproduce on regular GIL CPython 3.14 / macOS arm64 with CPU only. python3 replay.txt --output-dir /tmp/NEW_DIRECTORY Fixed public wheels are fetched and SHA256-verified, then extracted into a pip-free venv under TemporaryDirectory(dir='/tmp'). A -I worker runs this same TXT as Python. Inputs are generated in RAM; wheels, venv and staging outputs are deleted on exit. Only a new output directory is kept, after the worker succeeds. Never edits a ledger. No credentials, login, external inference, audio device, GPU, Metal or Bluetooth. """ import argparse from datetime import datetime, timezone import gc import hashlib import importlib.metadata import json import math 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 = 'ujson-simplejson-local-2026-11-18-req065-a1' REPEATS = 7 TOOLS = ('ujson', 'simplejson') WHEELS = [ {'name': 'ujson', 'version': '6.1.0', 'filename': 'ujson-6.1.0-cp314-cp314-macosx_11_0_arm64.whl', 'url': 'https://files.pythonhosted.org/packages/7a/53/fb59cbca5822c5d2fd8c841e2828fc5d84c22bb12e7c0e772b11b81e9a1c/ujson-6.1.0-cp314-cp314-macosx_11_0_arm64.whl', 'sha256': '9242c01ab5bb98b42d4afcee33410fe02d4a2aa29ace84886437988a70c27a67', 'bytes': 54298}, {'name': 'simplejson', 'version': '4.3.0', 'filename': 'simplejson-4.3.0-cp314-cp314-macosx_11_0_arm64.whl', 'url': 'https://files.pythonhosted.org/packages/bd/35/1b5aa5ddbbb28c47a68e2d247a6eac9b0118deef6530085c0b48eb6f4c5f/simplejson-4.3.0-cp314-cp314-macosx_11_0_arm64.whl', 'sha256': '8fe881edc5f7c2256af022d290c4a933ff52b1c163c49e0a976af37540ae2498', 'bytes': 102840}, ] def sha(data): return hashlib.sha256(data).hexdigest() def save_json(path, data): with Path(path).open('x', encoding='utf-8') as f: json.dump(data, f, ensure_ascii=False, allow_nan=False, indent=2) f.write('\n') def canonical(obj): return json.dumps(obj, ensure_ascii=False, allow_nan=False, separators=(',', ':')) def corpus(): """No random input, no user files; strings are original synthetic literals.""" for n, loops in ((10, 1500), (1000, 25), (10000, 3)): records = [ {'id': i, 'name': '架空のJSON資料😀 / record %d' % i, 'text': 'quote=" backslash=\\ newline=\n tab=\t & café', 'count': i * 3 - 7, 'fraction': (i % 257 - 128) / 8, 'enabled': i % 2 == 0, 'optional': None, 'nested': {'tags': ['試験', 'alpha', str(i % 9)], 'samples': [i, -i, 0.125, -0.0, True, False, None]}} for i in range(n) ] yield 'dict%d' % n, {'dataset': 'synthetic-local-json', 'records': records, 'metadata': {'count': n, 'empty_list': [], 'empty_dict': {}}}, loops, n for n, loops in ((10, 10000), (1000, 300), (100000, 3)): values = [((i % 257) - 128) if i % 2 == 0 else ((i % 257) - 128) / 8 for i in range(n)] yield 'array%d' % n, values, loops, n def same_typed_value(a, b): """Also distinguishes bool/int, int/float and the sign of zero.""" if type(a) is not type(b): return False if isinstance(a, dict): return a.keys() == b.keys() and all(same_typed_value(a[k], b[k]) for k in a) if isinstance(a, list): return len(a) == len(b) and all(same_typed_value(x, y) for x, y in zip(a, b)) if isinstance(a, float): return a == b and (a != 0 or math.copysign(1, a) == math.copysign(1, b)) return a == b def timed_batch(fn, loops): gc.collect() was_enabled = gc.isenabled() gc.disable() try: begin = time.perf_counter_ns() for _ in range(loops): result = fn() del result elapsed = time.perf_counter_ns() - begin finally: if was_enabled: gc.enable() assert elapsed > 0 return elapsed def worker(out): import ujson import simplejson import simplejson._speedups import simplejson.encoder import simplejson.decoder out.mkdir() start = datetime.now(timezone.utc).isoformat() modules = {'ujson': ujson, 'simplejson': simplejson} for name, module in modules.items(): assert Path(module.__file__).is_relative_to(Path(sys.prefix)) expected = next(w['version'] for w in WHEELS if w['name'] == name) assert importlib.metadata.version(name) == expected assert simplejson.encoder.c_make_encoder is not None assert simplejson.decoder.c_scanstring is not None environment = { 'platform': platform.platform(), 'machine': platform.machine(), 'python': sys.version, 'implementation': platform.python_implementation(), 'logical_cpus': os.cpu_count(), 'execution_device': 'cpu', 'gpu_backend': 'none (not imported or initialized)', 'threading': 'one Python process; sequential calls; regular GIL build', 'versions': {n: importlib.metadata.version(n) for n in TOOLS}, 'simplejson_c_encoder_available': simplejson.encoder.c_make_encoder is not None, 'simplejson_c_scanstring_available': simplejson.decoder.c_scanstring is not None, 'extension_files': {'ujson': Path(ujson.__file__).name, 'simplejson': Path(simplejson._speedups.__file__).name}, 'clock': vars(time.get_clock_info('perf_counter')), 'load_average_start': list(os.getloadavg()), } print('environment:', json.dumps(environment, ensure_ascii=False), flush=True) # Both emit compact Unicode str. ujson's slash escaping is explicitly disabled. dumps = { 'ujson': lambda x: ujson.dumps(x, ensure_ascii=False, separators=(',', ':'), escape_forward_slashes=False), 'simplejson': lambda x: simplejson.dumps(x, ensure_ascii=False, separators=(',', ':'), allow_nan=False), } loads = {'ujson': ujson.loads, 'simplejson': simplejson.loads} cases, trials, warmups, groups = [], [], [], [] condition_index = 0 for case_id, obj, loops, elements in corpus(): input_text = canonical(obj) encoded = {tool: dumps[tool](obj) for tool in TOOLS} def validate(): checks = [] for tool in TOOLS: decoded = loads[tool](input_text) output = dumps[tool](obj) cycled = loads[tool](output) independent = json.loads(output) record = { 'id': tool, 'decode_typed_match': int(same_typed_value(obj, decoded)), 'encode_stdlib_typed_match': int(same_typed_value(obj, independent)), 'roundtrip_typed_match': int(same_typed_value(obj, cycled)), 'output_is_str': int(isinstance(output, str)), 'encoded_utf8_bytes': len(output.encode('utf-8')), 'encoded_utf8_sha256': sha(output.encode('utf-8')), 'cross_load_typed_match': int(same_typed_value( obj, loads[TOOLS[1 - TOOLS.index(tool)]](output))), } for key in ('decode_typed_match', 'encode_stdlib_typed_match', 'roundtrip_typed_match', 'output_is_str', 'cross_load_typed_match'): assert record[key] == 1, (case_id, tool, key) checks.append(record) return checks before = validate() case = {'id': case_id, 'root_type': type(obj).__name__, 'elements': elements, 'loops': loops, 'input_utf8_bytes': len(input_text.encode('utf-8')), 'input_utf8_sha256': sha(input_text.encode('utf-8')), 'tools_encoded_bytes_equal': int(encoded['ujson'] == encoded['simplejson']), 'validation_before': before} if elements == 10: for tool in TOOLS: (out / (PREFIX + '-' + case_id + '-' + tool + '-output.json')).write_bytes( encoded[tool].encode('utf-8')) for operation in ('encode', 'decode', 'roundtrip'): functions = {} for tool in TOOLS: if operation == 'encode': functions[tool] = lambda t=tool: dumps[t](obj) elif operation == 'decode': functions[tool] = lambda t=tool: loads[t](input_text) else: functions[tool] = lambda t=tool: loads[t](dumps[t](obj)) base_order = TOOLS if condition_index % 2 == 0 else TOOLS[::-1] for tool in base_order: elapsed = timed_batch(functions[tool], loops) warmups.append({'id': case_id + '-' + operation + '-' + tool + '-warmup', 'case': case_id, 'operation': operation, 'tool': tool, 'loops': loops, 'elapsed_ns': elapsed}) per_tool = {t: [] for t in TOOLS} for rep in range(1, REPEATS + 1): order = base_order if rep % 2 else base_order[::-1] for position, tool in enumerate(order, 1): elapsed = timed_batch(functions[tool], loops) tid = '%s-%s-%s-r%d' % (case_id, operation, tool, rep) trials.append({'id': tid, 'case': case_id, 'operation': operation, 'tool': tool, 'repeat': rep, 'order_position': position, 'loops': loops, 'elapsed_ns': elapsed}) per_tool[tool].append(elapsed) for tool, ns in per_tool.items(): groups.append({'id': case_id + '-' + operation + '-' + tool, 'case': case_id, 'operation': operation, 'tool': tool, 'loops': loops, 'elapsed_ns': ns, 'median_batch_ns': statistics.median(ns), 'median_ms_per_call': statistics.median(ns) / loops / 1e6, 'min_batch_ns': min(ns), 'max_batch_ns': max(ns)}) condition_index += 1 case['validation_after'] = validate() assert case['validation_before'] == case['validation_after'] cases.append(case) print('validated and measured:', case_id, 'loops=', loops, 'UTF8 bytes=', case['input_utf8_bytes'], flush=True) assert len(trials) == 252 and len(warmups) == 36 assert not any(m.split('.')[0] in ('torch', 'mlx', 'tensorflow', 'cupy', 'jax') for m in sys.modules) environment['load_average_end'] = list(os.getloadavg()) raw = { 'request_id': 'req-20261022-065', 'topic_id': 'ujson-simplejson-local-hikaku', 'measured_date': datetime.now().astimezone().date().isoformat(), 'started_at_utc': start, 'finished_at_utc': datetime.now(timezone.utc).isoformat(), 'environment': environment, 'conditions': { 'repeats': REPEATS, 'warmup_batches_per_condition_per_tool': 1, 'decode_input': 'same compact Unicode str generated by stdlib json.dumps', 'encode_input': 'same generated Python dict/list object', 'roundtrip': 'loads(dumps(original object)); memory only', 'ujson_dumps_options': {'ensure_ascii': False, 'separators': [',', ':'], 'escape_forward_slashes': False}, 'simplejson_dumps_options': {'ensure_ascii': False, 'separators': [',', ':'], 'allow_nan': False}, 'loads_options': 'defaults; inputs contain finite values only', 'timing_includes': 'Python loop, wrappers, allocation, native calls, result release', 'timing_excludes': 'download, venv, imports, generation, validation, hashes, file IO', 'gc': 'collect before batch, cyclic GC disabled during timing; refcounts active', 'order': 'condition start alternates; each repeat alternates tool order', 'equality': 'recursive types and values; negative-zero sign checked; no key-order test', }, 'unit_map': {'trials[].elapsed_ns': 'ns', 'trials[].loops': '回', 'cases[].input_utf8_bytes': 'bytes', 'series[].elapsed_ns[]': 'ns', 'cases[].validation_after[].decode_typed_match': '件', 'cases[].validation_after[].encode_stdlib_typed_match': '件', 'cases[].validation_after[].roundtrip_typed_match': '件', 'cases[].validation_after[].cross_load_typed_match': '件'}, 'cases': cases, 'warmups': warmups, 'trials': trials, 'series': [{'id': g['id'], 'loops': g['loops'], 'elapsed_ns': g['elapsed_ns']} for g in groups], 'summary': groups, } save_json(out / (PREFIX + '-raw.json'), raw) print('SUCCESS: 6 cases; 252 measured batches; 36 discarded warmup batches;', 'typed value checks passed before and after timing.', flush=True) def main(): p = argparse.ArgumentParser(description=__doc__) p.add_argument('--output-dir', type=Path) p.add_argument('--worker', type=Path, help=argparse.SUPPRESS) args = p.parse_args() if args.worker is not None: worker(args.worker) return if args.output_dir is None: p.error('--output-dir is required') out = args.output_dir.resolve() if out.exists(): p.error('output directory already exists; refusing overwrite') if sys.implementation.name != 'cpython' or sys.version_info[:2] != (3, 14): p.error('requires regular GIL CPython 3.14 (fixed binary wheels)') if sys.platform != 'darwin' or platform.machine() != 'arm64': p.error('requires macOS arm64 (fixed binary wheels)') if hasattr(sys, '_is_gil_enabled') and not sys._is_gil_enabled(): p.error('requires regular GIL build') with tempfile.TemporaryDirectory(prefix='ujson-simplejson-cpu-', dir='/tmp') as tmp: temp = Path(tmp) envdir = temp / 'venv' venv.EnvBuilder(with_pip=False).create(envdir) vp = envdir / 'bin' / 'python' site = Path(subprocess.check_output([str(vp), '-I', '-B', '-c', 'import sysconfig;print(sysconfig.get_paths()["purelib"])'], text=True).strip()) dependencies = [] for spec in WHEELS: wheel = temp / spec['filename'] fetched = datetime.now(timezone.utc).isoformat() subprocess.run(['curl', '--fail', '--silent', '--show-error', '--location', '--max-time', '120', '--output', str(wheel), spec['url']], check=True) data = wheel.read_bytes() actual_sha = sha(data) if actual_sha != spec['sha256'] or len(data) != spec['bytes']: raise RuntimeError('wheel size/hash mismatch: ' + spec['name']) with zipfile.ZipFile(wheel) as z: for name in z.namelist(): if not (site / name).resolve().is_relative_to(site.resolve()): raise RuntimeError('unsafe wheel member') z.extractall(site) dependencies.append({**spec, 'actual_sha256': actual_sha, 'actual_bytes': len(data), 'fetched_at_utc': fetched, 'metadata_url': 'https://pypi.org/pypi/%s/%s/json' % (spec['name'], spec['version'])}) work = temp / 'result' proc = subprocess.run([str(vp), '-I', '-B', str(Path(__file__).resolve()), '--worker', str(work)], capture_output=True, text=True) print(proc.stdout, end='', flush=True) if proc.returncode: print(proc.stderr, file=sys.stderr) raise RuntimeError('worker failed; no output copied') save_json(work / (PREFIX + '-dependencies.json'), { 'source': 'public PyPI wheel metadata; pinned filenames/versions/expected SHA256', 'venv': 'TemporaryDirectory /tmp; pip-free; wheel extraction; removed at exit', 'packages': dependencies}) (work / (PREFIX + '-run-log.txt')).write_text(proc.stdout, encoding='utf-8') shutil.copyfile(__file__, work / (PREFIX + '-replay.txt')) out.mkdir(parents=True, exist_ok=False) for f in work.iterdir(): with (out / f.name).open('xb') as dest: dest.write(f.read_bytes()) print('Results saved to new directory:', out, flush=True) if __name__ == '__main__': main()