r/programming • u/jonjonbee • 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
View all comments
Show parent comment
6
u/evaned Jun 05 '18 edited Jun 05 '18
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 indexi, it's trivially easy and fast (and constant time) to get an iterator to any other indexjin 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 backwardnitems isO(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 stillstd::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
+= 1can be generalized to+= nbut++can't be generalized (without an explicit loop) makes a real difference in code.Edit: fixed word typo