You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: proposals/0456-stdlib-span-properties.md
+29-29Lines changed: 29 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -46,9 +46,9 @@ func function() {
46
46
```
47
47
If we were to attempt using `b` again after the call to `modify(&a)`, the compiler would report an overlapping access error, due to attempting to mutate `a` (with `modify(&a)`) while it is already being accessed through `b`'s borrow. Note that the copyability of `B` means that it cannot represent a mutation of `A`; it therefore represents a non-exclusive borrowing relationship.
48
48
49
-
Given this, we propose to enable the definition of a borrowing relationship via a computed property. With this feature we then propose to add `storage` computed properties to standard library types that can share their internal typed storage, as well as `bytes` computed properties to those standard library types that can safely share their internal storage as untyped memory.
49
+
Given this, we propose to enable the definition of a borrowing relationship via a computed property. With this feature we then propose to add `span` computed properties to standard library types that can share access to their internal typed memory. When a `span` has `BitwiseCopyable` elements, it will have a `bytes` computed property to share a view of the memory it represents as untyped memory.
50
50
51
-
One of the purposes of `Span` is to provide a safer alternative to `UnsafeBufferPointer`. This proposal builds on it and allows us to rewrite code reliant on `withUnsafeBufferPointer()` to use `storage` properties instead. Eventually, code that requires access to contiguous memory can be rewritten to use `Span`, gaining better composability in the process. For example:
51
+
One of the purposes of `Span` is to provide a safer alternative to `UnsafeBufferPointer`. This proposal builds on it and allows us to rewrite code reliant on `withUnsafeBufferPointer()` to use `span` properties instead. Eventually, code that requires access to contiguous memory can be rewritten to use `Span`, gaining better composability in the process. For example:
52
52
53
53
```swift
54
54
let result =try myArray.withUnsafeBufferPointer { buffer in
@@ -63,7 +63,7 @@ let result = try myArray.withUnsafeBufferPointer { buffer in
63
63
This closure-based call is difficult to evolve, such as making `result` have a non-copyable type, adding a concurrent task, or adding typed throws. An alternative based on a vended `Span` property would look like this:
64
64
65
65
```swift
66
-
let span = myArray.storage
66
+
let span = myArray.span
67
67
let indices =findElements(span)
68
68
var myResult =MyResult()
69
69
for i in indices {
@@ -87,47 +87,47 @@ By allowing the language to define lifetime dependencies in these limited ways,
87
87
88
88
#### <aname="extensions"></a>Extensions to Standard Library types
89
89
90
-
The standard library and Foundation will provide `storage` computed properties, returning lifetime-dependent `Span` instances. These computed properties are the safe and composable replacements for the existing `withUnsafeBufferPointer` closure-taking functions.
90
+
The standard library and Foundation will provide `span` computed properties, returning lifetime-dependent `Span` instances. These computed properties are the safe and composable replacements for the existing `withUnsafeBufferPointer` closure-taking functions.
91
91
92
92
```swift
93
93
extensionArray {
94
94
/// Share this `Array`'s elements as a `Span`
95
-
varstorage: Span<Element> { get }
95
+
varspan: Span<Element> { get }
96
96
}
97
97
98
98
extensionArraySlice {
99
99
/// Share this `Array`'s elements as a `Span`
100
-
varstorage: Span<Element> { get }
100
+
varspan: Span<Element> { get }
101
101
}
102
102
103
103
extensionContiguousArray {
104
104
/// Share this `Array`'s elements as a `Span`
105
-
varstorage: Span<Element> { get }
105
+
varspan: Span<Element> { get }
106
106
}
107
107
108
108
extensionString.UTF8View {
109
109
/// Share this `UTF8View`'s code units as a `Span`
110
-
varstorage: Span<Unicode.UTF8.CodeUnit> { get }
110
+
varspan: Span<Unicode.UTF8.CodeUnit> { get }
111
111
}
112
112
113
113
extensionSubstring.UTF8View {
114
114
/// Share this `UTF8View`'s code units as a `Span`
/// Share this `Collection`'s elements as a `Span`
130
-
varstorage: Span<(Key, Value)> { get }
130
+
varspan: Span<(Key, Value)> { get }
131
131
}
132
132
```
133
133
@@ -136,13 +136,13 @@ Conditionally to the acceptance of [`Vector`][SE-0453], we will also add the fol
136
136
```swift
137
137
extensionVectorwhereElement:~Copyable {
138
138
/// Share this vector's elements as a `Span`
139
-
varstorage: Span<Element> { get }
139
+
varspan: Span<Element> { get }
140
140
}
141
141
```
142
142
143
143
#### Accessing the raw bytes of a `Span`
144
144
145
-
When a `Span`'s element is `BitwiseCopyable`, we allow viewing the underlying storage as raw bytes with `RawSpan`:
145
+
When a `Span`'s element is `BitwiseCopyable`, we allow viewing the underlying memory as raw bytes with `RawSpan`:
146
146
147
147
```swift
148
148
extensionSpanwhereElement:BitwiseCopyable {
@@ -160,12 +160,12 @@ We hope that `Span` and `RawSpan` will become the standard ways to access shared
160
160
```swift
161
161
extensionUnsafeBufferPointer {
162
162
/// Unsafely view this buffer as a `Span`
163
-
varstorage: Span<Element> { get }
163
+
varspan: Span<Element> { get }
164
164
}
165
165
166
166
extensionUnsafeMutableBufferPointer {
167
167
/// Unsafely view this buffer as a `Span`
168
-
varstorage: Span<Element> { get }
168
+
varspan: Span<Element> { get }
169
169
}
170
170
171
171
extensionUnsafeRawBufferPointer {
@@ -193,22 +193,22 @@ While the `swift-foundation` package and the `Foundation` framework are not gove
193
193
```swift
194
194
extensionFoundation.Data {
195
195
// Share this `Data`'s bytes as a `Span`
196
-
varstorage: Span<UInt8> { get }
196
+
varspan: Span<UInt8> { get }
197
197
198
198
// Share this `Data`'s bytes as a `RawSpan`
199
199
var bytes: RawSpan { get }
200
200
}
201
201
```
202
202
203
-
Unlike with the standard library types, we plan to have a `bytes` property on `Foundation.Data` directly. This type conceptually consists of untyped bytes, and `bytes` is likely to be the primary way to directly access its memory. As `Data`'s API presents its storage as a collection of `UInt8` elements, we provide both `bytes` and `storage`. Types similar to `Data` may choose to provide both typed and untyped `Span` properties.
203
+
Unlike with the standard library types, we plan to have a `bytes` property on `Foundation.Data` directly. This type conceptually consists of untyped bytes, and `bytes` is likely to be the primary way to directly access its memory. As `Data`'s API presents its storage as a collection of `UInt8` elements, we provide both `bytes` and `span`. Types similar to `Data` may choose to provide both typed and untyped `Span` properties.
204
204
205
205
#### <aname="performance"></a>Performance
206
206
207
-
The `storage` and `bytes` properties should be performant and return their `Span` or `RawSpan` with very little work, in O(1) time. This is the case for all native standard library types. There is a performance wrinkle for bridged `Array` and `String` instances on Darwin-based platforms, where they can be bridged to Objective-C types that do not guarantee contiguous storage. In such cases the implementation will eagerly copy the underlying data to the native Swift form, and return a `Span` or `RawSpan` pointing to that copy.
207
+
The `span` and `bytes` properties should be performant and return their `Span` or `RawSpan` with very little work, in O(1) time. This is the case for all native standard library types. There is a performance wrinkle for bridged `Array` and `String` instances on Darwin-based platforms, where they can be bridged to Objective-C types that may not be represented in contiguous memory. In such cases the implementation will eagerly copy the underlying data to the native Swift form, and return a `Span` or `RawSpan` pointing to that copy.
208
208
209
-
This eager copy behaviour will be specific to the `storage` and `bytes` properties, and therefore the memory usage behaviour of existing unchanged code will remain the same. New code that adopts the `storage` and `bytes` properties will occasionally have higher memory usage due to the eager copies, but we believe this performance compromise is the right approach for the standard library. The alternative is to compromise the design for all platforms supported by Swift, and we consider that a non-starter.
209
+
This eager copy behaviour will be specific to the `span` and `bytes` properties, and therefore the memory usage behaviour of existing unchanged code will remain the same. New code that adopts the `span` and `bytes` properties will occasionally have higher memory usage due to the eager copies, but we believe this performance compromise is the right approach for the standard library. The alternative is to compromise the design for all platforms supported by Swift, and we consider that a non-starter.
210
210
211
-
As a result of the eager copy behaviour for bridged `String.UTF8View` and `Array` instances, the `storage` property for these types will have a documented performance characteristic of "amortized constant time performance."
211
+
As a result of the eager copy behaviour for bridged `String.UTF8View` and `Array` instances, the `span` property for these types will have a documented performance characteristic of "amortized constant time performance."
212
212
213
213
## Source compatibility
214
214
@@ -226,10 +226,10 @@ The additions described in this proposal require a version of the Swift standard
226
226
227
227
#### Adding `withSpan()` and `withBytes()` closure-taking functions
228
228
229
-
The `storage` and `bytes` properties aim to be safe replacements for the `withUnsafeBufferPointer()` and `withUnsafeBytes()` closure-taking functions. We could consider `withSpan()` and `withBytes()` closure-taking functions that would provide an quicker migration away from the older unsafe functions. We do not believe the closure-taking functions are desirable in the long run. In the short run, there may be a desire to clearly mark the scope where a `Span` instance is used. The default method would be to explicitly consume a `Span` instance:
229
+
The `span` and `bytes` properties aim to be safe replacements for the `withUnsafeBufferPointer()` and `withUnsafeBytes()` closure-taking functions. We could consider `withSpan()` and `withBytes()` closure-taking functions that would provide an quicker migration away from the older unsafe functions. We do not believe the closure-taking functions are desirable in the long run. In the short run, there may be a desire to clearly mark the scope where a `Span` instance is used. The default method would be to explicitly consume a `Span` instance:
230
230
```swift
231
231
var a =ContiguousArray(0..<8)
232
-
var span = a.storage
232
+
var span = a.span
233
233
read(span)
234
234
_=consume span
235
235
a.append(8)
@@ -239,7 +239,7 @@ In order to visually distinguish this lifetime, we could simply use a `do` block
239
239
```swift
240
240
var a =ContiguousArray(0..<8)
241
241
do {
242
-
let span = a.storage
242
+
let span = a.span
243
243
read(span)
244
244
}
245
245
a.append(8)
@@ -248,7 +248,7 @@ a.append(8)
248
248
A more targeted solution may be a consuming function that takes a non-escaping closure:
249
249
```swift
250
250
var a =ContiguousArray(0..<8)
251
-
var span = a.storage
251
+
var span = a.span
252
252
consuming(span) { span in
253
253
read(span)
254
254
}
@@ -257,9 +257,9 @@ a.append(8)
257
257
258
258
During the evolution of Swift, we have learned that closure-based API are difficult to compose, especially with one another. They can also require alterations to support new language features. For example, the generalization of closure-taking API for non-copyable values as well as typed throws is ongoing; adding more closure-taking API may make future feature evolution more labor-intensive. By instead relying on returned values, whether from computed properties or functions, we build for greater composability. Use cases where this approach falls short should be reported as enhancement requests or bugs.
259
259
260
-
#### Giving the properties different names
260
+
#### Different naming for the properties
261
261
262
-
We chose the names`storage`and `bytes` because those reflect _what_ they represent. Another option would be to name the properties after _how_ they represent what they do, which would be `span` and `rawSpan`. It is possible the name `storage`would be deemed to clash too much with existing properties of types that would like to provide views of their internal storage with `Span`-providing properties. For example, the Standard Library's concrete `SIMD`-conforming types have a property `var _storage`. The current proposal means that making this property of `SIMD` types into public API would entail a name change more significant than simply removing its leading underscore.
262
+
We originally proposed the name`storage`for the `span` properties introduced here. That name seems to imply that the returned `Span` is the storage itself, rather than a view of the storage. That would be misleading for types that own their storage, especially those that delegate their storage to another type, such as a `ContiguousArray`. In such cases, it would make sense to have a `storage`property whose type is the type that implements the storage.
263
263
264
264
#### Disallowing the definition of non-escapable properties of non-escapable types
265
265
@@ -269,15 +269,15 @@ The original version of this pitch disallowed this. As a consequence, the `bytes
269
269
270
270
#### Omitting extensions to `UnsafeBufferPointer` and related types
271
271
272
-
We could omit the extensions to `UnsafeBufferPointer` and related types, and rely instead of future `Span` and `RawSpan` initializers. The initializers can have the advantage of being able to communicate semantics (somewhat) through their parameter labels. However, they also have a very different shape than the `storage` computed properties we are proposing. We believe that the adding the same API on both safe and unsafe types is advantageous, even if the preconditions for the properties cannot be statically enforced.
272
+
We could omit the extensions to `UnsafeBufferPointer` and related types, and rely instead of future `Span` and `RawSpan` initializers. The initializers can have the advantage of being able to communicate semantics (somewhat) through their parameter labels. However, they also have a very different shape than the `span` computed properties we are proposing. We believe that the adding the same API on both safe and unsafe types is advantageous, even if the preconditions for the properties cannot be statically enforced.
273
273
274
274
## <aname="directions"></a>Future directions
275
275
276
276
Note: The future directions stated in [SE-0447](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0447-span-access-shared-contiguous-storage.md#Directions) apply here as well.
277
277
278
278
#### <aname="MutableSpan"></a>Safe mutations with `MutableSpan<T>`
279
279
280
-
Some data structures can delegate mutations of their owned memory. In the standard library the function `withMutableBufferPointer()` provides this functionality in an unsafe manner. We expect to add a `MutableSpan` type to support delegating mutations of initialized memory. Standard library types will then add a way to vend `MutableSpan` instances. This could be with a closure-taking `withMutableSpan()` function, or a new property, such as `var mutableStorage`. Note that a computed property providing mutable access needs to have a different name than the `storage` properties proposed here, because we cannot overload the return type of computed properties based on whether mutation is desired.
280
+
Some data structures can delegate mutations of their owned memory. In the standard library the function `withMutableBufferPointer()` provides this functionality in an unsafe manner. We expect to add a `MutableSpan` type to support delegating mutations of initialized memory. Standard library types will then add a way to vend `MutableSpan` instances. This could be with a closure-taking `withMutableSpan()` function, or a new property, such as `var mutableStorage`. Note that a computed property providing mutable access needs to have a different name than the `span` properties proposed here, because we cannot overload the return type of computed properties based on whether mutation is desired.
0 commit comments