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>
This commit is contained in:
Joe Tsai 2022-08-22 18:19:24 -07:00 committed by Gopher Robot
parent a9a398220f
commit e20106ac00
2 changed files with 29 additions and 0 deletions

View file

@ -1494,6 +1494,14 @@ func BenchmarkGoString(b *testing.B) {
}
}
func BenchmarkUnmarshalText(b *testing.B) {
var t Time
in := []byte("2020-08-22T11:27:43.123456789-02:00")
for i := 0; i < b.N; i++ {
t.UnmarshalText(in)
}
}
func TestMarshalBinaryZeroTime(t *testing.T) {
t0 := Time{}
enc, err := t0.MarshalBinary()

View file

@ -100,9 +100,30 @@ func (l *Location) String() string {
return l.get().name
}
var unnamedFixedZones []*Location
var unnamedFixedZonesOnce sync.Once
// FixedZone returns a Location that always uses
// the given zone name and offset (seconds east of UTC).
func FixedZone(name string, offset int) *Location {
// Most calls to FixedZone have an unnamed zone with an offset by the hour.
// Optimize for that case by returning the same *Location for a given hour.
const hoursBeforeUTC = 12
const hoursAfterUTC = 14
hour := offset / 60 / 60
if name == "" && -hoursBeforeUTC <= hour && hour <= +hoursAfterUTC && hour*60*60 == offset {
unnamedFixedZonesOnce.Do(func() {
unnamedFixedZones = make([]*Location, hoursBeforeUTC+1+hoursAfterUTC)
for hr := -hoursBeforeUTC; hr <= +hoursAfterUTC; hr++ {
unnamedFixedZones[hr+hoursBeforeUTC] = fixedZone("", hr*60*60)
}
})
return unnamedFixedZones[hour+hoursBeforeUTC]
}
return fixedZone(name, offset)
}
func fixedZone(name string, offset int) *Location {
l := &Location{
name: name,
zone: []zone{{name, offset, false}},