Skip to content

Cache LoadLocation results - #34

Merged
mjradwin merged 1 commit into
mainfrom
claude/cache-loadlocation
Jul 9, 2026
Merged

Cache LoadLocation results#34
mjradwin merged 1 commit into
mainfrom
claude/cache-loadlocation

Conversation

@mjradwin

@mjradwin mjradwin commented Jul 9, 2026

Copy link
Copy Markdown
Member

time.LoadLocation re-parses the zoneinfo out of the tzdata database on every call and does not cache internally (only UTC/Local are special-cased). zmanim.New loads a timezone from its TimeZoneId string on every construction, so a multi-day date range re-parses the same zone once per day.

Measured against the standard library (embedded tzdata):

Call Time Allocs
time.LoadLocation("America/New_York") ~11.7 µs 8.6 KB, 13 allocs
time.LoadLocation("Asia/Jerusalem") ~8.7 µs 5.9 KB, 16 allocs
cached hit (this PR) ~20 ns 0 allocs

Change

  • Add a public zmanim.LoadLocation(name string) (*time.Location, error) that memoizes successful lookups in a sync.Map. The returned *time.Location is immutable and safe to share.
  • Use it in zmanim.New.
  • Only successful lookups are cached — an unknown tzid is caller-controlled, potentially unbounded input, so caching failures could grow the map without bound. Invalid tzids still return an error.

Verification

go build ./..., go vet ./zmanim/, gofmt -l, and the full go test ./... are clean; a new TestLoadLocation covers the cached-pointer identity and the invalid-tz error path.

This is the shared home for the cache that hebcal-api-go will reuse (it has several of its own time.LoadLocation call sites for tzid validation and response Expires headers).

🤖 Generated with Claude Code

https://claude.ai/code/session_01SnNqQE8n9SckYpeaHgcWsV


Generated by Claude Code

time.LoadLocation re-parses the zoneinfo out of the tzdata database on every
call (~10µs and several KB of garbage for a typical zone) and does not cache
internally. zmanim.New loads a timezone from its tzid string on every
construction, which for a multi-day date range means re-parsing the same zone
once per day.

Add a public zmanim.LoadLocation that memoizes successful lookups in a
sync.Map (the returned *time.Location is immutable and safe to share), and use
it in zmanim.New. Cached hits drop from ~10µs/~8KB to ~20ns/0 allocs. Only
successful lookups are cached, since an unknown tzid is caller-controlled,
potentially unbounded input.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SnNqQE8n9SckYpeaHgcWsV
@sonarqubecloud

sonarqubecloud Bot commented Jul 9, 2026

Copy link
Copy Markdown

@mjradwin
mjradwin merged commit 5ec2794 into main Jul 9, 2026
6 checks passed
@mjradwin
mjradwin deleted the claude/cache-loadlocation branch July 9, 2026 22:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants