How do I implement write-time replication in the swift fabric?

Source: Internet
Author: User

Structural Body ( struct) plays an important role in the swift language , and in the Swift Standard library, about 90% Open types are structs, including our common Array,String,Dictionary. The most important feature of a struct compared to a class is that it is a value type and similar to a reference type. Value types are assigned values by copying values instead of referencing the same memory address, so there is no problem with data sharing, preventing accidental data changes, and it is thread-safe.

To give a very simple example, in OBJC , an array is a class, a reference type, in Swift , an array is a struct and a value type. So in the following code:

Let array1 = Nsmutablearray (array: ["Lihua", "liming"])

Let array2 = array1

array1.addobject ("Xiaowang")

Array2

array1 and array2 eventually became ["Lihua", "Liming", "Xiaowang"] , which is Array1 changes can cause array2 to change as they are two reference types and both refer to the same memory address.

and in in Swift, there is no such problem:

var array3 = ["Lihua", "liming"]

var array4 = array3

array3.append ("Xiaowang")

Array4

After this code executes, array3 becomes ["Lihua", "Liming", "Xiaowang"] , and array4 is ["Lihua", "liming"] . This is the biggest difference between a struct and a class.

So, is it true that every time a struct is assigned to another variable or passed to a function, replication occurs. The answer is no, in the Swift Array,Dictionary, String These types, although they are value types, are optimized in the specific implementation of Swift to avoid unnecessary replication. "Classes and Structures" in thebook "TheSwift programming Language (Swift 2.2)" A chapter at the end writes:

The description above refers to the "copying" of strings, arrays, and dictionaries. The behavior you see in your code would always is as if a copy took place. However, Swift only performs a actual copy behind the scenes when it was absolutely necessary to doing so. Swift manages all value copying to ensure optimal performance, and we should not avoid assignment to try to preempt this Optimization.

in the the optimization used in Swift is called the write-time replication technique, which simply means that replication behavior occurs only when a struct has written behavior. The specific approach is to use a reference type inside the struct to store the actual data, in the ordinary pass without writing operation, is the internal reference of the application Count +1, During a write operation, a copy operation is made to the internal reference to store the new data and prevent the previous reference unexpected data sharing is generated.

in the there is one method in Swift: ISUNIQUELYREFERENCEDNONOBJC (Swift 2.2), in Swift3 This function becomes this:isknownuniquelyreferenced , he can check that an instance of a class is not the only reference, if so, we do not need to copy the struct instance, if not, The description object is shared by a different struct, and it needs to be replicated when it is changed.

but this function is only for The Swift object is useful if you want to use a objective-c object, you can encapsulate the OC object one time with Swift.

The following is the An example of a code that implements the write-time replication technology in Advanced Swift, which I have converted to Swift3 :

Final class Box {

var unbox:a

Init (_ value:a) {

unbox = value

}

}

struct Gaussianblur {

private var boxedfilter:box= {

var filter = cifilter (name: "Cigaussianblur", Withinputparameters: [:])!

Filter.setdefaults ()

return Box (filter)

}()

fileprivate var filter:cifilter {

get {return Boxedfilter.unbox}

set {boxedfilter = Box (newvalue)}

}

private var filterforwriting:cifilter {

mutating get {

if!isknownuniquelyreferenced (&boxedfilter) {

filter = filter.copy () as! Cifilter

}

return Filter

}

}

var inputimage:ciimage {

get {return Filter.value (Forkey:kciinputimagekey) as! Ciimage}

set {filterforwriting.setvalue (NewValue, Forkey:kciinputimagekey)}

}

var radius:double {

get {return Filter.value (Forkey:kciinputradiuskey) as! Double}

set {filterforwriting.setvalue (NewValue, Forkey:kciinputradiuskey)}

}

}

extension Gaussianblur {

var outputimage:ciimage? {

return Filter.outputimage

}

}

Article source: Xiao Wukong

How do I implement write-time replication in the swift fabric?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.