From f9dce3d10f4fe628823815591fb593baed9e828f Mon Sep 17 00:00:00 2001 From: jvoisin Date: Sun, 8 Jun 2025 16:12:02 +0200 Subject: [PATCH] perf(timzone): cache getLocation's results Every time getLocation is called, it's opening and parsing a file on disc, sometimes a zip file depending on the system. We can cache the results instead of doing this. See https://github.com/golang/go/issues/24844 and https://github.com/golang/go/issues/26106 --- internal/timezone/timezone.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/internal/timezone/timezone.go b/internal/timezone/timezone.go index c0746945..1efd803d 100644 --- a/internal/timezone/timezone.go +++ b/internal/timezone/timezone.go @@ -4,9 +4,14 @@ package timezone // import "miniflux.app/v2/internal/timezone" import ( + "sync" "time" ) +var ( + tzCache = sync.Map{} // Cache for time locations to avoid loading them multiple times. +) + // Convert converts provided date time to actual timezone. func Convert(tz string, t time.Time) time.Time { userTimezone := getLocation(tz) @@ -42,9 +47,15 @@ func Now(tz string) time.Time { } func getLocation(tz string) *time.Location { + if loc, ok := tzCache.Load(tz); ok { + return loc.(*time.Location) + } + loc, err := time.LoadLocation(tz) if err != nil { loc = time.Local } + + tzCache.Store(tz, loc) return loc }