@@ -143,9 +143,11 @@ extension Unsafe${Mutable}BufferPointer: ${Mutable}Collection, RandomAccessColle
143
143
// optimizer is not capable of creating partial specializations yet.
144
144
// NOTE: Range checks are not performed here, because it is done later by
145
145
// 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.
149
151
let result = i. addingReportingOverflow ( 1 )
150
152
_debugPrecondition ( !result. overflow)
151
153
return result. partialValue
@@ -158,9 +160,7 @@ extension Unsafe${Mutable}BufferPointer: ${Mutable}Collection, RandomAccessColle
158
160
// optimizer is not capable of creating partial specializations yet.
159
161
// NOTE: Range checks are not performed here, because it is done later by
160
162
// 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.
164
164
let result = i. addingReportingOverflow ( 1 )
165
165
_debugPrecondition ( !result. overflow)
166
166
i = result. partialValue
@@ -173,9 +173,7 @@ extension Unsafe${Mutable}BufferPointer: ${Mutable}Collection, RandomAccessColle
173
173
// optimizer is not capable of creating partial specializations yet.
174
174
// NOTE: Range checks are not performed here, because it is done later by
175
175
// 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.
179
177
let result = i. subtractingReportingOverflow ( 1 )
180
178
_debugPrecondition ( !result. overflow)
181
179
return result. partialValue
@@ -188,9 +186,7 @@ extension Unsafe${Mutable}BufferPointer: ${Mutable}Collection, RandomAccessColle
188
186
// optimizer is not capable of creating partial specializations yet.
189
187
// NOTE: Range checks are not performed here, because it is done later by
190
188
// 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.
194
190
let result = i. subtractingReportingOverflow ( 1 )
195
191
_debugPrecondition ( !result. overflow)
196
192
i = result. partialValue
@@ -203,9 +199,7 @@ extension Unsafe${Mutable}BufferPointer: ${Mutable}Collection, RandomAccessColle
203
199
// optimizer is not capable of creating partial specializations yet.
204
200
// NOTE: Range checks are not performed here, because it is done later by
205
201
// 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.
209
203
let result = i. addingReportingOverflow ( n)
210
204
_debugPrecondition ( !result. overflow)
211
205
return result. partialValue
@@ -218,9 +212,7 @@ extension Unsafe${Mutable}BufferPointer: ${Mutable}Collection, RandomAccessColle
218
212
// optimizer is not capable of creating partial specializations yet.
219
213
// NOTE: Range checks are not performed here, because it is done later by
220
214
// 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.
224
216
let maxOffset = limit. subtractingReportingOverflow ( i)
225
217
_debugPrecondition ( !maxOffset. overflow)
226
218
let l = maxOffset. partialValue
0 commit comments