mohae / deepcopy

Deep copy things
MIT License
569 stars 122 forks source link

do not copy slice cap #14

Closed thinxer closed 5 years ago

thinxer commented 6 years ago

Copying slice cap is not necessary and will cause extra memory usages if there are multiple slices derived from the same underlying array.

dolmen commented 5 years ago

How do you know that methods of the type doesn't rely on capacity being higher than length? The only reliable copy is to strictly reproduce the original value.

Copying without replicating capacity would be a major breaking change. I recommend instead to fork the project if you need this feature.

thinxer commented 5 years ago

How do we "strictly reproduce" this? https://play.golang.org/p/hksiOMfByGT

Deep copying the exact arrangement of the slices and underlying arrays is not possible in Go. I'd suggest using the least surprise principle: copy only the face value of slices.

dolmen commented 5 years ago

I agree that your example (adapted now that play.golang.org supports public packages) is not handled by deepcopy.

But I insist that your proposed change is not a light change that just fixes things. It would break the current behavior for users that require the capacity to be preserved. Preserving or not preserving slice capacity are two different copy strategies that deserve two different implementations.