Can anyone explain (or reproduce) the following?
Why is the Base.hash
version slower and why the allocation?
julia> using BenchmarkTools
julia> mutable struct Foo end
julia> struct Bar
f::Foo
x::Int64
end
julia> Base.hash(b::Bar, h::UInt64) = hash(objectid(b.f), hash(b.x, h))
julia> Base_hash(b::Bar, h::UInt64) = hash(objectid(b.f), hash(b.x, h))
Base_hash (generic function with 1 method)
julia> b = Bar(Foo(), 1)
Bar(Foo(), 1)
julia> @benchmark Base.hash(b, UInt64(0))
BenchmarkTools.Trial:
memory estimate: 16 bytes
allocs estimate: 1
--------------
minimum time: 32.773 ns (0.00% GC)
median time: 35.294 ns (0.00% GC)
mean time: 37.063 ns (0.32% GC)
maximum time: 448.193 ns (91.04% GC)
--------------
samples: 10000
evals/sample: 991
julia> @benchmark Base_hash(b, UInt64(0))
BenchmarkTools.Trial:
memory estimate: 0 bytes
allocs estimate: 0
--------------
minimum time: 14.408 ns (0.00% GC)
median time: 15.383 ns (0.00% GC)
mean time: 15.637 ns (0.00% GC)
maximum time: 55.585 ns (0.00% GC)
--------------
samples: 10000
evals/sample: 998
julia> @which Base.hash(b, UInt64(0))
hash(b::Bar, h::UInt64) in Main at REPL[4]:1
julia> @which Base_hash(b, UInt64(0))
Base_hash(b::Bar, h::UInt64) in Main at REPL[5]:1
julia> versioninfo()
Julia Version 1.6.0
Commit f9720dc2eb (2021-03-24 12:55 UTC)
Platform Info:
OS: macOS (x86_64-apple-darwin19.6.0)
CPU: Intel(R) Core(TM) i5-8259U CPU @ 2.30GHz
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-11.0.1 (ORCJIT, skylake)