Skip to content

Instantly share code, notes, and snippets.

@jrevels
Created October 2, 2019 16:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jrevels/d2e9ac44d42c5fc7659281411131ff1e to your computer and use it in GitHub Desktop.
Save jrevels/d2e9ac44d42c5fc7659281411131ff1e to your computer and use it in GitHub Desktop.
using MsgPack, BenchmarkTools
struct MyType
a::Vector{Symbol}
b::Symbol
c::Float64
d::String
e::UInt64
f::Symbol
g::Union{Nothing,Dict{Symbol,Any}}
end
m = MyType([:a, :b, :c], :c, 13.4, "asdfasdfadf", 123498192384, :zst, nothing)
#####
##### master
#####
mutable struct MutableMyType
a::Vector{Symbol}
b::Symbol
c::Float64
d::String
e::UInt64
f::Symbol
g::Union{Nothing,Dict{Symbol,Any}}
MutableMyType() = new()
end
MsgPack.msgpack_type(::Type{MyType}) = MsgPack.ImmutableStructType()
MsgPack.msgpack_type(::Type{MutableMyType}) = MsgPack.MutableStructType()
unpack_mutable(bytes) = MsgPack.unpack(bytes, Vector{MutableMyType})
unpack_immutable(bytes) = MsgPack.unpack(bytes, Vector{MyType})
bytes = pack(fill(m, 10000))
@benchmark unpack_mutable($bytes) evals=1
# BenchmarkTools.Trial:
# memory estimate: 2.06 MiB
# allocs estimate: 30003
# --------------
# minimum time: 13.091 ms (0.00% GC)
# median time: 13.822 ms (0.00% GC)
# mean time: 14.181 ms (1.92% GC)
# maximum time: 57.976 ms (70.04% GC)
# --------------
# samples: 353
# evals/sample: 1
@benchmark unpack_immutable($bytes) evals=1
# BenchmarkTools.Trial:
# memory estimate: 2.37 MiB
# allocs estimate: 50003
# --------------
# minimum time: 5.988 ms (0.00% GC)
# median time: 6.353 ms (0.00% GC)
# mean time: 6.622 ms (3.23% GC)
# maximum time: 46.284 ms (83.28% GC)
# --------------
# samples: 755
# evals/sample: 1
#####
##### PR
#####
MsgPack.msgpack_type(::Type{MyType}) = MsgPack.StructType()
unpack_strict(bytes) = MsgPack.unpack(bytes, Vector{MyType}; strict = (MyType,))
unpack_nonstrict(bytes) = MsgPack.unpack(bytes, Vector{MyType})
bytes = pack(fill(m, 10000))
@benchmark unpack_strict($bytes) evals=1
# BenchmarkTools.Trial:
# memory estimate: 2.37 MiB
# allocs estimate: 50005
# --------------
# minimum time: 6.076 ms (0.00% GC)
# median time: 6.424 ms (0.00% GC)
# mean time: 6.694 ms (3.12% GC)
# maximum time: 46.050 ms (82.95% GC)
# --------------
# samples: 747
# evals/sample: 1
@benchmark unpack_nonstrict($bytes) evals=1
# BenchmarkTools.Trial:
# memory estimate: 3.89 MiB
# allocs estimate: 70003
# --------------
# minimum time: 16.324 ms (0.00% GC)
# median time: 17.572 ms (0.00% GC)
# mean time: 17.990 ms (2.27% GC)
# maximum time: 59.383 ms (65.98% GC)
# --------------
# samples: 278
# evals/sample: 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment