Skip to content

duyquang91/CopyOnWriteSwift

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CopyOnWriteSwift

CocoaPods compatible Carthage compatible Swift Package Manager

Overview

Copy-on-write is a common computing technique that helps boost performance when copying structures. To give you an example, imagine an array with 1000 elements inside it: if you copied that array into another variable, Swift would have to copy all 1000 elements even if the two arrays ended up being the same.

This problem is solved by using copy-on-write: when you point two variables at the instance, they both point to the same underlying data, if you modify the second variable, compiler takes a full copy at that point so that only the second variable is modified - the first isn't changed.

In Swift, only some kinds of value type are implemented under copy-on-write such as Array, Dictionaty, Set, String ... I'm here to bring copy-on-write to support all value types in Swift by using a simple annotation: @CopyOnWrite

import CopyOnWriteSwift

struct Foo {...}

@CopyOnWrite
var foo1 = Foo()        // memory_address: 0x60000006e420

var foo2 = foo1         // memory_address: 0x60000006e420

foo2.mutatingMethod()   // memory_address: 0x6080000a88a0

without @CopyOnWrite

struct Foo {...}

var foo1 = Foo()        // memory_address: 0x60000006e420

var foo2 = foo1         // memory_address: 0x6080000a88a0

This library was inspired by Writing High-Performance Swift Code from Apple/swift. I already created an article on LinkedIn to talk more about this topic, feel free to take a look.

Installation

CocoaPods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

To integrate CopyOnWriteSwift into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '11.0'
use_frameworks!

pod 'CopyOnWriteSwift', '~> 1.0.0'

Then, run the following command:

$ pod install
Carthage

Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate CopyOnWriteSwift into your Xcode project using Carthage, specify it in your Cartfile:

github "Duyquang91/CopyOnWriteSwift" ~> 1.0.0
Swift Package Manager

To use CopyOnWriteSwift as a Swift Package Manager package just add the following in your Package.swift file.

// swift-tools-version:4.2

import PackageDescription

let package = Package(
    name: "HelloCopyOnWriteSwift",
    dependencies: [
        .package(url: "https://github.com/duyquang91/CopyOnWriteSwift.git", .upToNextMajor(from: "1.0.0"))
    ],
    targets: [
        .target(name: "HelloCopyOnWriteSwift", dependencies: ["CopyOnWriteSwift"])
    ]
)

Author

Steve Dao
Senior Software Engineer at NTUC Enterprise Co-operative Limited, Singapore.

License

CopyOnWriteSwift is released under the MIT license. See LICENSE for details.

About header photo

The header photo is a famous place named Cầu Vàng (Golden Bridge) in Da Nang, Vietnam. If you are going to visit Vietnam, don't forget to take a photo there!