Context:
- https://twitter.com/an0/status/627476411050147840
- http://airspeedvelocity.net/2015/08/03/arrays-linked-lists-and-performance/
Array concatenation optimization is easy. The hard part is ARC optimization. Essentially, the question is whether ARC can eliminate the autorelease_1
& retain_1
pair and just transfer the ownership from obj
to arg
:
// I have to use Objective-C code to demonstrate because there is no Manual Reference Counting in Swift at all.
Obj *foo(Obj *arg) {
Obj *result = [arg retain_1];
…
return [result autorelease_2];
}
{
…
Obj *obj = …;
obj = [foo([obj autorelease_1]) retain_2];
…
}
ARC surely does eliminate the autorelease_2
& retain_2 pair
. That’s exactly what objc_retainAutoreleaseReturnValue
and objc_retainAutoreleasedReturnValue
do.
I had the impression that the similar optimization is also done for autorelease_1
& retain_1
. But I didn’t found any supporting documentation. Since ARC is open sourced there must be people who can confirm whether it is true or false.
If ARC doesn’t do this optimization yet I still think it is doable in future because we programmers can clearly see the optimization opportunity here and compilers are always evolving to do what we can do with code and then better us.