Cotton code
55 lines · 2.5 KB · PythonRaw
#!/usr/bin/env python3"""Pick the simulator a speed figure is measured on: the newest iPhone and thenewest iOS runtime whose dyld_sim shared cache exists on this host — or, whenno runtime has one, the newest runtime with the name saying so.A runtime without that cache loads every system framework by hand at launch:on the signing Mac (macOS 25G83) iOS 27.0 had none and its cold start read 2.6 s wherethe same app on iOS 26.5 read 1.1-1.3 s (14 Oct 2026) — a simulator cost nophone pays. Prints `<device type id> <runtime id> <runtime name>`."""import jsonimport platformimport reimport subprocessimport sysfrom pathlib import PathCACHES = Path('/Library/Developer/CoreSimulator/Caches/dyld')def has_dyld_cache(runtime, host_build, arch, caches=CACHES):    folder = caches / host_build / f"{runtime['identifier']}.{runtime['buildversion']}"    return (folder / f'dyld_sim_shared_cache_{arch}').is_file()def choose(device_types, runtimes, host_build, arch, caches=CACHES):    phones = [t for t in device_types if re.fullmatch(r'iPhone [0-9]+', t['name'])]    if not phones:        raise ValueError('no iPhone simulator device type')    ios = sorted((r for r in runtimes if r.get('isAvailable') and r['identifier'].startswith('com.apple.CoreSimulator.SimRuntime.iOS-')),                 key=lambda r: tuple(map(int, r['version'].split('.'))), reverse=True)    if not ios:        raise ValueError('no iOS simulator runtime is installed on this host')    phone = max(phones, key=lambda t: int(t['name'].split()[1]))    usable = [r for r in ios if has_dyld_cache(r, host_build, arch, caches)]    if usable:        return phone['identifier'], usable[0]['identifier'], usable[0]['name']    # No cached runtime on this host (one Mac, 14 Oct 2026: iOS 27.0 only, no    # cache folder at all). Speed is report-only until 1 Nov, so a missing    # cache must never refuse a cut: measure on the newest runtime and let the    # figure SAY it was measured without a cache, so nobody reads 2.6 s as the    # phone's cold start.    return phone['identifier'], ios[0]['identifier'], f"{ios[0]['name']} (no dyld cache)"def main():    read = lambda kind: json.loads(subprocess.check_output(['xcrun', 'simctl', 'list', kind, '-j']))[kind]    host = subprocess.check_output(['sw_vers', '-buildVersion'], text=True).strip()    print(*choose(read('devicetypes'), read('runtimes'), host, platform.machine()))if __name__ == '__main__':    try:        main()    except (ValueError, OSError, subprocess.CalledProcessError) as error:        sys.exit(f'[speed] FAILED: {error}')