Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@btime reports wrong allocation #360

Open
chunjiw opened this issue Mar 11, 2024 · 1 comment
Open

@btime reports wrong allocation #360

chunjiw opened this issue Mar 11, 2024 · 1 comment

Comments

@chunjiw
Copy link

chunjiw commented Mar 11, 2024

MWE:

using BenchmarkTools

struct S
    x
end
function (s::S)()
    s.x[1]
end

function f()
    s1 = S(rand(10))
    @time s1()
    @btime $s1()
    println(@allocated s1())
    s2 = S(1)
    @time s2()
    @btime $s2()
    @benchmark $s2()
    println(@allocated s2())
end

f()

The output I see is

  0.000001 seconds
  19.435 ns (1 allocation: 16 bytes)
0
  0.000000 seconds
  18.050 ns (0 allocations: 0 bytes)
0

So when the field is a vector, @btime reports wrong allocation, assuming @time and @allocated are right. This may be related to JuliaLang/julia#53694 but I'm not sure.

julia> versioninfo()
Julia Version 1.10.2
Commit bd47eca2c8a (2024-03-01 10:14 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 12 × Intel(R) Core(TM) i7-6850K CPU @ 3.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, broadwell)
Threads: 1 default, 0 interactive, 1 GC (on 12 virtual cores)
@Zentrik
Copy link
Contributor

Zentrik commented Mar 19, 2024

It seems the @time reports no allocations as the compiler can see that s1 is constructed and then the first field is accessed and so can figure out that the type of s1() is Float64. Whilst for BenchmarkTools the s1() call happens in a non-inlined function so it cannot do the same optimisation.

It's not obvious that it could be worked around, nor that we would want to.

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

No branches or pull requests

2 participants