Skip to content

Manual Copy-on-Write (COW) implementation in Swift using struct + class pattern to preserve value semantics with performance optimization.

Notifications You must be signed in to change notification settings

Lainaaa/Custom-Copy-on-Write

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Custom Copy-on-Write (COW) in Swift

This project demonstrates a manual implementation of Copy-on-Write (COW) behavior in Swift using a combination of struct and an internal final class. This approach allows maintaining value semantics while optimizing performance by avoiding unnecessary copies.

πŸ” Problem

By default, value types in Swift (like struct) are copied on every mutation. This can be inefficient if the internal data is large or expensive to copy. COW helps avoid these copies when it's safe to reuse the existing storage.

🧠 How It Works

  • Car is a value type (struct) that wraps a reference to a private CarStorage class.
  • The CarStorage class holds the actual data (model, year, topSpeed).
  • Before mutation, the struct checks whether it uniquely owns the storage using isKnownUniquelyReferenced.
  • If not, it makes a copy of the storage β€” classic COW pattern.

βœ… Example

var bmw = Car(model: "BMW", year: 2020, topSpeed: 250)
let bmw2 = bmw

print(bmw.compare(with: bmw2)) // true

bmw.year = 2021

print(bmw.compare(with: bmw2)) // false

About

Manual Copy-on-Write (COW) implementation in Swift using struct + class pattern to preserve value semantics with performance optimization.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages