JuliaLogging / LoggingExtras.jl

Composable Loggers for the Julia Logging StdLib

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Allocations in TransformerLogger

baumgold opened this issue · comments

A trivial example shows that the TransformerLogger allocates even in the simplest case:

julia> using LoggingExtras, BenchmarkTools

julia> @benchmark @debug "foo"
BenchmarkTools.Trial: 10000 samples with 991 evaluations.
 Range (min  max):  40.886 ns  232.741 ns  ┊ GC (min  max): 0.00%  0.00%
 Time  (median):     42.128 ns               ┊ GC (median):    0.00%
 Time  (mean ± σ):   53.330 ns ±  20.183 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

  █▄▂▄▃▃▂▂▂▁▁         ▁▁                                       ▁
  ████████████████████████████▇▇█▇▇▇█▇███████████▇██▇▇▆▇▅▆▅▆▅▅ █
  40.9 ns       Histogram: log(frequency) by time       118 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.

julia> timestamp_logger(logger) = TransformerLogger(identity, logger);

julia> ConsoleLogger(stdout, Logging.Info) |> timestamp_logger |> global_logger;

julia> @benchmark @debug "foo"
BenchmarkTools.Trial: 10000 samples with 6 evaluations.
 Range (min  max):  5.577 μs  280.281 μs  ┊ GC (min  max): 0.00%  94.83%
 Time  (median):     5.905 μs               ┊ GC (median):    0.00%
 Time  (mean ± σ):   7.087 μs ±   6.190 μs  ┊ GC (mean ± σ):  1.79% ±  2.11%

  ▇█▅▄▄▄▃▃▃▃▃▃▃▃▃▃▂▁▁▁                       ▁▁▁▁▁▁▂▁▁▁       ▂
  ████████████████████████▇▇▇▇▇▆▆▆▆▆▄▅▅▆▅▆▇▇██████████████▇▆▅ █
  5.58 μs      Histogram: log(frequency) by time      13.3 μs <

 Memory estimate: 4.37 KiB, allocs estimate: 32.

This uses LoggingExtras v1.0.3 and Julia v1.10.0:

julia> versioninfo()
Julia Version 1.10.0
Commit 3120989f39b (2023-12-25 18:01 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 4 × Intel(R) Xeon(R) Gold 6136 CPU @ 3.00GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, skylake-avx512)
  Threads: 5 on 4 virtual cores
Environment:
  JULIA_NUM_THREADS = 4

julia> using Pkg; Pkg.status("LoggingExtras")
Status `~/.julia/environments/v1.10/Project.toml`
  [e6f89c97] LoggingExtras v1.0.3

I don't think it is possible to construct strings are run-time without allocations.
And including a timestamp in a string is such an operation.

I don't think it is possible to construct strings are run-time without allocations. And including a timestamp in a string is such an operation.

I agree with @baumgold that those allocations are unexpected, because it suggests that LoggingExtras.jl works differently than how stdlib Logging.jl works. From the docs1:

it's very cheap to leave debug statements like this in the source code because the system avoids evaluating the message if it would later be ignored

In fact @baumgold 's example shows this: the first @debug does not allocate because it would later be ignored. My expectation would be that adding a TransformerLogger that does nothing shouldn't change this.

Oh i thought that was making a timestamp logger.
cos that was the name in the code.
But I see it does the identity operation.

I agree. That should not allocate. That's unexpected and needs looking into