Merge pull request #65612 from eeckstein/inline-array-count

stdlib: force inlining of `ContiguousArray.endIndex` and `ContiguousArray._getCount`
This commit is contained in:
eeckstein
2023-05-03 21:09:25 +02:00
committed by GitHub
2 changed files with 22 additions and 0 deletions

View File

@@ -53,6 +53,7 @@ public struct ContiguousArray<Element>: _DestructorSafeContainer {
//===--- private helpers---------------------------------------------------===//
extension ContiguousArray {
@inlinable
@inline(__always)
@_semantics("array.get_count")
internal func _getCount() -> Int {
return _buffer.immutableCount
@@ -204,6 +205,7 @@ extension ContiguousArray: RandomAccessCollection, MutableCollection {
/// If the array is empty, `endIndex` is equal to `startIndex`.
public var endIndex: Int {
@inlinable
@inline(__always)
get {
return _getCount()
}

View File

@@ -0,0 +1,20 @@
// RUN: %target-swift-frontend -O -module-name=test -emit-sil -primary-file %s | %FileCheck %s
// REQUIRES: swift_stdlib_no_asserts
// REQUIRES: swift_in_compiler
// Test that even with a generic array the iteration is done efficiently.
// 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 {
// CHECK-NOT: function_ref
// CHECK-NOT: method
// CHECK: } // end sil function '$s4test0A15ContiguousArrayySis0bC0VyxG_SixXEtlF'
public func testContiguousArray<Element>(_ a: ContiguousArray<Element>, _ c: (Element) -> Int) -> Int {
var s = 0
for x in a {
s += c(x)
}
return s
}