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

Show parent comment

0

u/[deleted] Jun 05 '18

[deleted] — view deleted comment

154

u/Tarmen Jun 05 '18

Most places where += for String is relevant StringBuilder would be the idiomatic solution. This is because String in java is immutable so a loop like

for (int i = 0; i < n; i++) {
    s += "hi";
}

Has O(no) runtime.

206

u/Herbstein Jun 05 '18

Oh no :D

61

u/landonepps Jun 05 '18

A big oh no!

24

u/eckesicle Jun 05 '18

A O(no!)

12

u/CheezyXenomorph Jun 05 '18

no factorial = nonononononononononononononononononononononono

→ More replies (1)

3

u/Tarmen Jun 05 '18 edited Jun 05 '18

The actual one is O(n^2) but when string concatenation is as efficient as bubblesort Oh no seemed appropriate.

21

u/mirhagk Jun 05 '18

If it's in a loop yes. But if you're just doing `+=` a couple times then there's no need for StringBuilder. Of course `i++` wouldn't be used there, but that's still very weird that nobody noticed.

→ More replies (1)

37

u/Luvax Jun 05 '18

Isn't this one of these cases in which the Java Runtime will automatically use a StringBuilder even if you didn't?

Edit: Or the compiler, interpreter or which kind of godly entity is actually doing the optimisation.

17

u/Steveadoo Jun 05 '18

Yeah. The compiler will just use StringBuilder for that, but I'm not sure if it will hoist it out of the loop though. I'm pretty sure it will allocate once per iteration.

→ More replies (1)

9

u/moomaka Jun 05 '18

Most places where += for String is relevant StringBuilder would be the idiomatic solution.

The idiomatic solution is to use += and + with strings and let the compiler deal with it. For example the code you posted is not O(no) because javac compiles it to use StringBuilder anyway.

20

u/Tarmen Jun 05 '18

This is the bytecode shown with javap -c for that loop:

  public void main(java.lang.String[]);
    Code:
       0: ldc           #2                  // String
       2: astore_2
       3: iconst_0
       4: istore_3
       5: iload_3
       6: bipush        10
       8: if_icmpge     37
      11: new           #3                  // class java/lang/StringBuilder
      14: dup
      15: invokespecial #4                  // Method java/lang/StringBuilder."<init>":()V
      18: aload_2
      19: invokevirtual #5                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)L
java/lang/StringBuilder;
      22: ldc           #6                  // String hi
      24: invokevirtual #5                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)L
java/lang/StringBuilder;
      27: invokevirtual #7                  // Method java/lang/StringBuilder.toString:()Ljava/lang/String
;
      30: astore_2
      31: iinc          3, 1
      34: goto          5
      37: getstatic     #8                  // Field java/lang/System.out:Ljava/io/PrintStream;
      40: aload_2
      41: invokevirtual #9                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
      44: return

This is the equivalent of

for (int i = 0; i < 10; i++) {
    foo = new StringBuilder().append(foo).append("hi").toString();
}

which doesn't seem like it would fix the copying? The runtime might hoist the string builder out of the loop if the function is hot enough to get optimized.

→ More replies (8)
→ More replies (13)

178

u/moekakiryu Jun 05 '18

tbh I wouldn't be shocked if someone has, but it was probably just written off as some unsolvable bug and they rewrote the script because they couldn't be bothered working out what was causing it

225

u/ClownFundamentals Jun 05 '18

"Hey guys I think there's a bug in the compiler - I'm sure my code is right but it isn't working!"

christ fucking Steve again "Look just try to rewrite it and see if it goes away, k?"

69

u/cmsimike Jun 05 '18

Who hasn't been in that situation!?

32

u/-ghostinthemachine- Jun 05 '18

I've been down that hole about 8 times, but on two occasions it really was a compiler bug! Unfortunately that usually means it won't be fixed in time for your project. I still don't have the chutzpah to actually file a new bug report against a compiler though. ;)

28

u/Ksevio Jun 05 '18

On the rare occasion it's happened to me, I've found someone else has reported it already which is a relief to know you're not insane.

Of course it still sucks if it's marked on the roadmap to be fixed in Java 12 or something

4

u/CSMastermind Jun 05 '18

Likewise, I've found a compiler bug once and was relieved there was already an open issue about it.

6

u/[deleted] Jun 05 '18

[removed] — view removed comment

4

u/evaned Jun 05 '18

I am now very curious. Don't suppose you can link to the actual issue?

→ More replies (7)
→ More replies (2)

7

u/PM__YOUR__GOOD_NEWS Jun 05 '18

Bugs in the libraries/frameworks/etc. I use are the worst to troubleshoot because 99% of the time the fault is my code so as a rule of thumb I take external dependencies as perfect until proven flawed.

→ More replies (1)
→ More replies (3)

10

u/13steinj Jun 05 '18

On the other hand, I also wouldn't be surprised if it hasn't. Combined with the fact that JDK 9 was a relatively major change from 8 (modules, getting rid of the Observer pattern, so on), I don't think many companies would make the switch. In fact there are still some stuck on 6/7. That combined with

The issue seems to be limited to the string concatenation and assignment operator (+=) with an expression with side effect(s) as the left operand, like in array[test()]+="a", array[ix++]+="a", test()[index]+="a", or test().field+="a"

Makes it seem less likely to occur in production code that needs to readable and more like the OP's situation-- code golf.

→ More replies (2)

3

u/cjg_000 Jun 05 '18

Even if they did figure out the cause, they might not bother digging further and determining whether it was intended or a bug.

6

u/kynovardy Jun 05 '18

I mean when your code doesn't work, do you generally blame the compiler?

4

u/adrianmonk Jun 05 '18

If your code is the compiler, then you should.

And, like any other code, you ought to have tests so you know whether it works.

→ More replies (1)
→ More replies (21)