Movatterモバイル変換


[0]ホーム

URL:


Friday,January8,2016

Exploring Swift Array’s Implementation

Ankit Aggarwal:

Swift declares a C struct in its SwiftShim’s module inside GlobalObject.h called_SwiftArrayBodyStorage

[…]

  • count is the number of elements currently stored in the buffer
  • _capacityAndFlags is used to store two things:

    • first is the capacity of the buffer
    • second “Is the Element type bitwise-compatible with some Objective-C class?”

ViaDavid Smith:

Apparently C-like performance, memory safety, resizing, copy-on-write, and ObjC bridging all together is hard

Thesubscript implementation in_ContiguousArrayBuffer has an interesting note:

Manually swap because it makes the ARC optimizer happy.

The method is also marked with amysteriousnonmutatingkeyword, which the Swift language guide mentions but does not explain. This could be an optimization to avoid unnecessary writebacks or a way to make the compiler enforce that the setter does not actually change any properties of the structure (just the contents of the memory they point to).

Update (2016-05-03):Jasdev Singh:

Stepping through Sidney’s example, we can see hownonmutating signals that a setter doesn’t modify the containing instance, but instead has global side effects.

2 CommentsRSS ·Twitter


Michael Tsai - Blog - Exploring Swift Dictionary’s Implementation
January 23, 2016 11:36 AM

[…] Exploring Swift Array’s Implementation, Exposing […]


Michael Tsai - Blog - Working Around a Swift “nonmutating” Crash
October 12, 2018 3:47 PM

[…] Previously: Exploring Swift Array’s Implementation. […]

Leave a Comment

  

Black Friday
Blog
Archives
Tag Cloud
Top Posts

Recently Updated
RSS Feed ·Comments
Mastodon ·Twitter
Apple News
Trackback

Support this site viaPatreon.

Try my Mac apps:

DropDMGDropDMGDropDMG
EagleFilerEagleFilerEagleFiler
SpamSieveSpamSieveSpamSieve
ToothFairyToothFairyToothFairy
Copyright © 2000–2025Michael Tsai.

[8]ページ先頭

©2009-2025 Movatter.jp