quinnj / JSON3.jl

Other
214 stars 47 forks source link

Union type performance #152

Closed dantaras closed 2 years ago

dantaras commented 3 years ago

If I run the code below

using JSON3, StructTypes, BenchmarkTools
struct T1
    x::Int
end
struct T2
    x::Union{Nothing, Int}
end
StructTypes.StructType(::Type{T1}) = StructTypes.Struct()
StructTypes.StructType(::Type{T2}) = StructTypes.Struct()
const s  = "{\"x\":1}"
@btime  JSON3.read($s, T1)
@btime  JSON3.read($s, T2)

I get:

  90.149 ns (1 allocation: 336 bytes)
  703.410 ns (7 allocations: 512 bytes)

Any ideas why there's such a large difference in performance here and how it could be fixed?