[SR-13956] Access to property with private setter leads to unnecessary swift_beginAccess call #56353
Labels
bug
A deviation from expected or documented behavior. Also: expected but undesirable behavior.
compiler
The Swift compiler in itself
performance
Environment
Swift 5.3 RELEASE
Additional Detail from JIRA
md5: c78f0c2dc0113bad52de085b4deaea1b
Issue Description:
Consider the following simple code.
When compiled, we get the following code for getBase and its callees:
Note that in this code path, we emit calls to
swift_beginAccess
ingetBase
.If we change the above code to make
var base
private
instead ofprivate(set)
, we get new output forgetBase
: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.The text was updated successfully, but these errors were encountered: