Skip to content

stdlib: force inlining of ContiguousArray.endIndex and ContiguousArray._getCount #65612

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions stdlib/public/core/ContiguousArray.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
Expand Down
20 changes: 20 additions & 0 deletions test/SILOptimizer/array_loop.swift
Original file line number Diff line number Diff line change
@@ -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
}