{"id":3988,"date":"2021-12-28T14:11:05","date_gmt":"2021-12-28T06:11:05","guid":{"rendered":"https:\/\/1989lifejourney.com\/?p=3988"},"modified":"2021-12-28T14:11:05","modified_gmt":"2021-12-28T06:11:05","slug":"understanding-swift-copy-on-write-mechanisms","status":"publish","type":"post","link":"https:\/\/1989lifejourney.com\/?p=3988","title":{"rendered":"Understanding Swift Copy-on-Write mechanisms"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-rich is-provider-embed-handler wp-block-embed-embed-handler\"><div class=\"wp-block-embed__wrapper\">\n<style>.gist table { margin-bottom: 0; }<\/style><div style=\"tab-size: 8\" id=\"gist113830416\" class=\"gist\">\n    <div class=\"gist-file\" translate=\"no\" data-color-mode=\"light\" data-light-theme=\"light\">\n      <div class=\"gist-data\">\n        \n<div class=\"js-gist-file-update-container js-task-list-container\">\n      <div id=\"file-gistfile1-txt\" class=\"file my-2\">\n    \n    <div itemprop=\"text\"\n      class=\"Box-body p-0 blob-wrapper data type-text  \"\n      style=\"overflow: auto\" tabindex=\"0\" role=\"region\"\n      aria-label=\"gistfile1.txt content, created by tgnivekucn on 06:09AM on December 28, 2021.\"\n    >\n\n        \n<div class=\"js-check-hidden-unicode js-blob-code-container blob-code-content\">\n\n  <template class=\"js-file-alert-template\">\n  <div data-view-component=\"true\" class=\"flash flash-warn flash-full d-flex flex-items-center\">\n  <svg aria-hidden=\"true\" data-component=\"Octicon\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n    <span>\n      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.\n      <a class=\"Link--inTextBlock\" href=\"https:\/\/github.co\/hiddenchars\" target=\"_blank\">Learn more about bidirectional Unicode characters<\/a>\n    <\/span>\n\n\n  <div data-view-component=\"true\" class=\"flash-action\">        <a href=\"{{ revealButtonHref }}\" data-view-component=\"true\" class=\"btn-sm btn\">    Show hidden characters\n<\/a>\n<\/div>\n<\/div><\/template>\n<template class=\"js-line-alert-template\">\n  <span aria-label=\"This line has hidden Unicode characters\" data-view-component=\"true\" class=\"line-alert tooltipped tooltipped-e\">\n    <svg aria-hidden=\"true\" data-component=\"Octicon\" height=\"16\" viewBox=\"0 0 16 16\" version=\"1.1\" width=\"16\" data-view-component=\"true\" class=\"octicon octicon-alert\">\n    <path d=\"M6.457 1.047c.659-1.234 2.427-1.234 3.086 0l6.082 11.378A1.75 1.75 0 0 1 14.082 15H1.918a1.75 1.75 0 0 1-1.543-2.575Zm1.763.707a.25.25 0 0 0-.44 0L1.698 13.132a.25.25 0 0 0 .22.368h12.164a.25.25 0 0 0 .22-.368Zm.53 3.996v2.5a.75.75 0 0 1-1.5 0v-2.5a.75.75 0 0 1 1.5 0ZM9 11a1 1 0 1 1-2 0 1 1 0 0 1 2 0Z\"><\/path>\n<\/svg>\n<\/span><\/template>\n\n  <table data-hpc class=\"highlight tab-size js-file-line-container\" data-tab-size=\"4\" data-paste-markdown-skip data-tagsearch-path=\"gistfile1.txt\">\n        <tr>\n          <td id=\"file-gistfile1-txt-L1\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"1\"><\/td>\n          <td id=\"file-gistfile1-txt-LC1\" class=\"blob-code blob-code-inner js-file-line\">final class Ref&lt;T&gt; {<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-gistfile1-txt-L2\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"2\"><\/td>\n          <td id=\"file-gistfile1-txt-LC2\" class=\"blob-code blob-code-inner js-file-line\">  var val: T<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-gistfile1-txt-L3\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"3\"><\/td>\n          <td id=\"file-gistfile1-txt-LC3\" class=\"blob-code blob-code-inner js-file-line\">  init(_ v: T) {val = v}<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-gistfile1-txt-L4\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"4\"><\/td>\n          <td id=\"file-gistfile1-txt-LC4\" class=\"blob-code blob-code-inner js-file-line\">}<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-gistfile1-txt-L5\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"5\"><\/td>\n          <td id=\"file-gistfile1-txt-LC5\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-gistfile1-txt-L6\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"6\"><\/td>\n          <td id=\"file-gistfile1-txt-LC6\" class=\"blob-code blob-code-inner js-file-line\">struct Box&lt;T&gt; {<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-gistfile1-txt-L7\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"7\"><\/td>\n          <td id=\"file-gistfile1-txt-LC7\" class=\"blob-code blob-code-inner js-file-line\">    var ref: Ref&lt;T&gt;<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-gistfile1-txt-L8\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"8\"><\/td>\n          <td id=\"file-gistfile1-txt-LC8\" class=\"blob-code blob-code-inner js-file-line\">    init(_ x: T) { ref = Ref(x) }<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-gistfile1-txt-L9\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"9\"><\/td>\n          <td id=\"file-gistfile1-txt-LC9\" class=\"blob-code blob-code-inner js-file-line\">\n<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-gistfile1-txt-L10\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"10\"><\/td>\n          <td id=\"file-gistfile1-txt-LC10\" class=\"blob-code blob-code-inner js-file-line\">    var value: T {<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-gistfile1-txt-L11\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"11\"><\/td>\n          <td id=\"file-gistfile1-txt-LC11\" class=\"blob-code blob-code-inner js-file-line\">        get { return ref.val }<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-gistfile1-txt-L12\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"12\"><\/td>\n          <td id=\"file-gistfile1-txt-LC12\" class=\"blob-code blob-code-inner js-file-line\">        set {<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-gistfile1-txt-L13\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"13\"><\/td>\n          <td id=\"file-gistfile1-txt-LC13\" class=\"blob-code blob-code-inner js-file-line\">          if !isKnownUniquelyReferenced(&amp;ref) {<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-gistfile1-txt-L14\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"14\"><\/td>\n          <td id=\"file-gistfile1-txt-LC14\" class=\"blob-code blob-code-inner js-file-line\">            ref = Ref(newValue)<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-gistfile1-txt-L15\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"15\"><\/td>\n          <td id=\"file-gistfile1-txt-LC15\" class=\"blob-code blob-code-inner js-file-line\">            return<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-gistfile1-txt-L16\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"16\"><\/td>\n          <td id=\"file-gistfile1-txt-LC16\" class=\"blob-code blob-code-inner js-file-line\">          }<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-gistfile1-txt-L17\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"17\"><\/td>\n          <td id=\"file-gistfile1-txt-LC17\" class=\"blob-code blob-code-inner js-file-line\">          ref.val = newValue<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-gistfile1-txt-L18\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"18\"><\/td>\n          <td id=\"file-gistfile1-txt-LC18\" class=\"blob-code blob-code-inner js-file-line\">        }<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-gistfile1-txt-L19\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"19\"><\/td>\n          <td id=\"file-gistfile1-txt-LC19\" class=\"blob-code blob-code-inner js-file-line\">    }<\/td>\n        <\/tr>\n        <tr>\n          <td id=\"file-gistfile1-txt-L20\" class=\"blob-num js-line-number js-blob-rnum\" data-line-number=\"20\"><\/td>\n          <td id=\"file-gistfile1-txt-LC20\" class=\"blob-code blob-code-inner js-file-line\">}<\/td>\n        <\/tr>\n  <\/table>\n<\/div>\n\n\n    <\/div>\n\n  <\/div>\n\n<\/div>\n\n      <\/div>\n      <div class=\"gist-meta\">\n        <a href=\"https:\/\/gist.github.com\/tgnivekucn\/7085e6009baa3217d636107c5a4b0e97\/raw\/0aa570b041bea86e31df55104c285057106ed09b\/gistfile1.txt\" style=\"float:right\" class=\"Link--inTextBlock\">view raw<\/a>\n        <a href=\"https:\/\/gist.github.com\/tgnivekucn\/7085e6009baa3217d636107c5a4b0e97#file-gistfile1-txt\" class=\"Link--inTextBlock\">\n          gistfile1.txt\n        <\/a>\n        hosted with &#10084; by <a class=\"Link--inTextBlock\" href=\"https:\/\/github.com\">GitHub<\/a>\n      <\/div>\n    <\/div>\n<\/div>\n\n<\/div><figcaption><strong><a href=\"https:\/\/github.com\/apple\/swift\/blob\/main\/docs\/OptimizationTips.rst\">Sample code from official github repo<\/a><\/strong><\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/medium.com\/@lucianoalmeida1\/understanding-swift-copy-on-write-mechanisms-52ac31d68f2f\">https:\/\/medium.com\/@lucianoalmeida1\/understanding-swift-copy-on-write-mechanisms-52ac31d68f2f<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p><a href=\"https:\/\/medium.com\/@lucianoalmeida1\/understanding-swift\" rel=\"nofollow\">https:\/\/medium.com\/@lucianoalmeida1\/understanding-swift<\/a> [&hellip;]<\/p>\n","protected":false},"author":139426785,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","_postgate_lock_override":"inherit","_postgate_lock_mode":"inherit","_postgate_preview_mode":"inherit","_postgate_preview_limit":0,"_postgate_product_ids":[],"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_seo_schema_type":"","jetpack_seo_llms_txt_always_include":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_wpcom_ai_launchpad_first_post":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"{title}\n\n{excerpt}\n\n{url}","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"_wpas_customize_per_network":false,"jetpack_post_was_ever_published":false},"categories":[37324612,1],"tags":[],"post_folder":[],"class_list":["post-3988","post","type-post","status-publish","format-standard","hentry","category-ios-development","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/paR5vw-12k","jetpack-related-posts":[{"id":9022,"url":"https:\/\/1989lifejourney.com\/?p=9022","url_meta":{"origin":3988,"position":0},"title":"IOS Swift Opaque type","author":"Kevin","date":"7 11 \u6708, 2023","format":false,"excerpt":"Prerequisite The Problem That Opaque Types Solve O\u2026","rel":"","context":"\u5728\u300cIOS Development\u300d\u4e2d","block_context":{"text":"IOS Development","link":"https:\/\/1989lifejourney.com\/?cat=37324612"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":4982,"url":"https:\/\/1989lifejourney.com\/?p=4982","url_meta":{"origin":3988,"position":1},"title":"Difference between value type and a reference type in iOS swift\ufffc","author":"Kevin","date":"29 3 \u6708, 2022","format":false,"excerpt":"\u4ee5\u4e0b\u8cc7\u6599\u7686\u53c3\u8003\u81eaRef1\uff0c\u9019\u88e1\u50c5\u4f5c\u500b\u4eba\u5b78\u7fd2\u6574\u7406: Definition of Value type \u2026","rel":"","context":"\u5728\u300cIOS Development\u300d\u4e2d","block_context":{"text":"IOS Development","link":"https:\/\/1989lifejourney.com\/?cat=37324612"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":5915,"url":"https:\/\/1989lifejourney.com\/?p=5915","url_meta":{"origin":3988,"position":2},"title":"Write stream to file by Swift","author":"Kevin","date":"17 10 \u6708, 2022","format":false,"excerpt":"Original version in objective-c: https:\/\/gist.gith\u2026","rel":"","context":"\u5728\u300cIOS Development\u300d\u4e2d","block_context":{"text":"IOS Development","link":"https:\/\/1989lifejourney.com\/?cat=37324612"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":4009,"url":"https:\/\/1989lifejourney.com\/?p=4009","url_meta":{"origin":3988,"position":3},"title":"Static vs Dynamic Dispatch in Swift","author":"Kevin","date":"6 1 \u6708, 2022","format":false,"excerpt":"\u4ee5\u4e0b\u5167\u5bb9\u53c3\u8003\u81ea[Ref]\uff0c\u9019\u88e1\u50c5\u4f5c\u500b\u4eba\u6458\u8981\u6574\u7406: Method Dispatch\u5b9a\u7fa9: Method\u2026","rel":"","context":"\u5728\u300cIOS Development\u300d\u4e2d","block_context":{"text":"IOS Development","link":"https:\/\/1989lifejourney.com\/?cat=37324612"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":12189,"url":"https:\/\/1989lifejourney.com\/?p=12189","url_meta":{"origin":3988,"position":4},"title":"App vs AI agent","author":"Kevin","date":"19 7 \u6708, 2025","format":false,"excerpt":"The first thing I did this morning was argue with \u2026","rel":"","context":"\u5728\u300cAI related\u300d\u4e2d","block_context":{"text":"AI related","link":"https:\/\/1989lifejourney.com\/?cat=763194612"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3895,"url":"https:\/\/1989lifejourney.com\/?p=3895","url_meta":{"origin":3988,"position":5},"title":"Functional Reactive Programming","author":"Kevin","date":"2 12 \u6708, 2021","format":false,"excerpt":"What is functional reactive programming (FRP)?FRP \u2026","rel":"","context":"\u5728\u300cIOS Development\u300d\u4e2d","block_context":{"text":"IOS Development","link":"https:\/\/1989lifejourney.com\/?cat=37324612"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=\/wp\/v2\/posts\/3988","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=\/wp\/v2\/users\/139426785"}],"replies":[{"embeddable":true,"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3988"}],"version-history":[{"count":4,"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=\/wp\/v2\/posts\/3988\/revisions"}],"predecessor-version":[{"id":3992,"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=\/wp\/v2\/posts\/3988\/revisions\/3992"}],"wp:attachment":[{"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3988"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3988"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3988"},{"taxonomy":"post_folder","embeddable":true,"href":"https:\/\/1989lifejourney.com\/index.php?rest_route=%2Fwp%2Fv2%2Fpost_folder&post=3988"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}