Skip to content

Conversation

ettersi
Copy link
Contributor

@ettersi ettersi commented Nov 24, 2021

With this PR, the values interpolated into an @benchmarkable are stored in the Benchmark object rather than interpolated directly into the benchmark code. This ensures that interpolated values can be garbage-collected once the Benchmark object goes out of scope, in contrast to the current behaviour where values interpolated into a benchmark expression will stick around forever.

This PR would solve #127.

MWE demonstrating what works with this PR but fails otherwise:

# Assemble a benchmark with an interpolated value
x = []
x_finalized = false
finalizer(x->(global x_finalized=true), x)
b = @benchmarkable $x

# Free the memory
b = x = nothing
GC.gc()

# Check that the memory has indeed been freed
@test x_finalized

@ettersi
Copy link
Contributor Author

ettersi commented Dec 2, 2021

Bump.

@mcabbott
Copy link

mcabbott commented Dec 5, 2021

This also seems to have effects on constant propagation. Taking the example from the manual, before:

julia> a = 1; b = 2
2

julia> @btime $a + $b
  0.001 ns (0 allocations: 0 bytes)
3

julia> @btime $(Ref(a))[] + $(Ref(b))[]
  2.125 ns (0 allocations: 0 bytes)  # not 1.500 ns just because of noise I think
3

(@v1.5) pkg> st BenchmarkTools
Status `~/.julia/environments/v1.5/Project.toml`
  [6e4b80f9] BenchmarkTools v1.2.0

After:

julia> a = 1; b = 2
2

julia> @btime $a + $b
  1.500 ns (0 allocations: 0 bytes)
3

julia> @btime $(Ref(a))[] + $(Ref(b))[]
  1.500 ns (0 allocations: 0 bytes)
3

julia> @btime 1+2
  0.001 ns (0 allocations: 0 bytes)   # unchanged
3

julia> @btime $1 + $2
  2.083 ns (0 allocations: 0 bytes)
3

julia> @btime $1 + $2
  1.458 ns (0 allocations: 0 bytes)
3

(@v1.5) pkg> st BenchmarkTools
Status `~/.julia/environments/v1.5/Project.toml`
  [6e4b80f9] BenchmarkTools v1.2.1

I think that's better. Are there any examples left where an interpolated variable will defeat the benchmark? If not, perhaps the manual should be updated to remove the Ref trick in favour of $.

Edit: this doesn't seem to apply to variables from setup=:

julia> @btime a+b  setup=(a=1; b=2;)
  0.001 ns (0 allocations: 0 bytes)
3

julia> @btime a[] + b[]  setup=(a=Ref(1); b=Ref(2);)
  1.500 ns (0 allocations: 0 bytes)
3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants