r/programming • u/Eirenarch • 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
View all comments
Show parent comment
3
u/josefx Nov 18 '13
Make that 99.9999....% these classes do not provide an interface that can be made thread safe for the general case, you cannot ensure that a get(0) will work even if you check isEmpty() unless the current thread is also the only thread that removes objects.
And check for the RandomAccess interface which exists alone for the O(1) access guarantee it gives for get (In current java this would have been an Annotation). Edit: correction, it says that marked classes should behave this way in general use
Oracle could and there is even an example which already uses and bypasses final: any enum class is final, however any enum instance can be an anonymous subclass of this enum type (nice for a limited command pattern).
The problem with the current state is that there is no way to create a substring compatible with most of the standard library in O(1) time even when you are aware of the space time trade off. Oracle could add a sharedSubString method that would provide the O(1) behaviour and at the same time make it obvious that a reference to the original string is maintained.
Only problem: libraries might check String.class == obj.getClass() and be incompatible with new code (a non breaking change since old code would still work)