I get a performance hit to deepcopy
once I import an unrelated package, CSV
. How can I fix this?
import BenchmarkTools
mutable struct GameState
gameScore::Vector{Int64}
setScore::Vector{Int64}
matchScore::Vector{Int64}
serve::Int64
end
BenchmarkTools.@benchmark deepcopy(GameState([0,0],[0,0],[0,0],-1))
BenchmarkTools.Trial:
memory estimate: 1.02 KiB
allocs estimate: 10
--------------
minimum time: 1.585 μs (0.00% GC)
median time: 1.678 μs (0.00% GC)
mean time: 2.519 μs (27.10% GC)
maximum time: 5.206 ms (99.88% GC)
--------------
samples: 10000
evals/sample: 10
import CSV
BenchmarkTools.@benchmark deepcopy(GameState([0,0],[0,0],[0,0],-1))
BenchmarkTools.Trial:
memory estimate: 1.02 KiB
allocs estimate: 10
--------------
minimum time: 6.709 μs (0.00% GC)
median time: 7.264 μs (0.00% GC)
mean time: 9.122 μs (18.00% GC)
maximum time: 13.289 ms (99.87% GC)
--------------
samples: 10000
evals/sample: 5
UPDATE: Code for suggested solution
import Base:deepcopy
function deepcopy(x::GameState)
return GameState([x.gameScore[1], x.gameScore[2]], [x.setScore[1], x.setScore[2]], [x.matchScore[1], x.matchScore[2]],x.serve)
end
BenchmarkTools.@benchmark deepcopy(GameState([0,0],[0,0],[0,0],-1))
BenchmarkTools.Trial:
memory estimate: 672 bytes
allocs estimate: 8
--------------
minimum time: 184.436 ns (0.00% GC)
median time: 199.305 ns (0.00% GC)
mean time: 256.366 ns (21.29% GC)
maximum time: 102.345 μs (99.52% GC)
--------------
samples: 10000
evals/sample: 656
There are at least two possibilities:
I would place my bets on the former. Use ProfileView.jl to easily detect run-time dispatch graphically. If you see a lot of red bars on top when you profile rungame
, then you know you've found the source of the problems. Aside from the interaction with CSV, eliminating those red bars might give you huge improvements in performance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With