[4.0] runtime: Move String implementation stubs that want need the auto-released return value optimization to an ARC compiled file #12343
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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