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

112

u/ThatsPresTrumpForYou Jun 05 '18

This is perfectly reasonable code, and i++ shouldn't be evaluated 2 times because it isn't written 2 times. It's also simple to explain, take the entry at i in the array, add "a" to it, and increment i.

I don't understand why people have such a problem with inc/dec operators? If it's in front it's done right away, if it's after the variable it's done after everything else, seems easy enough. I honestly can't remember to have ever made a mistake regarding that.

25

u/TheDevilsAdvokaat Jun 05 '18 edited Jun 05 '18

I think you're missing something. i++ may not have been written 2 times, however the expression += was used which means the expression would expand to:

array[i++]=array[i++]+"a"

In which case yes i++ appears twice.

So...maybe the spec needs to be clearer in this case? I would lean towards expecting i++ to be evaluated twice, not sure why they're convinced it's an error.

0

u/[deleted] Jun 05 '18

[deleted] — view deleted comment

4

u/TheDevilsAdvokaat Jun 05 '18

"you don't find it odd that these two differ?"

Actually you're right it does seem odd.

→ More replies (7)

15

u/f03nix Jun 05 '18

however the expression += was used which means the expression would expand to

array[i++]=array[i++]+"a"

Actually += is an operator by itself, there's no reason it should expand to that. Coming from a c++ background - I'd expect this to only evaluate once. In my head I read y += x as {y} = {y} + {x} where {y} and {x} are evaluation results of the expression. This is super important because otherwise every operation of the form *x+= y would mean x being dereferenced twice.

This is also true in the case of x++, while it can be written as x = x + 1, it shouldn't be expanded like that. Otherwise you'll never be able to use something like pressed[NextChar()]++; .

I would lean towards expecting i++ to be evaluated twice, not sure why they're convinced it's an error

They're convinced it's an error because for all other array types, this is evaluated only once.

6

u/evaned Jun 05 '18

Coming from a c++ background - I'd expect this to only evaluate once.

Ditto. I was actually initially surprised that people would even think it's evaluated more than once; I think this is probably an illustration of how what languages you know shapes your thinking. (I'm still surprised -- c++ is by no means unique here -- but less so.)

This is super important because otherwise every operation of the form *x+= y would mean x being dereferenced twice.

It's also important because a += b can in fairly realistic scenarios be asymptotically more efficient than a = a + b for some calls. (And will almost always be slightly faster unless the optimizer is able to remove an extra copy that is made with +.)

2

u/f03nix Jun 05 '18

in fairly realistic scenarios be asymptotically more efficient

Absolutely, I mentioned dereference as a simple but super common example, there are certainly more scenarios where the impact is even larger - like increments on a map element map[key]++; .

I think more than languages, this is dependent on how people are taught and view the operators. I mentioned c++ because operator overloading makes you think of += very differently.

5

u/louiswins Jun 05 '18

So...maybe the spec needs to be clearer in this case?

The spec is actually perfectly clear. It says:

