# Possible performance improvements for \`deepcopy\`?

**URL:** <https://discourse.julialang.org/t/possible-performance-improvements-for-deepcopy/107719>\
**Category:** Internals & Design\
**Tags:** performance, deepcopy\
**Created:** [December 16, 2023, 11:13pm UTC](https://discourse.julialang.org/t/possible-performance-improvements-for-deepcopy/107719 "2023-12-16T23:13:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tortar](https://avatars.discourse-cdn.com/v4/letter/t/6bbea6/32.png) [@Tortar](https://discourse.julialang.org/u/Tortar)\
**Post date:** [December 16, 2023, 11:13pm UTC](https://discourse.julialang.org/t/possible-performance-improvements-for-deepcopy/107719/1 "2023-12-16T23:13:38Z")

</div>

I created this small package here [GitHub - Tortar/FastDeepCopy.jl](https://github.com/Tortar/FastDeepCopy.jl) which outperform the current `deepcopy` implementation by quite a bit, for a vector of mutable structs it is more than 200 times faster as you can see in the ReadMe of the repository. The implementation is quite small and simple: [FastDeepCopy.jl/src/FastDeepCopy.jl at main · Tortar/FastDeepCopy.jl · GitHub](https://github.com/Tortar/FastDeepCopy.jl/blob/main/src/FastDeepCopy.jl). I’d like to ask to someone more knowledgeable on internals if my code relies on some assumptions which can’t be met in general (I didn’t find any by myself) and so it is not good for the standard library, or if instead a similar implementation could be added to it?

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [December 16, 2023, 11:37pm UTC](https://discourse.julialang.org/t/possible-performance-improvements-for-deepcopy/107719/2 "2023-12-16T23:37:34Z")

</div>

Could include `versioninfo()` in the README to better describe the benchmark conditions, particularly important because deepcopy.jl has changed in the last few minor revisions.

---

<div class="post-metadata">

**Author:** ![ericphanson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ericphanson/32/215186_2.png) [@ericphanson](https://discourse.julialang.org/u/ericphanson)\
**Post date:** [December 17, 2023, 12:22am UTC](https://discourse.julialang.org/t/possible-performance-improvements-for-deepcopy/107719/4 "2023-12-17T00:22:34Z")

</div>

I think there could be semantic differences around stuff like preserving identities. For example this works in Base:

```julia
julia> a = [1];

julia> b = [a, a]
2-element Vector{Vector{Int64}}:
[1]
[1]

julia> b[1] === b[2]
true

julia> c = deepcopy(b)
2-element Vector{Vector{Int64}}:
[1]
[1]

julia> c[1] === c[2]
true

```

From a quick look I don’t think your version keeps this property but I haven’t tried.

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [December 17, 2023, 12:42am UTC](https://discourse.julialang.org/t/possible-performance-improvements-for-deepcopy/107719/5 "2023-12-17T00:42:19Z")

</div>

I’m guessing that’s what the `stackdict` is for?

---

<div class="post-metadata">

**Author:** ![Tortar](https://avatars.discourse-cdn.com/v4/letter/t/6bbea6/32.png) [@Tortar](https://discourse.julialang.org/u/Tortar)\
**Post date:** [December 21, 2023, 1:09am UTC](https://discourse.julialang.org/t/possible-performance-improvements-for-deepcopy/107719/6 "2023-12-21T01:09:42Z")

</div>

> I’m guessing that’s what the `stackdict` is for?

yes, that’s what it seems to be achieving.

If you are interested, I found something to improve anyway hopefully :D: [Don't use `deepcopy_internal(xi, stackdict)` for bits fields by Tortar · Pull Request #52597 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/52597)
