@@ -205,7 +205,14 @@ extension Unsafe${Mutable}BufferPointer: ${Mutable}Collection, RandomAccessColle
205
205
// optimizer is not capable of creating partial specializations yet.
206
206
// NOTE: Range checks are not performed here, because it is done later by
207
207
// the subscript function.
208
- return i + 1
208
+ // NOTE: Wrapping math because we allow unsafe buffer pointers not to verify
209
+ // index preconditions in release builds. Our (optimistic) assumption is
210
+ // that the caller is already ensuring that indices are valid, so we can
211
+ // elide the usual checks to help the optimizer generate better code.
212
+ // However, we still check for overflow in debug mode.
213
+ let result = i. addingReportingOverflow ( 1 )
214
+ _debugPrecondition ( !result. overflow)
215
+ return result. partialValue
209
216
}
210
217
211
218
@inlinable // unsafe-performance
@@ -215,7 +222,10 @@ extension Unsafe${Mutable}BufferPointer: ${Mutable}Collection, RandomAccessColle
215
222
// optimizer is not capable of creating partial specializations yet.
216
223
// NOTE: Range checks are not performed here, because it is done later by
217
224
// the subscript function.
218
- i += 1
225
+ // See note on wrapping arithmetic in `index(after:)` above.
226
+ let result = i. addingReportingOverflow ( 1 )
227
+ _debugPrecondition ( !result. overflow)
228
+ i = result. partialValue
219
229
}
220
230
221
231
@inlinable // unsafe-performance
@@ -225,7 +235,10 @@ extension Unsafe${Mutable}BufferPointer: ${Mutable}Collection, RandomAccessColle
225
235
// optimizer is not capable of creating partial specializations yet.
226
236
// NOTE: Range checks are not performed here, because it is done later by
227
237
// the subscript function.
228
- return i - 1
238
+ // See note on wrapping arithmetic in `index(after:)` above.
239
+ let result = i. subtractingReportingOverflow ( 1 )
240
+ _debugPrecondition ( !result. overflow)
241
+ return result. partialValue
229
242
}
230
243
231
244
@inlinable // unsafe-performance
@@ -235,7 +248,10 @@ extension Unsafe${Mutable}BufferPointer: ${Mutable}Collection, RandomAccessColle
235
248
// optimizer is not capable of creating partial specializations yet.
236
249
// NOTE: Range checks are not performed here, because it is done later by
237
250
// the subscript function.
238
- i -= 1
251
+ // See note on wrapping arithmetic in `index(after:)` above.
252
+ let result = i. subtractingReportingOverflow ( 1 )
253
+ _debugPrecondition ( !result. overflow)
254
+ i = result. partialValue
239
255
}
240
256
241
257
@inlinable // unsafe-performance
@@ -245,7 +261,10 @@ extension Unsafe${Mutable}BufferPointer: ${Mutable}Collection, RandomAccessColle
245
261
// optimizer is not capable of creating partial specializations yet.
246
262
// NOTE: Range checks are not performed here, because it is done later by
247
263
// the subscript function.
248
- return i + n
264
+ // See note on wrapping arithmetic in `index(after:)` above.
265
+ let result = i. addingReportingOverflow ( n)
266
+ _debugPrecondition ( !result. overflow)
267
+ return result. partialValue
249
268
}
250
269
251
270
@inlinable // unsafe-performance
@@ -255,11 +274,18 @@ extension Unsafe${Mutable}BufferPointer: ${Mutable}Collection, RandomAccessColle
255
274
// optimizer is not capable of creating partial specializations yet.
256
275
// NOTE: Range checks are not performed here, because it is done later by
257
276
// the subscript function.
258
- let l = limit - i
277
+ // See note on wrapping arithmetic in `index(after:)` above.
278
+ let maxOffset = limit. subtractingReportingOverflow ( i)
279
+ _debugPrecondition ( !maxOffset. overflow)
280
+ let l = maxOffset. partialValue
281
+
259
282
if n > 0 ? l >= 0 && l < n : l <= 0 && n < l {
260
283
return nil
261
284
}
262
- return i + n
285
+
286
+ let result = i. addingReportingOverflow ( n)
287
+ _debugPrecondition ( !result. overflow)
288
+ return result. partialValue
263
289
}
264
290
265
291
@inlinable // unsafe-performance
@@ -295,7 +321,8 @@ extension Unsafe${Mutable}BufferPointer: ${Mutable}Collection, RandomAccessColle
295
321
296
322
@inlinable // unsafe-performance
297
323
public var indices : Indices {
298
- return startIndex..< endIndex
324
+ // Not checked because init forbids negative count.
325
+ return Indices ( uncheckedBounds: ( startIndex, endIndex) )
299
326
}
300
327
301
328
/// Accesses the element at the specified position.
0 commit comments