Skip to content

Bounded hot-path profiling

Issue #53 asks for evidence before changing schema lookup, instrument locking, KLL quota checks or evaluator retention. This investigation uses synthetic inputs and the current owners. It changes no production implementation or public contract. The results characterize one local machine, not Collector capacity, delivery reliability, production throughput or accelerator performance.

Reproduction and retained evidence

Use the project interpreter after its SDK preflight:

.venv/bin/python docs/reviews/hot_path_probe.py --repeats 5 \
  --output docs/reviews/hot-path-profile-results.json
make test-focused TEST=tests/unit/test_hot_path_profile_contracts.py

The probe retains aggregate raw observations and repeat summaries in the result document. Five fresh processes per family give fifteen process runs. Families and configurations have deterministic shuffled order between repeats. Every configuration retains all five observations; summaries give median, minimum, maximum and sample standard deviation. These describe repeat spread, not a population confidence interval. Fresh processes avoid accumulated caches and retained objects from earlier repeats, but do not establish statistical independence from shared host load, thermal state or scheduling.

The evidence records interpreter, OS family, architecture, logical CPU count and optional dependency versions, plus hashes of the measured owners, lock and probe. It excludes hostnames, absolute source paths, case contents, candidate outputs and serialized sketch samples. The source tree contains prior issue implementations; this is evidence for those exact file hashes rather than a clean release.

Workloads and resource envelope

One family process runs at a time. Each has a parent-owned 120-second timeout. The lookup family uses at most four threads, a five-second admission barrier and thirty-second future waits. A thread that does not terminate still cannot keep the parent waiting beyond the process deadline. Each worker emits at most one MiB of accepted aggregate output. The parent checks that limit after execution; this is an evidence-size check, not a hard pipe or memory quota. Process high-water RSS is observed and retained. No CPU affinity, memory quota, CPU quota, idle-host attestation or thermal control is claimed. The probe does not use network exporters or native Collector resources.

Family Fixed inputs Observed scope
Schema and instrument 8, 64, 256 registered counters; last metric; empty attributes; 1 or 4 threads; 40,000 aggregate calls per timed cell; 2,000 warm calls Direct schema lookup, cached instrument retrieval, public observe against no-op SDK providers
Lock diagnostic Same schema/thread cells; 10,000 cached instrument retrievals Exact lock acquisitions, failed immediate acquisitions, aggregate wait and hold time under an instrumented lock
KLL Real DataSketches double sketches, k=8/200/2048; 128/4,096/65,536 updates; values index modulo 1,024 Raw updates, public quota-checked adapter updates, 2,000 mature-state serializations, separate serialization-count/size diagnostic
Retention 128/1,024 cases; 1/3 repetitions; 0/4/16 fully overlapping slices; one constant-score Measurement; 100 bootstrap resamples; 4,096-byte synthetic outputs Single/paired evaluation, peak and returned live outputs, traced Python allocations, report bytes and release while aggregate reports remain

The last-metric lookup is a declared worst-position workload, not a production metric distribution. Cached instrument timing excludes creation, exporter queues and real SDK aggregation. Public observe includes schema/value validation and translation, but its no-op provider makes it an isolated adapter cost. The lock diagnostic adds clocks and an immediate-acquire attempt; its wait/hold values are perturbed diagnostics and cannot be subtracted from uninstrumented public-call timing.

Raw KLL and adapter timing run without diagnostic wrappers. Their difference includes finite-value validation, Python dispatch, serialization and quota checks; it is not an isolated quota-comparison instruction cost. Mature serialization uses the final populated raw sketch and does not represent every intermediate update. A separate wrapper counts one serialization per accepted update and records the maximum serialized length, without retaining the bytes. Serialized state is a bounded wire-size proxy, not full native heap usage; process RSS covers all cells in that family and does not attribute native allocator cost to one sketch. Sampled endpoints and default k characterize the supported k range, not every integer k or every possible input distribution. DataSketches compaction randomness is not controlled by this adapter.

Retention timing includes WeakSet bookkeeping, synthetic byte allocation and tracemalloc. It is an instrumented workload, unsuitable for an uninstrumented speed claim. Tracemalloc starts after evaluator construction, so case/schema planning allocations are excluded. Peak live output counts and release are observable lifetime evidence; traced bytes are Python allocations, not total process memory. Overlapping slices share observation/output references, rather than copying the output bytearrays. The largest paired workload creates 6,144 outputs with 24 MiB of synthetic byte storage; its additional observation, membership, score, summary and temporary allocations appear in traced peak/RSS evidence.

Throughput meaning and contract evidence

