Skip to content

Commit a3fdcfa

Browse files
authored
Reword note on wrapping arithmetic
1 parent b1683c8 commit a3fdcfa

File tree

1 file changed

+10
-18
lines changed

1 file changed

+10
-18
lines changed

stdlib/public/core/UnsafeBufferPointer.swift.gyb

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,11 @@ extension Unsafe${Mutable}BufferPointer: ${Mutable}Collection, RandomAccessColle
143143
// optimizer is not capable of creating partial specializations yet.
144144
// NOTE: Range checks are not performed here, because it is done later by
145145
// the subscript function.
146-
// NOTE: Unchecked math because unsafe pointers are not required to
147-
// behave sensibly if you misuse their indices. We validate in
148-
// debug mode.
146+
// NOTE: Wrapping math because we allow unsafe buffer pointers not to verify
147+
// index preconditions in release builds. Our (optimistic) assumption is
148+
// that the caller is already ensuring that indices are valid, so we can
149+
// elide the usual checks to help the optimizer generate better code.
150+
// However, we still check for overflow in debug mode.
149151
let result = i.addingReportingOverflow(1)
150152
_debugPrecondition(!result.overflow)
151153
return result.partialValue
@@ -158,9 +160,7 @@ extension Unsafe${Mutable}BufferPointer: ${Mutable}Collection, RandomAccessColle
158160
// optimizer is not capable of creating partial specializations yet.
159161
// NOTE: Range checks are not performed here, because it is done later by
160162
// the subscript function.
161-
// NOTE: Unchecked math because unsafe pointers are not required to
162-
// behave sensibly if you misuse their indices. We validate in
163-
// debug mode.
163+
// See note on wrapping arithmetic in `index(after:)` above.
164164
let result = i.addingReportingOverflow(1)
165165
_debugPrecondition(!result.overflow)
166166
i = result.partialValue
@@ -173,9 +173,7 @@ extension Unsafe${Mutable}BufferPointer: ${Mutable}Collection, RandomAccessColle
173173
// optimizer is not capable of creating partial specializations yet.
174174
// NOTE: Range checks are not performed here, because it is done later by
175175
// the subscript function.
176-
// NOTE: Unchecked math because unsafe pointers are not required to
177-
// behave sensibly if you misuse their indices. We validate in
178-
// debug mode.
176+
// See note on wrapping arithmetic in `index(after:)` above.
179177
let result = i.subtractingReportingOverflow(1)
180178
_debugPrecondition(!result.overflow)
181179
return result.partialValue
@@ -188,9 +186,7 @@ extension Unsafe${Mutable}BufferPointer: ${Mutable}Collection, RandomAccessColle
188186
// optimizer is not capable of creating partial specializations yet.
189187
// NOTE: Range checks are not performed here, because it is done later by
190188
// the subscript function.
191-
// NOTE: Unchecked math because unsafe pointers are not required to
192-
// behave sensibly if you misuse their indices. We validate in
193-
// debug mode.
189+
// See note on wrapping arithmetic in `index(after:)` above.
194190
let result = i.subtractingReportingOverflow(1)
195191
_debugPrecondition(!result.overflow)
196192
i = result.partialValue
@@ -203,9 +199,7 @@ extension Unsafe${Mutable}BufferPointer: ${Mutable}Collection, RandomAccessColle
203199
// optimizer is not capable of creating partial specializations yet.
204200
// NOTE: Range checks are not performed here, because it is done later by
205201
// the subscript function.
206-
// NOTE: Unchecked math because unsafe pointers are not required to
207-
// behave sensibly if you misuse their indices. We validate in
208-
// debug mode.
202+
// See note on wrapping arithmetic in `index(after:)` above.
209203
let result = i.addingReportingOverflow(n)
210204
_debugPrecondition(!result.overflow)
211205
return result.partialValue
@@ -218,9 +212,7 @@ extension Unsafe${Mutable}BufferPointer: ${Mutable}Collection, RandomAccessColle
218212
// optimizer is not capable of creating partial specializations yet.
219213
// NOTE: Range checks are not performed here, because it is done later by
220214
// the subscript function.
221-
// NOTE: Unchecked math because unsafe pointers are not required to
222-
// behave sensibly if you misuse their indices. We validate in
223-
// debug mode.
215+
// See note on wrapping arithmetic in `index(after:)` above.
224216
let maxOffset = limit.subtractingReportingOverflow(i)
225217
_debugPrecondition(!maxOffset.overflow)
226218
let l = maxOffset.partialValue

0 commit comments

Comments
 (0)