If `import Collections` and only use `Deque`, will my final binary only include what I use, or is it bloated with extra thing I don't use?

Should I instead

import DequeModule

and import other things when I use those?

Swift can do dead code elimination but it can be hit-and-miss. Itā€™s better to import specifically and only what you use.

1 Like

As @lukasa said, Swift tries to eliminate what you havenā€™t touched, at least in release builds. So in general the difference is more significant in the manifest where you select which products you need. If you depend on products you arenā€™t going to use, you can end up with more transitive dependency version constraints than really matter, and that makes it more likely to run into constraints that are mutually exclusive some other package. You also waste time during development each time you fetch or build the unneeded dependencies.

For quick experiments or beginnerā€level examples you provide in documentation, it tends to be better to depend and import broadly simply because it is less work to get something up and running. But on real projects it is almost always better to depend and import narrowly for the reasons mentioned above.

1 Like

What happens if you import specifically the Deque struct?

import struct Collections.Deque

It makes no difference:

3 Likes