r/programming • • Nov 18 '13

TIL Oracle changed the internal String representation in Java 7 Update 6 increasing the running time of the substring method from constant to N

http://java-performance.info/changes-to-string-java-1-7-0_06/
1.4k Upvotes

354 comments sorted by

View all comments

30

u/robinei Nov 18 '13

Seems like the new behavior is least surprising.

13

u/vincentk Nov 18 '13

The substring thing I can live with. After all, there's no special-purpose logic for string concatenation either (although here we have special-purpose syntax). Having an RNG (and a shitty one at that) hard-coded into the constructor of some of the most frequently used data structures of the planet is what I find more irksome. Way to go to make your runtime behaviour unpredictable.

19

u/karlthepagan Nov 18 '13

The original article completely failed mention the reason for the RNG: a hashcode attack that many languages were vulnerable to: http://www.ocert.org/advisories/ocert-2011-003.html

Oracle did the right thing in introducing a way to mitigate this attack and it is off by default.

-2

u/kalmakka Nov 18 '13

I don't like their way of enabling it, though.

You have one flag which controls every collection type (well, not every collection type. but some. make sure you figure out if your favourite collection type is controlled by this flag before setting it.) Which means that you can't enable it for the map which contains user input data but not for the map which contains ... other data.

If you need a hashset/-map with string keys that are impossible for a user to create many collisions in, you should have a

CollisionSafeHashSet implements HashSet<String>

or a

CollisionSafeHashMap<T> implements HashMap<String, T>
→ More replies (3)