Cache LoadLocation results - #34
Merged
Merged
Conversation
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
|
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.



time.LoadLocationre-parses the zoneinfo out of the tzdata database on every call and does not cache internally (onlyUTC/Localare special-cased).zmanim.Newloads a timezone from itsTimeZoneIdstring on every construction, so a multi-day date range re-parses the same zone once per day.Measured against the standard library (embedded tzdata):
time.LoadLocation("America/New_York")time.LoadLocation("Asia/Jerusalem")Change
zmanim.LoadLocation(name string) (*time.Location, error)that memoizes successful lookups in async.Map. The returned*time.Locationis immutable and safe to share.zmanim.New.Verification
go build ./...,go vet ./zmanim/,gofmt -l, and the fullgo test ./...are clean; a newTestLoadLocationcovers the cached-pointer identity and the invalid-tz error path.This is the shared home for the cache that
hebcal-api-gowill reuse (it has several of its owntime.LoadLocationcall sites for tzid validation and responseExpiresheaders).🤖 Generated with Claude Code
https://claude.ai/code/session_01SnNqQE8n9SckYpeaHgcWsV
Generated by Claude Code