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

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

4

u/[deleted] Jun 05 '18

/knocks over sentry

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.

2

u/josefx Jun 06 '18

But if you're just doing += a couple times then there's no need for StringBuilder.

It actually compiled down to StringBuilder for some time, so using it explicitly to concatenate smaller strings was pointless. The current Javadoc mentions StringBuffer, StringBuilder, or java.lang.invoke.StringConcatFactory as backends the compiler could use for string concatenation.

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.

2

u/aiij Jun 06 '18

Even if the compiler didn't pre-allocate the right length, I would be surprised StringBuilder allocates more than O(log n).

And, I am not surprised.

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.

3

u/moomaka Jun 05 '18

The JIT will likely remove the loop entirely

16

u/ForeverAlot Jun 05 '18

After executing it 200 times to figure out it's a no-op. The JVM is a marvel but it's not magic.

6

u/moomaka Jun 05 '18

If you only run that 200 times, you don't care what it's performance characteristics are. Also with a loop of 10, big O notation is not applicable so the only way to determine what is fastest is to profile it.

5

u/ForeverAlot Jun 05 '18

It's just important to understand that "the JVM will probably inline that" is never the whole picture; and cold code is no excuse for doing obviously* redundant work.

*The StringBuilder transformation and its limitations are basic knowledge that any Java programmer needs to understand early in their career. Naturally, this does not apply to people that don't work extensively with Java.

5

u/moomaka Jun 05 '18

It's just important to understand that "the JVM will probably inline that" is never the whole picture; and cold code is no excuse for doing obviously* redundant work.

Thing is, you have no idea what work is going to be done in that code. The CPU doesn't execute Java, it doesn't execute Java bytecode, and it doesn't even execute assembly in a straight-forward manor. You may find that the 'looks like it does more work' approach is substantially faster than the 'looks fast' approach because it blows the CPU cache constantly or it causes nasty dependency chains that kill your IPC, or a dozen other things.

Write code in a way that is easiest to understand first then, only if performance is an issue, profile carefully and iterate. Prematurely 'optimizing' trivial code is not a net benefit to the application.

1

u/ForeverAlot Jun 05 '18

You may find that the 'looks like it does more work' approach is substantially faster than the 'looks fast' approach because it blows the CPU cache constantly or it causes nasty dependency chains that kill your IPC, or a dozen other things.

The rule is, don't write "clever" code expecting to outperform the compiler, not, write whatever code because the compiler will fix it for you. Intuition is easily wrong at a macro level, certainly, but it is also easily accurate at a micro level. When it comes to non-trivial string concatenation with + in Java, for instance, the micro level intuition is extremely straight-forward: either you are doing too much work always, or you are doing too much work until the JVM finds a way to save you from yourself. Fixing something like that once won't make a dent in any mid-sized application, granted, but it's still fundamentally just the wrong thing to do. All environments have rules like this, because we don't work with abstract machines.

List traversal in Java might be a better example because it doesn't rely on any compiler special-casing. An inexperienced programmer is likely to start with an ArrayList because that's what they'll encounter nearly everywhere. Fortunately, that's the correct choice for most problems: it plays really well with the CPU cache and prefetcher. On the other hand, an inexperienced Computer Scientist might go out of their way to choose LinkedList because of big-O and they would almost surely be making a wrong choice.

→ More replies (1)

1

u/[deleted] Jun 05 '18

I find that the fastest way to learn how to write code that is easy to understand is to try write code that has the best performance. Until your get to the point where profiling is needed, they tend to be the same thing, and you can decide at that point if you are willing and able to trade off clarity for performance (or vice versa).

3

u/chrisrazor Jun 05 '18

If strings are immutable, how can += ever be applied meaningfully to one?

22

u/Tarmen Jun 05 '18

The object on the heap is immutable, the pointer to the string is mutable.

6

u/Eckish Jun 05 '18

Strings are objects that live on the heap. Your string variable is a reference to said heap object. When you use +=, an entirely new string object is created and your reference is updated.

9

u/tavianator Jun 05 '18

ints are also immutable, you ever try changing the number 4? I did but it's still 4. Values may be immutable but variables can, well, vary.

→ More replies (9)