Candid endpoint size and instruction experiment
The current isolated implementation reduces the 128-schema fixture from 10,413,682 to 6,015,471 bytes (42.23%). Replica executable code falls from 9,328,357 to 4,433,906 bytes (52.47%). Matched PocketIC measurements show cheaper encoding and decoding in every sampled update and query, including fresh queries. The experiment vendors and changes both Candid 0.10.38 and its derive; production SDK dependencies remain unchanged.
| Feature-off 128-schema artifact | Upstream baseline | Current implementation | Change |
|---|---|---|---|
| Prepared Wasm, bytes | 10,413,682 | 6,015,471 | −42.23% |
| Replica code bytes | 9,328,357 | 4,433,906 | −52.47% |
| Gzip bytes | 1,453,065 | 426,147 | −70.67% |
Both artifacts install on PocketIC 16.0.0. The original reproduction crosses the
explicit 10,000,000-byte experiment budget; it does not reproduce an IC
installation rejection or establish a regression against an older Candid release.
The replica code cap in this pinned release is 12 MiB. Compression does not change
that decompressed-code limit. The current module still includes a 1,190,252-byte
plain debug name section; these results do not depend on stripping names.
Fixture and diagnosis
canister/build.rs generates ordinary typed #[cdk_rsx::query] and
#[cdk_rsx::update] endpoints, alternating modes. Each echoes a transport value
through a scoped root service. The root owns an empty versioned memory contract
and normal SDK lifecycle hooks. There are no padding arrays or large assets.
Each medium schema has eight records/enums, nesting options, vectors and variants;
one record field label differs per endpoint, making the wire schemas distinct.
The original SDK revision 80b18c3, Candid 0.10.38 and pinned release profile
produced this scaling baseline. Every listed artifact installed and passed
nonempty endpoint roundtrips.
| Schema | Endpoints | Prepared Wasm | Replica code bytes | Gzip bytes |
|---|---|---|---|---|
| Shared | 1 | 1,112,657 | 838,310 | 313,261 |
| Shared | 32 | 1,179,968 | 900,629 | 315,650 |
| Shared | 128 | 1,374,945 | 1,082,837 | 318,993 |
| Distinct | 1 | 1,112,657 | 838,310 | 313,261 |
| Distinct | 32 | 3,389,956 | 2,914,117 | 598,250 |
| Distinct | 128 | 10,413,682 | 9,328,357 | 1,453,065 |
At 128 endpoints, distinct schemas add 8,245,520 code bytes over shared schemas;
6,546,397 bytes (79.39%) are in functions named for deserialization or visitors.
Original repeated Record::deserialize bodies occupy 17,268 bytes each.
Generated type-description construction accounts for another 1,093,593 bytes,
while surviving serialization wrappers account for only 4,272 bytes.
Names are diagnostic attribution, not exclusive causality after inlining.
Baseline and function analysis
preserve the evidence. A 192-schema build was stopped at the user's request;
there is no installation result for it.
The main problem is repeated specialized machinery. Merely combining nested request types into one large generated function does not share that machinery across requests. The implementation instead shares decoder preparation and visitor boundaries, compiles type metadata into compact descriptors, caches immutable field ids, and avoids repeated structural comparisons. It uses one primitive-element decoder with direct scalar paths, retaining shared fallback handling for custom visitors. Typed replies reuse completed encoded headers; decoding can reuse the last eligible validated wire header. Values and quotas remain checked on every call. Implementation and cache contracts explain the source changes and their limits.
Matched instruction results
The current comparison uses the same 132-method instrumented harness for upstream and optimized builds: 128 echo endpoints, a metrics query, and three query report probes. Feature-off 128-method artifacts determine the size table above. Source receipts confirm that the optimized size and instrumented builds have identical runtime, derive, SDK and fixture sources; only measurement features differ.
These are raw ic0.performance_counter(0) deltas around the existing SDK codecs,
not host timings, cycles or complete endpoint costs. The adjacent-counter control
is 200 throughout, without subtraction. Decode excludes ingress copying; encode
includes consumed-value destruction but excludes sending the reply. Query reports
carry counters in the same reply, with outer report encoding excluded.
Fresh installs for update endpoints 001, 065 and 127 each run one first update and three repeated updates per payload. The table gives warmed medians; ranges show variation across the three endpoints.
| Payload | Request bytes | Baseline decode | Optimized decode | Baseline encode | Optimized encode |
|---|---|---|---|---|---|
| Small nested request | 726 | 1,096,962 | 408,755–488,755 | 643,849 | 34,224 |
| 96 records | 10,770 | 7,620,987 | 5,360,956–5,440,956 | 1,206,271 | 754,731 |
| Large vector/blob | 99,006 | 8,008,386 | 3,698,401–3,778,401 | 1,225,037 | 616,395 |
Warm decode uses 55.44–62.74%, 28.61–29.66% and 52.82–53.82% fewer instructions, respectively. Warm encode uses 94.68%, 37.43% and 49.68% fewer. Every paired first update also improves both intervals. Endpoint 001's warmed decode is exactly 80,000 instructions above the other two; this spread is recorded without assigning a cause that has not been traced. Raw updates, matched baseline and comparison retain every observation.
Actual queries use unique deterministic callers to bypass cached query results. For each method/payload pair, three queries start from fresh installation state, one matching ordinary update commits, then three queries start from that state. Sequence checks confirm query writes were discarded. All 54 queries and nine warming updates per revision return the exact original inner reply bytes.
| Fresh-state query payload | Decode change | Encode change | Sum of codec intervals |
|---|---|---|---|
| Small nested request | −0.68% | −27.94% | −9.26% |
| 96 records | −18.91% | −12.71% | −18.08% |
| Large vector/blob | −43.97% | −12.30% | −40.10% |
Each fresh query phase repeats identically across all three endpoints. Queries following a matching committed update reduce the sum of codec intervals by 70.10–74.67%, 31.01–31.92% and 53.89–54.80%, respectively. Every individual query decode and encode interval improves. These sums are not complete query costs. Compare queries with queries; their counter values need not equal updates. Raw queries, matched query baseline and validated comparison preserve the caller schedule, phase checks and ranges. Query methodology and counter methodology describe the exact protocol.
Both revisions also complete three separate sweeps through all 64 update schemas: 192 exact-byte calls per revision. Committed Wasm extent remains 18,153,472 bytes (277 pages). This does not measure live cache allocation: the SDK reserves a 14 MiB transient arena. The parsed-header cache admits at most 4 KiB of wire bytes and 256 table entries, but retains the associated type graph too. A native allocation probe for this 312-byte/26-entry header retains 13,326 requested heap bytes; it is not an IC tracking or total-cache measurement. Typed header entries and expected type graphs can accumulate across compiled endpoint types. The single parsed-header slot primarily helps repeated same-schema traffic; alternating eligible headers replace it. Queries alone do not retain newly populated caches.
Historical selection evidence
All rows below use feature-off 128-schema artifacts and installed successfully. Only candidates with separately instrumented runs have IC cost evidence.
| Candidate | Prepared Wasm | Replica code bytes | Gzip bytes | Interpretation |
|---|---|---|---|---|
| Map preparation only | 8,732,806 | 7,770,335 | 1,303,699 | Accepted starting path; no measured penalty |
| Map plus quota/type helper outlining | 7,905,492 | 6,875,945 | 436,332 | Rejected: decode up to 5.22% slower |
| Composite preparation alone | 8,696,315 | 7,851,344 | 448,142 | Rejected size screen: code grew versus map-only |
| Shared descriptors, labels and entrypoints | 7,420,659 | 5,895,427 | 454,998 | Improved warm costs; some first decode costs rose |
| Whole-vector primitive specialization | 8,946,006 | 6,411,370 | 477,208 | Faster large vectors, but duplicated visitor loops |
| Single primitive-element representation | 6,279,644 | 4,999,656 | 431,532 | Removed duplication, retained part of vector gain |
| Shared seeds plus header caches | 6,503,155 | 4,921,372 | 440,269 | Size-only predecessor of the final source |
| Current optimized implementation | 6,015,471 | 4,433,906 | 426,147 | Matched query/update results above |
Historical raw receipts remain unchanged. Their original cache-policy text
incorrectly said a decode warmed that same call's encode: upstream
IDLBuilder::new clears its type environment on every encode. Other type/name
state and allocator state persist. The old numeric results and timed boundaries
are unaffected; the erratum makes the correction explicit.
The current query/update baseline reruns upstream with the current harness.
Qualification and reproduction
The final source passes 28 fixture contract tests with 20 exact quota snapshots,
six dynamic-value tests, eight encoded-header tests, eleven wire-header tests,
and seven primitive-vector tests. Paired registry probes establish wire/error/quota
expectations. Upstream library tests pass with default and value features:
16 passed in each configuration, with one upstream bincode test still intentionally
ignored. Native probes retain runners and source receipts.
These are targeted checks, not exhaustive protocol or custom-visitor cost coverage.
This synthetic fixture omits business logic, persistent mutation, upgrades,
rollback and asynchronous application calls. It does not qualify those behaviors.
The prototype changes SharedLabel's source API and requires static schemas for
cached tuple encoders; dynamic builders remain available. Review
provenance and the IC review
before treating it as production dependency adoption.
From the worktree root, with the pinned toolchain and cached dependencies:
export POCKET_IC_BIN=/absolute/path/to/pocket-ic-16.0.0
python3 research/experiments/2026-10-17-candid-wasm-size/test_size.py \
--counts 128 --schemas distinct --out \
research/experiments/2026-10-17-candid-wasm-size/artifacts-recheck --install
The driver sources ver1/tools/env.sh and the Wasm environment, builds matching
native descriptors, and transforms/validates final artifacts through
cdk-rsx-wasm. Chunked installation verifies module hashes and exact query/update
roundtrips. Its 10,000,000-byte default budget is a regression-test threshold,
not an IC limit. Failed size/installation reports are retained.
Use measure_instructions.py with --queries for the
matched runtime protocol; query-measurement.md shows the
upstream overrides and optional residency sweeps. Source patches, compiler/flag
identities, native results and raw counters are retained under candidates/,
native-probes/ and evidence/. Local Wasms, requests and bulk build logs remain
in ignored artifact directories. IC limits distinguish upload
size, total module bytes, debug names and decompressed executable code.