Copy-on-Write
💡 Collection의 Copy -On-Write
예를 들어 변수 A에 들어있는 Collection값을 새로운 변수 B에 복사한다고 할 때 바로 복사본을 만들지 않습니다.
Reference Type 처럼 B는 A의 참조만 공유합니다. 만약 B의 값에 변경이 발생되면 그 시점에서
A에 대한 새로운 복사본을 만들어 B에게 주고 값을 변경합니다.
Copy-On-Write는 Reference type의 효율성과 Value type의 불변성을 이용하는 것 입니다.
할당은 많이 일어나지만 변경은 그것보다 적게 일어나 복사의 비용을 줄일 수 있고 변경이 일어나면
기존의 값을 복사하여 새로운 값을 만든 뒤 변경하므로 기존의 값도 변하지 않는 불변성을 유지 할 수 있습니다.
// 예시
var abouSwift: String = """
Swift is a fantastic way to write software, whether it’s for phones, desktops, servers,
or anything else that runs code. It’s a safe, fast, and interactive programming
language that combines the best in modern language thinking with wisdom
from the wider Apple engineering culture and the diverse contributions
from its open-source community. The compiler is optimized for performance
and the language is optimized for development, without compromising on either.
Swift is friendly to new programmers.
It’s an industrial-quality programming language
that’s as expressive and enjoyable as a scripting language.
Writing Swift code in a playground lets you experiment
with code and see the results immediately,
without the overhead of building and running an app.
"""
var str: String = aboutSwift
str += "BoostCamp is the Best !! "
아래 참고 자료를 확인해보면 더욱 잘 이해 할 수 있다.
스위프트 타입별 메모리 분석 실험
struct와 class 가 메모리 영역을 어디를 사용하는지 분석한 실험 결과
medium.com
이걸 그림으로 그려보면 다음과 같을 것이다.
'iOS > Swift' 카테고리의 다른 글
[Swift] 정규표현식 (1) | 2024.01.12 |
---|---|
[Swift] Swift에서의 메모리 관리 (0) | 2023.12.28 |
[Swift] Swift에서의 Value Type, Reference Type (0) | 2023.12.28 |
[Swift] Swift Package 만들기 (0) | 2023.12.12 |
[Swift] Optional(옵셔널) (0) | 2023.06.11 |