You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"copy on write" is an optimization technique that helps in managing memory more efficiently.
It is used primarly with value types such as Array, Dictionary and String to defer the actual copying of data until it is modified. This helps in minimizing unnecessary data copying and thus improve performance.
How "copy on write" works ?
When you assign or pass a value type to another variable or function. Swift doesn't immediately creates the copy of data.
Instead it shares the same underlying storage until the copy is modified.
Note: When you have an array of custom structs, the array itself will benefit from COW.
The memory layout of a type is determined at complie time based on its properties.
Allowing stored properties in extension would complicate this process, as compiler would need to accounts additional properties that might be added in extension and recompile the type.
If extension could add stored properties, the compiler needs to dynamically adjust the memory layout of a type, which could lead to performance overhead.
2. Initilization Complexity
Initialization logic becomes more complex if stored properties are allowed in extensions. The complier needs to ensure that all the stored properties are properly initialized even those are added in extensions.
3. Definition
Extensions are intended to extend existing types with new functionality rather than altering the fundamental structure.
Observation:
Frameworks provided by Apple like Foundation, UIKit, CoreGraphics etc is precomplied. They are dynamically linked at runtime rather than compiled being compiled with our source code.
Memory layout of a type, including its size (calculated from properties) is determined at compile time.
This means the exact memory structure is known and fixed when code is compiled.
For the layout and size the struct and classes includes all the stored properites while classes also include additional meta data for object management. Enums layout depends upon cases and associated values.