# Overloading Base.hash is 2x slower than an identical function

**URL:** <https://discourse.julialang.org/t/overloading-base-hash-is-2x-slower-than-an-identical-function/63058>\
**Category:** Performance\
**Created:** [June 17, 2021, 1:17am UTC](https://discourse.julialang.org/t/overloading-base-hash-is-2x-slower-than-an-identical-function/63058 "2021-06-17T01:17:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [June 17, 2021, 1:17am UTC](https://discourse.julialang.org/t/overloading-base-hash-is-2x-slower-than-an-identical-function/63058/1 "2021-06-17T01:17:30Z")

</div>

Can anyone explain (or reproduce) the following?

Why is the `Base.hash` version slower and why the allocation?

```nohighlight
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)

```

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [June 17, 2021, 1:20am UTC](https://discourse.julialang.org/t/overloading-base-hash-is-2x-slower-than-an-identical-function/63058/2 "2021-06-17T01:20:55Z")

</div>

Ah. Interpolating is the cause:

```nohighlight
julia> @benchmark Base.hash($b, UInt64(0))
BenchmarkTools.Trial: 
  memory estimate: 0 bytes
  allocs estimate: 0
  --------------
  minimum time: 8.727 ns (0.00% GC)
  median time: 8.989 ns (0.00% GC)
  mean time: 9.292 ns (0.00% GC)
  maximum time: 89.672 ns (0.00% GC)
  --------------
  samples: 10000
  evals/sample: 999

julia> @benchmark Base_hash($b, UInt64(0))
BenchmarkTools.Trial: 
  memory estimate: 0 bytes
  allocs estimate: 0
  --------------
  minimum time: 8.727 ns (0.00% GC)
  median time: 8.778 ns (0.00% GC)
  mean time: 9.336 ns (0.00% GC)
  maximum time: 71.057 ns (0.00% GC)
  --------------
  samples: 10000
  evals/sample: 999

```
