# Find unintentional copy of struct

**URL:** <https://forums.swift.org/t/find-unintentional-copy-of-struct/68528>\
**Category:** Using Swift\
**Created:** [November 18, 2023, 9:07am UTC](https://forums.swift.org/t/find-unintentional-copy-of-struct/68528 "2023-11-18T09:07:49Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![wadetregaskis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/wadetregaskis/32/8494_2.png) [@wadetregaskis](https://forums.swift.org/u/wadetregaskis)\
**Post date:** [November 19, 2023, 5:55am UTC](https://forums.swift.org/t/find-unintentional-copy-of-struct/68528/6 "2023-11-19T05:55:34Z")

</div>

> [@nocchijiang](#):
>
> There are ~1K people working on our codebase and it is hard to teach all the developers to write copy-free code, so we want an automatic solution, like compiler warnings, to keep the code clean reasonably.

I get where you're coming from, but it'll be hard to get help from the compiler on this. Not every copy is unnecessary nor a performance problem, and the compiler doesn't really have great insight into which cases will be acceptable and which won't (though maybe when PGO is in use, it could).

It'll probably be more viable - and to the point of what you care about, performance - to focus on profiling and benchmark unit tests. That way you can ignore any copies that don't actually matter, and you'll also catch performance problems due to more than just copies. You can make performance testing part of your development process - e.g. part of your CI pipeline - and even block integration of patches that introduce regressions.

> [@nocchijiang](#):
>
> I think the copying problem exists no matter if it is a release build. The compiler behavior should be consistent (a copyable `struct` should always be copied when being passed around under any optimization levels).

I don't think that's accurate (nor a desirable constraint). Swift is a pretty high-level language that nonetheless aspires to be very performant, and that means it has to do some pretty advanced optimisations, such as eliminating entire instantiations of structs let-alone mere copies (e.g. to make lazy `Collection`s and `Sequence`s fast, you can't _actually_ allocate all the intermediary structures nor actually have them chain calls through each other for every element you fetch - it all gets completely eliminated by the optimiser until you're left with a trivial loop and the essential operations, potentially just a few machine instructions).

> [@nocchijiang](#):
>
> From my testing done on Swift 5.9, the functionality of `~Copyable` is extremely limited. It does not play with protocol and generics at all.

It's true, non-copyable structs currently don't play well with those features. [There's work underway to rectify that](https://forums.swift.org/t/pitch-noncopyable-generics/68180), although it's probably still some way off from landing in a Swift release.

---

_[View the full topic](https://forums.swift.org/t/find-unintentional-copy-of-struct/68528)._
