]> Cypherpunks repositories - gostls13.git/commit
time: optimize FixedZone by caching unnamed zones by the hour
authorJoe Tsai <joetsai@digital-static.net>
Tue, 23 Aug 2022 01:19:24 +0000 (18:19 -0700)
committerGopher Robot <gobot@golang.org>
Thu, 8 Sep 2022 21:18:53 +0000 (21:18 +0000)
commite20106ac001a58e273738cb24ea61096384dc2f8
treecff4e938795a75635dcc5eec65c7e0be49292518
parenta9a398220ff387fe4924aee08365ffc374767a3a
time: optimize FixedZone by caching unnamed zones by the hour

FixedZone is transitively called by Time.UnmarshalJSON or Time.UnmarshalText
for any RFC 3339 timestamp that is not in UTC.
This function is relatively slow since it allocates 3 times.

Given that RFC 3339 never has a zone name and most offsets are by the hour,
we can cache unnamed zones on hour offsets.
Caching a Location should be safe since it has no exported fields or methods
that can mutate the Location. It is functionally immutable.

The only way to observe that the Location was cached is either
by pointer comparison or by shallow copying the struct value.
Neither operation seems sensible to do with a *time.Location.

Performance:

name           old time/op  new time/op  delta
UnmarshalText  268ns ± 2%   182ns ± 1%  -32.01%  (p=0.000 n=10+10)

Change-Id: Iab5432f34bdbb485512bb8b5464e076c03fd106f
Reviewed-on: https://go-review.googlesource.com/c/go/+/425116
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Run-TryBot: Joseph Tsai <joetsai@digital-static.net>
Auto-Submit: Joseph Tsai <joetsai@digital-static.net>
Reviewed-by: Ian Lance Taylor <iant@google.com>
src/time/time_test.go
src/time/zoneinfo.go