Skip to content

Commit 607f4eb

Browse files
authored
Merge pull request #65612 from eeckstein/inline-array-count
stdlib: force inlining of `ContiguousArray.endIndex` and `ContiguousArray._getCount`
2 parents 9a57840 + 5d43f75 commit 607f4eb

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

stdlib/public/core/ContiguousArray.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public struct ContiguousArray<Element>: _DestructorSafeContainer {
5353
//===--- private helpers---------------------------------------------------===//
5454
extension ContiguousArray {
5555
@inlinable
56+
@inline(__always)
5657
@_semantics("array.get_count")
5758
internal func _getCount() -> Int {
5859
return _buffer.immutableCount
@@ -204,6 +205,7 @@ extension ContiguousArray: RandomAccessCollection, MutableCollection {
204205
/// If the array is empty, `endIndex` is equal to `startIndex`.
205206
public var endIndex: Int {
206207
@inlinable
208+
@inline(__always)
207209
get {
208210
return _getCount()
209211
}

test/SILOptimizer/array_loop.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: %target-swift-frontend -O -module-name=test -emit-sil -primary-file %s | %FileCheck %s
2+
3+
// REQUIRES: swift_stdlib_no_asserts
4+
// REQUIRES: swift_in_compiler
5+
6+
7+
// Test that even with a generic array the iteration is done efficiently.
8+
9+
// CHECK-LABEL: sil @$s4test0A15ContiguousArrayySis0bC0VyxG_SixXEtlF : $@convention(thin) <Element> (@guaranteed ContiguousArray<Element>, @guaranteed @noescape @callee_guaranteed @substituted <τ_0_0> (@in_guaranteed τ_0_0) -> Int for <Element>) -> Int {
10+
// CHECK-NOT: function_ref
11+
// CHECK-NOT: method
12+
// CHECK: } // end sil function '$s4test0A15ContiguousArrayySis0bC0VyxG_SixXEtlF'
13+
public func testContiguousArray<Element>(_ a: ContiguousArray<Element>, _ c: (Element) -> Int) -> Int {
14+
var s = 0
15+
for x in a {
16+
s += c(x)
17+
}
18+
return s
19+
}
20+

0 commit comments

Comments
 (0)