Skip to content

Commit e3b0d36

Browse files
committed
[SE-0456] amend proposal to rename properties to span
1 parent 79c7031 commit e3b0d36

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

proposals/0456-stdlib-span-properties.md

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ func function() {
4646
```
4747
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.
4848

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.
5050

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:
5252

5353
```swift
5454
let result = try myArray.withUnsafeBufferPointer { buffer in
@@ -63,7 +63,7 @@ let result = try myArray.withUnsafeBufferPointer { buffer in
6363
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:
6464

6565
```swift
66-
let span = myArray.storage
66+
let span = myArray.span
6767
let indices = findElements(span)
6868
var myResult = MyResult()
6969
for i in indices {
@@ -87,47 +87,47 @@ By allowing the language to define lifetime dependencies in these limited ways,
8787

8888
#### <a name="extensions"></a>Extensions to Standard Library types
8989

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.
9191

9292
```swift
9393
extension Array {
9494
/// Share this `Array`'s elements as a `Span`
95-
var storage: Span<Element> { get }
95+
var span: Span<Element> { get }
9696
}
9797

9898
extension ArraySlice {
9999
/// Share this `Array`'s elements as a `Span`
100-
var storage: Span<Element> { get }
100+
var span: Span<Element> { get }
101101
}
102102

103103
extension ContiguousArray {
104104
/// Share this `Array`'s elements as a `Span`
105-
var storage: Span<Element> { get }
105+
var span: Span<Element> { get }
106106
}
107107

108108
extension String.UTF8View {
109109
/// Share this `UTF8View`'s code units as a `Span`
110-
var storage: Span<Unicode.UTF8.CodeUnit> { get }
110+
var span: Span<Unicode.UTF8.CodeUnit> { get }
111111
}
112112

113113
extension Substring.UTF8View {
114114
/// Share this `UTF8View`'s code units as a `Span`
115-
var storage: Span<Unicode.UTF8.CodeUnit> { get }
115+
var span: Span<Unicode.UTF8.CodeUnit> { get }
116116
}
117117

118118
extension CollectionOfOne {
119119
/// Share this `Collection`'s element as a `Span`
120-
var storage: Span<Element> { get }
120+
var span: Span<Element> { get }
121121
}
122122

123123
extension SIMD_N_ { // where _N_ ∈ {2, 3, 4 ,8, 16, 32, 64}
124124
/// Share this vector's elements as a `Span`
125-
var storage: Span<Scalar> { get }
125+
var span: Span<Scalar> { get }
126126
}
127127

128128
extension KeyValuePairs {
129129
/// Share this `Collection`'s elements as a `Span`
130-
var storage: Span<(Key, Value)> { get }
130+
var span: Span<(Key, Value)> { get }
131131
}
132132
```
133133

@@ -136,13 +136,13 @@ Conditionally to the acceptance of [`Vector`][SE-0453], we will also add the fol
136136
```swift
137137
extension Vector where Element: ~Copyable {
138138
/// Share this vector's elements as a `Span`
139-
var storage: Span<Element> { get }
139+
var span: Span<Element> { get }
140140
}
141141
```
142142

143143
#### Accessing the raw bytes of a `Span`
144144

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`:
146146

147147
```swift
148148
extension Span where Element: BitwiseCopyable {
@@ -160,12 +160,12 @@ We hope that `Span` and `RawSpan` will become the standard ways to access shared
160160
```swift
161161
extension UnsafeBufferPointer {
162162
/// Unsafely view this buffer as a `Span`
163-
var storage: Span<Element> { get }
163+
var span: Span<Element> { get }
164164
}
165165

166166
extension UnsafeMutableBufferPointer {
167167
/// Unsafely view this buffer as a `Span`
168-
var storage: Span<Element> { get }
168+
var span: Span<Element> { get }
169169
}
170170

171171
extension UnsafeRawBufferPointer {
@@ -193,22 +193,22 @@ While the `swift-foundation` package and the `Foundation` framework are not gove
193193
```swift
194194
extension Foundation.Data {
195195
// Share this `Data`'s bytes as a `Span`
196-
var storage: Span<UInt8> { get }
196+
var span: Span<UInt8> { get }
197197

198198
// Share this `Data`'s bytes as a `RawSpan`
199199
var bytes: RawSpan { get }
200200
}
201201
```
202202

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.
204204

205205
#### <a name="performance"></a>Performance
206206

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.
208208

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.
210210

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."
212212

213213
## Source compatibility
214214

@@ -226,10 +226,10 @@ The additions described in this proposal require a version of the Swift standard
226226

227227
#### Adding `withSpan()` and `withBytes()` closure-taking functions
228228

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:
230230
```swift
231231
var a = ContiguousArray(0..<8)
232-
var span = a.storage
232+
var span = a.span
233233
read(span)
234234
_ = consume span
235235
a.append(8)
@@ -239,7 +239,7 @@ In order to visually distinguish this lifetime, we could simply use a `do` block
239239
```swift
240240
var a = ContiguousArray(0..<8)
241241
do {
242-
let span = a.storage
242+
let span = a.span
243243
read(span)
244244
}
245245
a.append(8)
@@ -248,7 +248,7 @@ a.append(8)
248248
A more targeted solution may be a consuming function that takes a non-escaping closure:
249249
```swift
250250
var a = ContiguousArray(0..<8)
251-
var span = a.storage
251+
var span = a.span
252252
consuming(span) { span in
253253
read(span)
254254
}
@@ -257,9 +257,9 @@ a.append(8)
257257

258258
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.
259259

260-
#### Giving the properties different names
260+
#### Different naming for the properties
261261

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.
263263

264264
#### Disallowing the definition of non-escapable properties of non-escapable types
265265

@@ -269,15 +269,15 @@ The original version of this pitch disallowed this. As a consequence, the `bytes
269269

270270
#### Omitting extensions to `UnsafeBufferPointer` and related types
271271

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.
273273

274274
## <a name="directions"></a>Future directions
275275

276276
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.
277277

278278
#### <a name="MutableSpan"></a>Safe mutations with `MutableSpan<T>`
279279

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.
281281

282282
#### <a name="ContiguousStorage"></a>A `ContiguousStorage` protocol
283283

0 commit comments

Comments
 (0)