Skip to content

perf(durable): cache each type's full spelling - #273

Merged
reka18 merged 3 commits into
mainfrom
refactor/durable-service-result-cleanups
Nov 26, 2026
Merged

reka18 merged 3 commits into
mainfrom
refactor/durable-service-result-cleanups

Conversation

@reka18

@reka18 reka18 commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

What this does

typeName spells a result's full Go type, with import paths kept inside unnamed composites. It runs on every service settle and every replayed one, and it walked reflect recursively each time. The spelling is now cached per reflect.Type, in a cache each Runner owns, so there's no package-level state. A cached entry is always the right spelling, because two reflect.Type values are equal exactly when the types are identical.

The cache is capped. A host that mints result types at runtime with reflect.StructOf could otherwise grow it without bound. Past the cap, a type is spelled and not stored.

WithResultTypeAlias and StateAt have no Runner in reach, so they spell uncached. That changes nothing they return.

Behavior doesn't change, and there's no new API and no CHANGELOG entry.

The other two items in #255

Numbers

Medians of -count=5 -benchmem, before and after:

Benchmark Before After
FullTypeName_DeepUnnamedType (new) 569.5 ns, 2,456 B, 21 allocs 4.9 ns, 0 B, 0 allocs (cache hit)
RunService_Settle 3,129 ns, 64 allocs 3,033 ns, 63 allocs
Recover_ServiceResults (200 settles) 321,048 ns, 9,054 allocs 314,608 ns, 8,859 allocs

Tests

The type-name and parked-type tests check exact spellings on cache hits. Keying the cache on Kind instead of the type fails all three type-name tests. Three new tests:

  • the cap holds, and every spelling past it is still exact;
  • goroutines settling through one Runner's cache get exact spellings, and the entry count stays within the cap's bound;
  • two Runners share no entries.

Checks

  • mage check passes in every module (go1.27.4)
  • go test -race ./durable/...

Closes #255

@reka18 reka18 added not-ready On every new PR until all review dimensions are confirmed wait:correctness ACR pending: correctness wait:regression-safety ACR pending: regression-safety wait:concurrency-discipline ACR pending: concurrency-discipline wait:replay-determinism ACR pending: replay-determinism wait:api-compatibility ACR pending: api-compatibility wait:missed-context ACR pending: missed-context wait:human-readability ACR pending: human-readability wait:probes ACR: the probes lens is still open labels Nov 18, 2026
@reka18

reka18 commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

Review findings

The AI reviewers' pass over this change checked every review label. They raised one plausible issue, which is now fixed:

  1. The cache never evicted. A host that mints result types at runtime with reflect.StructOf would grow it for the life of the process. It's now capped, and a test pins the cap.

Two candidates were refuted:

  • Concurrency. sync.Map keyed by reflect.Type never calls user code. Concurrent misses store identical strings, so which store wins can't be observed.
  • A missing probe. Existing tests already check exact spellings on a cache hit.

@reka18

reka18 commented Nov 18, 2026

Copy link
Copy Markdown
Contributor Author

Decisions for Temple to ratify

I made these calls in this PR. Each is a suggestion for you to confirm or change.

  1. The settledJSON holder stays for now. Removing it means returning the recorded outcome through the exported WithResultTransform and WithSettleTransform signatures, so it's your call. I filed it as durable: keep or remove the settledJSON holder between a settle and its journal Record #271 with both options.
  2. fullTypeName keeps its full spelling. The shorter form would let struct{ R alpha.Result } and struct{ R beta.Result } match, which loosens the replay type check.
  3. The spelling cache is capped, so a host that mints types at runtime can't grow it without bound. The cap is an unexported constant of about 4,096 entries.

@reka18
reka18 force-pushed the fix/durable-stopactor-actor-sets branch from 78bcfe1 to 7cd223c Compare November 25, 2026 20:42
Base automatically changed from fix/durable-stopactor-actor-sets to main November 25, 2026 21:08
Every settle, park and replay type check asks a result's type name, and an
unnamed type's spelling walks its whole shape, but the same handful of result
types settle again and again. fullTypeName now caches each type's spelling on
its reflect.Type - two reflect.Types compare equal exactly when the types are
identical, so a cached spelling is always the one its own type spells - and
the spelling itself doesn't change.

The cache is capped at 4096 entries: past the cap a type is spelled and
returned unstored, so runtime-minted reflect.StructOf types can't grow it
without end.

Item 1, the settledJSON holder, moved to #271 for Temple's ruling, since
Option B would put a recording concern in state's public signatures. Item 3
stays as it is: the full spelling keeps the replay type check strict.

Closes #255.

Signed-off-by: Ray Karnes <ray.karnes@stablekernel.com>
The cache's own concurrency had no test: no test spells types from two
goroutines at once, so simultaneous first misses on one type, hits under
load and a fill racing the cap were unpinned - a regression in the
counter or the cap check would ship unnoticed through the -race gate.

A new probe runs every spelling concurrently: each goroutine spells more
distinct StructOf-minted structs than a lowered cap admits, all built
from the same shared types, then the shared types themselves. Whatever
the interleaving, every spelling must be its type's own, the counter
must count each stored entry exactly once, and the fill can pass the cap
only by one in-flight store per goroutine that raced it.

The benchmark's comment now says what it measures: the first iteration
spells and stores, every one after - the steady state for a settling
run - is a hit, so the reported number is the hit, not the first
spelling.

Signed-off-by: Ray Karnes <ray.karnes@stablekernel.com>
Signed-off-by: Ray Karnes <ray.karnes@stablekernel.com>
@reka18
reka18 force-pushed the refactor/durable-service-result-cleanups branch from 700dac5 to 774cc37 Compare November 25, 2026 23:06
@reka18 reka18 added confirmed:correctness ACR clean: correctness confirmed:regression-safety ACR clean: regression-safety confirmed:concurrency-discipline ACR clean: concurrency-discipline confirmed:replay-determinism ACR clean: replay-determinism confirmed:api-compatibility ACR clean: api-compatibility confirmed:missed-context ACR clean: missed-context and removed wait:correctness ACR pending: correctness wait:regression-safety ACR pending: regression-safety wait:concurrency-discipline ACR pending: concurrency-discipline wait:replay-determinism ACR pending: replay-determinism wait:api-compatibility ACR pending: api-compatibility labels Nov 25, 2026
@reka18 reka18 added confirmed:human-readability ACR clean: human-readability confirmed:probes ACR: the probes lens's latest round found no blocker ready All ACR dimensions confirmed - clear to merge and removed wait:missed-context ACR pending: missed-context wait:human-readability ACR pending: human-readability wait:probes ACR: the probes lens is still open not-ready On every new PR until all review dimensions are confirmed labels Nov 25, 2026
@reka18
reka18 marked this pull request as ready for review November 25, 2026 23:13
@reka18
reka18 merged commit 890026b into main Nov 26, 2026
123 checks passed
@reka18
reka18 deleted the refactor/durable-service-result-cleanups branch November 26, 2026 00:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

confirmed:api-compatibility ACR clean: api-compatibility confirmed:concurrency-discipline ACR clean: concurrency-discipline confirmed:correctness ACR clean: correctness confirmed:human-readability ACR clean: human-readability confirmed:missed-context ACR clean: missed-context confirmed:probes ACR: the probes lens's latest round found no blocker confirmed:regression-safety ACR clean: regression-safety confirmed:replay-determinism ACR clean: replay-determinism ready All ACR dimensions confirmed - clear to merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

durable: service-result cleanups (settle output holder, typeName cost, fullTypeName size)

2 participants