Depending on your performance tolerances – absolutely, you should care. Putting values in the DATA_CONST section can make the difference on the OS choice to jetsam your process when a device is running out of memory or not. Constant DATA can be purged e.g. on iOS when not in use, because it can just be reloaded from memory. Mutable DATA (as well as heap memory) cannot. Does that matter for an app who's use case is mainly running in the foreground? Not hugely. A widget or watch app? More so. A daemon or low-level framework? Definitely. To this end, if you are doing critical performance optimization, it is good to have an understanding of this stuff and what facilities the language provides to control it.
let
signals immutable once initialized, but that initial value can be generated dynamically. That is different from fully constant. In the case of globals, this also includes overhead to ensure that it will be lazily initialized in a thread-safe way. This has a cost. Whether that cost is material or not depends on your performance tolerances, but we design Swift to span low- to high-level use cases. If you don't want to worry about these things, this feature is designed not to require you to use it, but it's there for you if you need it.
It is not just about helping the compiler out – though it is important with optimizing compilers to be able to "lock in" certain optimizations to guarantee performance instead of relying on/expecting the optimizer to do it. Write array.reduce([], +)
and Swift might, with a following wind, optimize away your accidentally quadratic performance, but you absolutely shouldn't rely on it to. Instead you should write array.reduce(into: [], +=)
.
If you have a large amount of data that you want to be sure is initialized at compile time so it can go into DATA_CONST, it would be healthy to be able to annotate it as such. It would be very sad, if you put in lots of effort to ensure such things were happening, if later someone came along and with a seemingly innocent change to some code that accidentally turned all your constant calculations into dynamic ones. Allowing variables to be decorated with @const
in a way where making such changes causes compiler errors avoids that.