r/programming • • Jun 05 '18

Code golfing challenge leads to discovery of string concatenation bug in JDK 9+ compiler

https://stackoverflow.com/questions/50683786/why-does-arrayin-i-give-different-results-in-java-8-and-java-10
2.2k Upvotes

355 comments sorted by

View all comments

4

u/[deleted] Jun 05 '18 edited Jun 06 '18

This is what happens when optimisations are done on a high level AST, instead of a relevant IR level.

EDIT: I was looking at the older JDK output which produced a StringBuilder for this code as a half-assed optimisation attempt. In JDK9 a single intrinsic call is emited, though I'd still classify this as an optimisation and blame for this issue is on a fact that javac does not use multiple IRs before reducing to bytecode.

40

u/ygra Jun 05 '18

String concatenation has been weird in Java for ages, since it used to be compiled into a new StringBuilder and some method calls on that. That's already a rather complex series of statements for a single +.

2

u/[deleted] Jun 05 '18

Sure, but this sort of optimisations is ok (see the idiom detection in LLVM for example). It's just the implementation that is amateurish here.

12

u/vytah Jun 05 '18

Except the bug is in javac and javac doesn't optimize anything (except for constant folding).

-8

u/[deleted] Jun 05 '18

This is exactly why javac is amateurish. A proper implementation should have included an IR suitable for analysis (hint: JVM is not suitable) and at least few trivial optimisation passes.

12

u/vytah Jun 05 '18

Why isn't JVM bytecode suitable for analysis? You can literally decompile it back to almost identical source code (assuming the source language was Java; Scala and Kotlin make many decompilers give up). I guess you don't like stack-oriented VM's?

And optimization is better left for the JVM: it knows the runtime context better and javac trying to outsmart it could backfire. Javac's optimizations would obfuscate the bytecode, making it less suitable for analysis.

-11

u/[deleted] Jun 05 '18 edited Jun 05 '18

Why isn't JVM bytecode suitable for analysis?

Do you have any idea on how to analyse it? Directly, without translating into something else. I don't.

You can literally decompile it back to almost identical source code

Go on. Decompile first, then analyse, rewrite, optimise. Then compile back. The language you decompile it to would be exactly the IR missing from javac.

And optimization is better left for the JVM

Wrong again. Low level optimisations are better with JVM. Domain-specific ones, such as idiom detection, must be done statically.

Javac's optimizations would obfuscate the bytecode, making it less suitable for analysis.

What?!? Optimisations make code more suitable for analysis. Try analysing anything at all before you do, say, a usual SSA transform.

EDIT: guess downvoters know something insightful about compiler analysis passes? Mind sharing?

6

u/mirhagk Jun 05 '18

Your original comment claimed this bug was a result of high level optimization passes.

Those don't exist so you were wrong.

You then turn around and attack java for not doing high level optimization passes.

Now I'm absolutely positive that you are going to turn around and say "well obviously the parse tree is too high level and the intermediate representation (JVM bytecode) is too low level. It needs an intermediate intermediate representation" because you're one of those people that would never admit a mistake and instead move the goalpost.

Add to all that your nonsensical

The language you decompile it to would be exactly the IR missing from javac.

Because that language would be Java. It'd be Java with some generic optimizations applied to it. And as you mentioned doing generic optimizations in the high level language would be silly.

1

u/[deleted] Jun 05 '18 edited Jun 05 '18

My original claim was that you should not do this shit on an AST. And yes, translating a concatenation into a complex construction involving instantiation of a StringBuilder is an optimisation, even if you do not do any further coalescing passes.

Those don't exist so you were wrong.

No, such a syntax sugar is an ill thought out optimisation attempt (vs. simply calling concat method).

Anyway, you can still do it, but not on an AST level.

Because that language would be Java.

Don't go there. It'd be exceptionally retarded. Think of something much more relevant - like, an SSA.

→ More replies (6)

1

u/[deleted] Jun 05 '18

Do you have any idea on how to analyse it? Directly, without translating into something else. I don't.

Why would it be any different than any other kind of bytecode? It's been a while since i've done that, but you can build a graph of jvm instructions, wire the jumps, and write whatever flow analysis you want ?

1

u/[deleted] Jun 05 '18

And you'll effectively produce another IR by building all those CFGs, stack state traces, and so on. That's my point. You better have that before lowering to stack machine, not after.

1

u/yawkat Jun 05 '18

Do you have any idea on how to analyse it? Directly, without translating into something else. I don't.

Using asm. Unless you count parsing as "translating into something else", which would match basically any IR

1

u/[deleted] Jun 05 '18

How do you analyse JVM bytecode without rewriting it into more suitable IRs? ASM does quite a lot of rewriting. Remember, javac is supposed to be fast.

1

u/yawkat Jun 05 '18

ASM does not do major rewriting beyond parsing stuff like resolving constant pool entries.

→ More replies (13)
→ More replies (2)

17

u/reddister Jun 05 '18 edited Jun 05 '18

This is not about optimization. (Even if it uses Stringbuilder now)

String += is syntactic sugar. This has nothing to do with optimization.

5

u/[deleted] Jun 05 '18

Recognising a StringBuilder pattern vs. a single concatenation is an optimisation. Or at least it should be.

The right way to implement such a thing - translate string addition to concatenation first, recognise the builder pattern in optimisation passes later.

The amateurish way of doing it is to treat it as a syntax sugar.

11

u/F54280 Jun 05 '18

The amateurish way of doing it is to treat it as a syntax sugar.

“Syntactic sugar causes cancer of the semicolon.” -- Alan Perlis

3

u/Deaod Jun 05 '18

The amateurish way of doing it is to treat it as a syntax sugar.

Even if you do treat it as syntax sugar, at least make sure your replacement is idempotent.

2

u/[deleted] Jun 05 '18

Of course. But this is also better done with lower level IRs.

4

u/mirhagk Jun 05 '18

It has nothing to do with recognizing a StringBuilder pattern or not, and nothing to do with optimizations.

It's an incorrect translation from += to relevant IR. They turned X += Y; into X = X + Y; which is incorrect.

The compiled code doesn't even use StringBuilder. If you look at the generated Java what it does is:

translate string addition to concatenation first,

ie exactly what you said it should do.

→ More replies (16)

2

u/Uncaffeinated Jun 05 '18

There is no such thing as string concatenation at the bytecode level. You have to compile it to some sequence of method calls anyway (or invokedynamic now) and StringBuilder is as good a choice as any.

1

u/[deleted] Jun 05 '18

Wrong. For a loop or a sequence of concatenations you must only instantiate one StringBuilder. A correct and efficient implementation must use loop analysis to avoid overhead.

2

u/Uncaffeinated Jun 05 '18

Javac doesn't do optimizations like that. It leaves things like that to the JVM.

AFAIK, the only optimizations javac does are precomputing the value of constant expressions and inlining static final fields.

Anyway, that's irrelevant to my original point. I was responding to the claim that javac could just compile it to "string concatenation" instead of StringBuilder sequences, which is false.

0

u/[deleted] Jun 05 '18

Do you see a difference between a single concatenation (using StringBuilder or not, does not matter) and a single StringBuilder with multiple append calls?

7

u/Uncaffeinated Jun 05 '18

I'm not even sure what we're debating at this point.

→ More replies (3)