Skip to content

Commit f67cee6

Browse files
committed
Merge pull request #2370 from swiftix/new-indexing-model-regression-fixes
Fix some Array-related performance regressions introduced by the new …
2 parents e8c8f03 + 4544cd1 commit f67cee6

File tree

2 files changed

+84
-1
lines changed

2 files changed

+84
-1
lines changed

stdlib/public/core/Arrays.swift.gyb

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,88 @@ public struct ${Self}<Element>
503503
%end
504504
}
505505

506+
@warn_unused_result
507+
public func index(after i: Int) -> Int {
508+
// NOTE: this is a manual specialization of index movement for a Strideable
509+
// index that is required for Array performance. The optimizer is not
510+
// capable of creating partial specializations yet.
511+
// NOTE: Range checks are not performed here, because it is done later by
512+
// the subscript function.
513+
return i + 1
514+
}
515+
516+
public func formIndex(after i: inout Int) {
517+
// NOTE: this is a manual specialization of index movement for a Strideable
518+
// index that is required for Array performance. The optimizer is not
519+
// capable of creating partial specializations yet.
520+
// NOTE: Range checks are not performed here, because it is done later by
521+
// the subscript function.
522+
i += 1
523+
}
524+
525+
@warn_unused_result
526+
public func index(before i: Int) -> Int {
527+
// NOTE: this is a manual specialization of index movement for a Strideable
528+
// index that is required for Array performance. The optimizer is not
529+
// capable of creating partial specializations yet.
530+
// NOTE: Range checks are not performed here, because it is done later by
531+
// the subscript function.
532+
return i - 1
533+
}
534+
535+
public func formIndex(before i: inout Int) {
536+
// NOTE: this is a manual specialization of index movement for a Strideable
537+
// index that is required for Array performance. The optimizer is not
538+
// capable of creating partial specializations yet.
539+
// NOTE: Range checks are not performed here, because it is done later by
540+
// the subscript function.
541+
i -= 1
542+
}
543+
544+
@warn_unused_result
545+
public func index(_ i: Int, offsetBy n: Int) -> Int {
546+
// NOTE: this is a manual specialization of index movement for a Strideable
547+
// index that is required for Array performance. The optimizer is not
548+
// capable of creating partial specializations yet.
549+
// NOTE: Range checks are not performed here, because it is done later by
550+
// the subscript function.
551+
return i + n
552+
}
553+
554+
@warn_unused_result
555+
public func index(
556+
_ i: Int, offsetBy n: Int, limitedBy limit: Int
557+
) -> Int? {
558+
// NOTE: this is a manual specialization of index movement for a Strideable
559+
// index that is required for Array performance. The optimizer is not
560+
// capable of creating partial specializations yet.
561+
// NOTE: Range checks are not performed here, because it is done later by
562+
// the subscript function.
563+
let l = limit - i
564+
if n > 0 ? l >= 0 && l < n : l <= 0 && n < l {
565+
return nil
566+
}
567+
return i + n
568+
}
569+
570+
@warn_unused_result
571+
public func distance(from start: Int, to end: Int) -> Int {
572+
// NOTE: this is a manual specialization of index movement for a Strideable
573+
// index that is required for Array performance. The optimizer is not
574+
// capable of creating partial specializations yet.
575+
// NOTE: Range checks are not performed here, because it is done later by
576+
// the subscript function.
577+
return end - start
578+
}
579+
580+
public func _failEarlyRangeCheck(_ index: Int, bounds: Range<Int>) {
581+
// NOTE: This method is a no-op for performance reasons.
582+
}
583+
584+
public func _failEarlyRangeCheck(_ range: Range<Int>, bounds: Range<Int>) {
585+
// NOTE: This method is a no-op for performance reasons.
586+
}
587+
506588
public typealias Indices = CountableRange<Int>
507589

508590
%{

stdlib/public/core/RandomAccessCollection.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ extension RandomAccessIndexable {
8181
func index(
8282
i: Index, offsetBy n: IndexDistance, limitedBy limit: Index
8383
) -> Index? {
84+
// FIXME: swift-3-indexing-model: tests.
8485
let l = distance(from: i, to: limit)
85-
if n > 0 ? l > 0 && l < n : l <= 0 && n < l {
86+
if n > 0 ? l >= 0 && l < n : l <= 0 && n < l {
8687
return nil
8788
}
8889
return index(i, offsetBy: n)

0 commit comments

Comments
 (0)