Cache elapsedDays in a fixed-size atomic.Int32 array - #5
Merged
Conversation
Reintroduces the elapsedDays memoization that was removed in 4750e9d, but backed by a fixed-size atomic.Int32 array indexed directly by year rather than a mutex-protected map. Each slot is written at most once with a deterministic value, so atomic load/store is sufficient — and on x86/arm it lowers to the same MOV instructions as a plain load. Microbenchmarks (ns per elapsedDays call): workload nocache atomic-array hot (1 yr) 15 2 warm (10 yrs) 19 3 cold (random) 43 3 out-of-range 15 15 The cache covers Hebrew years 5000-6999 (~1240 CE to 3239 CE), which spans every realistic use of the library. Years outside the window fall through to recompute, so nothing breaks for callers operating on extreme dates — the cache cannot grow and contributes ~8 KB of static memory in exchange for a 5-15x speedup on the common path.
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Reintroduces the
elapsedDaysmemoization that was removed in 4750e9d, but backed by a fixed-sizeatomic.Int32array indexed directly by year rather than a mutex-protected map. Each slot is written at most once with a deterministic value, so atomic load/store is sufficient — and on x86/arm it lowers to the same MOV instructions as a plain load.The cache covers Hebrew years 5000–6999 (~1240 CE to 3239 CE), which spans every realistic use of the library. Years outside the window fall through to recompute, so nothing breaks for callers operating on extreme dates. The cache cannot grow and contributes ~8 KB of static memory.
Why this shape
The previous mutex-protected map cache was a net slowdown on every realistic workload (a sync.RWMutex round-trip costs more than
elapsedDays0itself), which is what motivated 4750e9d. A year-indexed array avoids the lock entirely; usingatomic.Int32rather than a plainint32slot keeps it race-free per the Go memory model at zero runtime cost on the platforms we run on.Microbenchmarks (ns per
elapsedDayscall, Go 1.24, x86-64):That's a 5–15× speedup on every in-range workload; one
ToRDcall invokeselapsedDays~5 times for the same year (viaDaysInYear→LongCheshvan/ShortKislev), so the win compounds.Test plan
go test ./...— all existing tests pass, including coverage of out-of-range years (1, 2, 123, 1234, 3671, 3762) which exercise the fall-through pathgo test -race ./...— race detector cleango vet ./...cleanstaticcheck -checks=all ./...cleangofmt -l .cleango doc -allbyte-identical tomain)Generated by Claude Code