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

[4.0] runtime: Move String implementation stubs that want need the auto-released return value optimization to an ARC compiled file #12343

Conversation

aschwaighofer
Copy link
Member

String's hashValue function is implemented in terms of Foundation's hash
function in a runtime function on darwin platforms. For non-ASCII strings we
will call str.decomposedStringWithCanonicalMapping inside this runtime function
which will allocate a new NSString and return the result in the current
autorelease pool. We implemented this function in a file compiled without ARC.
This meant that we would leak said NSString into the current active autorelease
pool.
This patch moves the implementation to a file compiled with ARC. ARC will insert
objc_retainAutoreleasedReturnValue call and on platforms that require it an
marker for the hand-off of the autoreleased return value optimization.

SR-4889
rdar://32199117

  • Explanation: The standard library’s hashValue, uppercased, and loweredCased functions “leak” a temporary object into the current autorelease pool. This autorelease pool can be scope such that this means unbounded memory growth.
    The reason is that we implement the hashing function by a runtime stub that calls str.decomposedStringWithCanonicalMapping.hash. The decomposedStringWithCanonicalMapping call puts its result as a temporary object into the current autorelease pool. We did not compile the file that implemented this method with ARC and so the compiler did not insert code to remove the temporary object from the autorelease pool.
    The fix is to move these runtime stubs into a file that is compiled with ARC such that the temporary object is released at the end of the runtime stub call.

  • Scope: Calls to hashValue, uppercased, and lowercased leak an object into the current active autorelease pool whose scope is to large effectively causing a memory leak or a least unexpected memory growth. This code has been around since several releases.

  • Risk: Low. The runtime stubs where moved into a file that is compiled with ARC.

  • Testing: Runtime and assembly tests where added to the swift regression tests

…to-rel…

…eased return value optimization to an ARC compiled file

String's hashValue function is implemented in terms of Foundation's hash
function in a runtime function on darwin platforms. For non-ASCII strings we
will call str.decomposedStringWithCanonicalMapping inside this runtime function
which will allocate a new NSString and return the result in the current
autorelease pool. We implemented this function in a file compiled without ARC.
This meant that we would leak said NSString into the current active autorelease
pool.
This patch moves the implementation to a file compiled with ARC. ARC will insert
objc_retainAutoreleasedReturnValue call and on platforms that require it an
marker for the hand-off of the autoreleased return value optimization.

SR-4889
rdar://32199117
@aschwaighofer
Copy link
Member Author

@swift-ci Please test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants