Skip to content

Commit 47eecbd

Browse files
committed
Fix some Array-related performance regressions introduced by the new indexing model.
This is a manual specialization of index movement functions for a Strideable index that is required for Array performance. The optimizer is not capable of creating partial specializations yet. rdar://25946325
1 parent 4021836 commit 47eecbd

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
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
%{

0 commit comments

Comments
 (0)