# Overloading \`Base.getproperty\` performance

**URL:** https://discourse.julialang.org/t/overloading-base-getproperty-performance/54178
**Category:** Performance
**Tags:** question, performance
**Created:** [January 29, 2021, 12:12pm UTC](https://discourse.julialang.org/t/overloading-base-getproperty-performance/54178 "2021-01-29T12:12:26Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![jlbosse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlbosse/32/11274_2.png) [@jlbosse](https://discourse.julialang.org/u/jlbosse)
#### Post date: [January 29, 2021, 12:12pm UTC](https://discourse.julialang.org/t/overloading-base-getproperty-performance/54178/1 "2021-01-29T12:12:27Z")

</div>

Inspired by [this post](https://discourse.julialang.org/t/help-with-base-getproperty-syntax/16628/5) I wanted to add extra properties to my type, but noted a significant performance hit:

```julia
julia> using BenchmarkTools

julia> struct AType{T}
            data::T
       end

julia> struct BType{T}
           data::T
       end

julia> _getproperty(x::AType, ::Val{s}) where {s} = getfield(x, s)
julia> _getproperty(x::AType, ::Val{:two}) = 2*x.data
julia> Base.getproperty(x::AType, s::Symbol) = _getproperty(x, Val{s}())

julia> a = AType(1.0)
julia> b = BType(1.0)

julia> @benchmark b.data
BenchmarkTools.Trial: 
  memory estimate: 48 bytes
  allocs estimate: 3
  --------------
  minimum time: 69.511 ns (0.00% GC)
  median time: 73.613 ns (0.00% GC)
  mean time: 78.808 ns (2.21% GC)
  maximum time: 1.978 μs (95.25% GC)
  --------------
  samples: 10000
  evals/sample: 977

julia> @benchmark a.data
BenchmarkTools.Trial: 
  memory estimate: 48 bytes
  allocs estimate: 3
  --------------
  minimum time: 5.675 μs (0.00% GC)
  median time: 5.855 μs (0.00% GC)
  mean time: 5.909 μs (0.00% GC)
  maximum time: 13.520 μs (0.00% GC)
  --------------
  samples: 10000
  evals/sample: 6

```

As you can see, adding a custom `getproperty(x::AType, ...)` function makes accessing all properties (not only `.two`) much (almost 100x) slower. Are there other, more clever ways to overload `getproperty`, or will checking the value of `s` cost me performance, no matter what?

---

<div class="post-metadata">

### Author: ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)
#### Post date: [January 29, 2021, 12:15pm UTC](https://discourse.julialang.org/t/overloading-base-getproperty-performance/54178/2 "2021-01-29T12:15:51Z")

</div>

global scope?

`@btime $b.data`?

---

<div class="post-metadata">

### Author: ![jlbosse](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlbosse/32/11274_2.png) [@jlbosse](https://discourse.julialang.org/u/jlbosse)
#### Post date: [January 29, 2021, 12:23pm UTC](https://discourse.julialang.org/t/overloading-base-getproperty-performance/54178/3 "2021-01-29T12:23:05Z")

</div>

Yes, thats it. I was confused for a second, because I thought this would extrapolate the whole `b.data` into the expression, making it efficiently constant. But putting a small wrapper function around it all I got the same, faster times.
