JuliaTime / TimeZones.jl

IANA time zone database access for the Julia programming language
Other
86 stars 51 forks source link

Defer loading the cache for constructing fixed time zones #463

Open omus opened 1 month ago

omus commented 1 month ago

Attempting to address this problem: https://github.com/apache/arrow-julia/pull/482#issuecomment-2123406667. The problem is that packages which use @tz_str or TimeZone at the top-level will end up triggering the lazy TZ cache load during there package initialization. For packages that just use the Class(:FIXED) time zones (e.g. "UTC") we can defer the TZ cache loading.

omus commented 1 month ago

Before this PR on Julia 1.9.4:

julia> @time_imports using TimeZones
      0.5 ms  Scratch
      8.1 ms  Preferences
      0.4 ms  PrecompileTools
     18.4 ms  Parsers
      4.3 ms  InlineStrings
      0.4 ms  TZJData
      0.7 ms  Compat
      0.3 ms  Compat → CompatLinearAlgebraExt
      0.3 ms  ExprTools
      0.6 ms  Mocking
     25.9 ms  TimeZones

julia> @time TimeZone("UTC")
  0.068178 seconds (321.75 k allocations: 13.138 MiB, 16.41% gc time, 22.11% compilation time)
UTC

julia> @time TimeZone("Europe/Warsaw")
  0.000008 seconds (2 allocations: 64 bytes)
Europe/Warsaw (UTC+1/UTC+2)

julia> @time TimeZone("UTC")
  0.000004 seconds (2 allocations: 64 bytes)
UTC

With this PR on Julia 1.9.4:

julia> @time_imports using TimeZones
      0.5 ms  Scratch
      8.2 ms  Preferences
      0.4 ms  PrecompileTools
     18.2 ms  Parsers
      4.4 ms  InlineStrings
      0.4 ms  TZJData
      0.7 ms  Compat
      0.3 ms  Compat → CompatLinearAlgebraExt
      0.3 ms  ExprTools
      0.6 ms  Mocking
     26.1 ms  TimeZones

julia> @time TimeZone("UTC")
  0.000093 seconds (6 allocations: 368 bytes)
UTC

julia> @time TimeZone("Europe/Warsaw")
  0.089128 seconds (321.75 k allocations: 13.138 MiB, 17.40% gc time, 33.12% compilation time)
Europe/Warsaw (UTC+1/UTC+2)

julia> @time TimeZone("UTC")
  0.000020 seconds (6 allocations: 368 bytes)
UTC

There is a minor trade off here as we now always create the fixed time zone instead of loading it from the cache if it exists.

omus commented 1 month ago

I have some concerns about the lazy loading now. We seem to just be passing the buck on performing the cache initialization. I especially don't like it now that packages which use time zones may see bad timings with @time_imports from using @tz_str