Skip to content
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

[SR-13956] Access to property with private setter leads to unnecessary swift_beginAccess call #56353

Open
Lukasa opened this issue Dec 11, 2020 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself performance

Comments

@Lukasa
Copy link
Contributor

Lukasa commented Dec 11, 2020

Previous ID SR-13956
Radar rdar://problem/72215351
Original Reporter @Lukasa
Type Bug
Environment

Swift 5.3 RELEASE

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, Performance
Assignee None
Priority Medium

md5: c78f0c2dc0113bad52de085b4deaea1b

Issue Description:

Consider the following simple code.

final class Test {
    private(set) var base: Int = 0
    func getBase() -> Int {
        return self.base
    }
}

When compiled, we get the following code for getBase and its callees:

_$s4test4TestC7getBaseSiyF:        // test.Test.getBase() -> Swift.Int
    push       rbp
    mov        rbp, rsp
    pop        rbp
    jmp        _$s4test4TestC4baseSivg      ; test.Test.base.getter : Swift.Int

_$s4test4TestC4baseSivg:        // test.Test.base.getter : Swift.Int
    push       rbp                          ; CODE XREF=_$s4test4TestC7getBaseSiyF+5
    mov        rbp, rsp
    sub        rsp, 0x20
    lea        rdi, qword [r13+0x10]
    lea        rsi, qword [rbp+var_18]
    xor        edx, edx
    xor        ecx, ecx
    call       imp___stubs__swift_beginAccess ; swift_beginAccess
    mov        rax, qword [r13+0x10]
    add        rsp, 0x20
    pop        rbp
    ret

Note that in this code path, we emit calls to swift_beginAccess in getBase.

If we change the above code to make var base private instead of private(set), we get new output for getBase:

_$s4test4TestC7getBaseSiyF:        // test.Test.getBase() -> Swift.Int
    push       rbp
    mov        rbp, rsp
    mov        rax, qword [r13+0x10]
    pop        rbp
    ret

Note that the call to swift_beginAccess is gone.

This optimisation implies that there exists an optimisation path that can audit all accesses to a class property and validate at compile time that it is not possible for this code to violate the law of exclusivity and so no runtime enforcement is required.

The issue here is that the same optimisation should apply to the first version of the code. Law of exclusivity violations require that at some point someone writes to this field. If they never do, we cannot violate it. Having a private setter makes that just as auditable in the first case as the second, however Swift fails to do it.

This optimisation would be very valuable. SwiftNIO's ByteBuffer emits significant beginAccess traffic in their code paths due to obtaining a single field off their base storage.

@Lukasa
Copy link
Contributor Author

Lukasa commented Dec 11, 2020

@swift-ci create

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself performance
Projects
None yet
Development

No branches or pull requests

1 participant