Single evaluation divides successful cases by trial-loop wall time. That scope includes invocation, observation construction, slice dispatch and failed attempts, but excludes Measurement reduction after the loop. Paired evaluation divides each candidate's successful cases by the sum of that candidate's successful call durations. It excludes peer calls, failed-call time, scheduling, observation construction, pairing, slice selection and Measurement reduction. Both pair reports separately retain the overall interleaved session elapsed time; their throughput fields do not describe session throughput. These existing denominators are preserved. Comparing the two throughput fields as though they shared a denominator would be invalid.

The observable contracts use a deterministic clock to prove these scopes, weak references to prove private outputs release while aggregate reports remain, and real installed DataSketches to count serialized quota checks and reject NaN before update. No test gates machine timing or changes Consumer Measurement meaning. Existing disabled-import, privacy and Safety Floor gates remain applicable.

Qualification limits and historical ordering defect

An initial 16-slice paired workload used numerical suffixes without padding. Membership order then differed from lexicographically sorted report slices, and the public pair constructor rejected it with paired slice membership must match report slice declarations. The measured configurations use zero-padded, consistently ordered slice declarations. Review issue 15 subsequently corrected declaration reconciliation to compare canonical slice identities; regression tests cover arbitrary declaration order. That correction does not change the historical workload or qualify new performance results.

Measured findings and decision

The retained run uses CPython 3.14.7, Darwin 27.0.0, arm64, 12 logical CPUs, DataSketches 5.2.0 and OpenTelemetry SDK 1.44.0. Its Python GIL is enabled. Agent-owned verification processes had finished before the retained profiling run; other host activity remains uncontrolled. The complete run contains 285 aggregate observations across 57 configurations and five fresh-process repeats per family. Family-process RSS high-water values and all raw observations remain in the result document.

The following representative cells show median cost and the five-repeat minimum–maximum range. They are explanatory local measurements, not acceptance thresholds. The result document is authoritative for the exact values and includes sample standard deviations and the other cells.

Single-thread lookup cell Median ns/call Repeat range ns/call
Last metric, 8 registered 157 155–163
Last metric, 64 registered 802 774–811
Last metric, 256 registered 2,745 2,684–2,803
Cached instrument, 256 registered 115 114–121
Public observe/no-op SDK, 256 registered 3,467 3,388–3,532

Four-thread aggregate costs were similar for this GIL-enabled workload. All diagnostic runs observed zero failed immediate lock acquisitions out of 10,000 acquisitions. This is absence of observed contention in these cached, short critical sections; it does not rule out contention during cold instrument creation, real SDK aggregation, free-threaded Python or different scheduling. Lookup scales with tuple position as expected. An index remains a possible optimization for large real Consumer schemas, but these worst-position/no-op cells do not establish its end-to-end benefit.

KLL cell, 65,536 updates Raw update median ns Adapter median ns (range) Mature serialization median ns Maximum observed serialized bytes
k=8 63 450 (446–467) 121 920
k=200 61 583 (572–623) 240 4,752
k=2048 65 2,017 (1,991–2,110) 1,768 42,736

Every diagnostic cell counted exactly one serialization per accepted update. Larger mature states increase the serialization component substantially. The largest observed state is well below the 1,048,576-byte accumulator quota, but this input sample does not certify every possible stream. Amortizing checks would weaken per-update enforcement unless a separately proven upper bound preserved the existing safety contract. No such change is made on these timings.

1,024 cases, 3 repetitions Peak live outputs Outputs alive after return Approximate traced peak MiB
Single, no slices 1,024 0 4.49
Single, 4 overlapping slices 3,072 0 13.62
Single, 16 overlapping slices 3,072 0 14.76
Paired, no slices 6,144 6,144 27.45
Paired, 4 overlapping slices 6,144 6,144 27.77
Paired, 16 overlapping slices 6,144 6,144 28.75

Live-output counts were identical across repeats. Fully overlapping slices increase reference and reduction overhead, but do not multiply output byte storage. Single sliced evaluation retains outputs across repetitions until reduction; ordinary unsliced evaluation releases each trial. Paired evaluation deliberately returns private per-pair outputs, so its output lifetime spans all repetitions even without slices. Releasing the private paired result leaves zero live outputs while its aggregate reports remain usable. Consumers retaining private paired results therefore own the corresponding memory lifetime. The measurements do not justify changing that statistical evidence contract.

The decision is to keep the existing deep owners and safety limits. No architectural optimization, public contract change, wire version change or production speed guarantee follows from this study. Any follow-up needs an actual Consumer workload, matching timing scopes, controlled environment evidence and unchanged observable safety/lifetime contracts.