Skip to content

Commit

Permalink
stdlib: improve debug performance of UnsafePointer.pointer(to:)
Browse files Browse the repository at this point in the history
Making those functions transparent enables to completely remove the creation of keypaths, even with -Onone.
rdar://127793797
  • Loading branch information
eeckstein committed May 16, 2024
1 parent fb036e9 commit 15acfbb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
6 changes: 0 additions & 6 deletions stdlib/public/core/UnsafePointer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,7 @@ extension UnsafePointer {
/// - Returns: A pointer to the stored property represented
/// by the key path, or `nil`.
@_alwaysEmitIntoClient
#if $Embedded
@_transparent
#endif
public func pointer<Property>(
to property: KeyPath<Pointee, Property>
) -> UnsafePointer<Property>? {
Expand Down Expand Up @@ -1332,9 +1330,7 @@ extension UnsafeMutablePointer {
/// - Returns: A pointer to the stored property represented
/// by the key path, or `nil`.
@_alwaysEmitIntoClient
#if $Embedded
@_transparent
#endif
public func pointer<Property>(
to property: KeyPath<Pointee, Property>
) -> UnsafePointer<Property>? {
Expand All @@ -1356,9 +1352,7 @@ extension UnsafeMutablePointer {
/// - Returns: A mutable pointer to the stored property represented
/// by the key path, or `nil`.
@_alwaysEmitIntoClient
#if $Embedded
@_transparent
#endif
public func pointer<Property>(
to property: WritableKeyPath<Pointee, Property>
) -> UnsafeMutablePointer<Property>? {
Expand Down
17 changes: 17 additions & 0 deletions test/SILOptimizer/pointer-performance.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// RUN: %target-swift-frontend %s -module-name=test -parse-as-library -emit-sil | %FileCheck %s

// REQUIRES: swift_stdlib_no_asserts,optimized_stdlib

public struct S {
var a: Int
var b: Int
}

// Check that even with -Onone, no keypath is created.

// CHECK-LABEL: sil @$s4test6testitySPySiGSgSPyAA1SVGF :
// CHECK-NOT: keypath
// CHECK: } // end sil function '$s4test6testitySPySiGSgSPyAA1SVGF'
public func testit(_ p: UnsafePointer<S>) -> UnsafePointer<Int>? {
return p.pointer(to: \.b)
}

0 comments on commit 15acfbb

Please sign in to comment.