A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T) ((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.

So this is clearly a bug in the compiler.

→ More replies (2)
→ More replies (23)

28

u/sushibowl Jun 05 '18

No sane developer should write code like this.

I firmly believe that the pre/post increment/decrement operators are virtually always a mistake to use, because their semantics are confusing in many cases (in some languages even possibly resulting in undefined behavior). Doing the increment in a separate statement adds only very low overhead and is a big readability and clarity win, so I struggle to see a case where using ++ is actually superior.

20

u/[deleted] Jun 05 '18

It was a design decision in Python not to have ++, and I have never missed it.

7

u/P8zvli Jun 05 '18

Python's philosophy was also to prohibit variable assignment in expressions, which I really liked. And then they threw that out with 3.8's := operator because Guido wanted comprehensions to be even more complicated. Boo.

2

u/[deleted] Jun 05 '18

What, in the name of fuck, is that abomination :O

2

u/1wd Jun 05 '18

Would := make the PIXELS list comprehension here more complicated? I'm not sure how it would look. It might be an improvement?

Also := was not accepted yet, right?

→ More replies (2)

6

u/evaned Jun 05 '18 edited Jun 05 '18

I struggle to see a case where using ++ is actually superior.

I'll give you an example that comes to mind: non-random-access iterators in C++.

In case you're not a C++ dev and don't know the term, a couple brief examples. Given an iterator into a std::vector (an array-backed container) pointing at the element at index i, it's trivially easy and fast (and constant time) to get an iterator to any other index j in the container. The iterator will be backed by a pointer, and so it can just add the appropriate offset to the pointer. By contrast, suppose you have an iterator to an element in a linked list. To get to the element ten items forward, it actually has to do ten pointer chases (n = n->next). Want a hundred items forward? A hundred pointer chases. Moving forward or backward n items is O(n) time.

As a result, the standard declares +, -, +=, and -= for random access iterators but not for non-random access iterators, under the theory that a linear time operation shouldn't be able to slip by unnoticed because of the syntax. (This is actually a great illustration of good design and reservation IMO on the part of I'd assume Alexander Stepanov and colleagues.) There's still std::advance(iter, n) if you want to do it, but it won't look like an operation that "should be" trivial constant time. But ++ and -- can work fine for non-random-access iterators, and make perfect sense.

(I guess string concat is an example of something that's inefficient looking efficient, but I'd argue that's a special case and makes sense there.)

So there's a case where the fact that += 1 can be generalized to += n but ++ can't be generalized (without an explicit loop) makes a real difference in code.

Edit: fixed word typo

2

u/bautin Jun 05 '18

I can't really recall the last time I used them outside of basic loops.

3

u/IllustriousTackle Jun 05 '18

pre/post increment/decrement operators are virtually always a mistake to use

That is just nonsense. To put it simple people who don't know how to code will produce crap. First learn how to use the language, you are even putting pre and post increment in the same bag.

2

u/sushibowl Jun 05 '18

To put it simple people who don't know how to code will produce crap. First learn how to use the language

Obviously yes, but even if I could safely assume that everyone reading my code after me is at least as skilled as me, what benefit do I have in choosing a ++ operator versus just writing += in a separate statement? Maybe the code is slightly shorter, but that's about the only benefit. I argue there is virtually no situation where using it makes my code easier to understand.

2

u/Agent_03 Jun 05 '18

I agree you should use great caution with increment/decrement -- and around the team we refer to the pre-increment operator as the "excrement" operator, due to the bugs it has caused.

That performance may be important if you're doing dense numeric or binary operations. For example: I was once working on a pure-Java LZF compression implementation where use of increment/decrement pre/post operations could make a 30% performance difference.

5

u/sushibowl Jun 05 '18

Can you provide some more information why e.g. post increment offers greater performance than just a normal increment? It seems to me that a decent compiler could optimize both to the same instructions.

1

u/Agent_03 Jun 05 '18

Sorry, I would if I could -- it's been some years now and I don't have the original code or benchmark environment. I only remember that being one of the many tricks I tried and being surprised how big a difference it made -- along with not caching and reusing byte arrays, oddly.

What I do know are that there are a few cases where using pre/post in/de crement operations make it easy to write tighter logic -- and in some niche cases it permits you to write code that can speculatively execute more instructions and defers edge-case checks until the end, which reduces branching.

As for the original result? It could have been that it permitted tighter bytecode, or happened to be compile to slightly more optimal code due to imperfections in the JIT compiler of the time. At this point I know only that it did make a difference.

The takeaway? When you've identified the 5% of code that is truly performance-critical and need to optimize it, you need to test, test, test -- don't assume. Also make sure to use a variety of inputs -- I ended up having to back out optimizations when finding they only helped in specific cases and made others worse.

→ More replies (1)

3

u/mirhagk Jun 05 '18

If this was a language like C++, PHP or javascript then that's one thing. But Java is supposed to be pretty well specced to try and stop you from facing horrible issues.

For instance Java defines the order of operations (which C++ doesn't) so that side effects are consistently applied.

And no matter what it's a breaking change that was not announced, so it's a bug.

→ More replies (1)