String concatenation tweaks

Alex Buckley alex.buckley at
Fri Mar 20 21:10:26 UTC 2015

On 3/20/2015 3:52 AM, Florian Weimer wrote:
> On 03/13/2015 12:28 AM, Alex Buckley wrote:
>> More abstract presentation. Given the expression:
>>    "foo" + m() + n()
>> you must not evaluate n() if evaluation of "foo" + m() completes
>> abruptly. The proposed implementation evaluates n() regardless.
>> All is not lost. In the proposed implementation, the abrupt completion
>> of "foo" + m() could occur because an append call fails or (thanks to
>> Jon for pointing this out) the StringBuilder ctor fails. The
>> quality-of-implementation issue is thus: if the proposed implementation
>> is of sufficiently high quality to guarantee that the ctor and the first
>> append both succeed, then the evaluation of "foo" + m() will always
>> complete normally, and it would be an unobservable (thus acceptable)
>> implementation detail to evaluate n() early.
> The current implementation can still call m() and n() and fail with OOME
> in something somewhat related to the concatenation, with a sufficiently
> unfortunate garbage collection.  (That's why OOME is a
> VirtualMachineError and not a RuntimeException.)

In the OpenJDK implementation, n() is only called if m()'s result was 
concatenated successfully; if the concatenation failed for any reason 
(OOME is an obvious one; maybe there will be additional failure modes 
for StringBuilder#append in future) then n() isn't called.

> I don't think preserving the OOME behavior is worth the effort.

We came to a nice agreement in the follow-ups to the quoted mail.


More information about the compiler-dev mailing list