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

0

u/[deleted] Nov 18 '13

[deleted] — view deleted comment

9

u/[deleted] Nov 18 '13

Reading the article instead of circlejerking would've told you that they didn't "get anything wrong".

0

u/[deleted] Nov 18 '13

[deleted] — view deleted comment

4

u/[deleted] Nov 18 '13

Changing complexity from constant to linear is a terrible thing to do

The substring routines in most languages have linear complexity, and it's not considered terrible. Substring referencing was an optimization, which turned out to cause memories leaks. You load a config file into memory, pull out the 8 characters you need, and the entire buffer is held forever. You cache one word from each network message in a high speed communication channel, and the entirety of every message stays in memory forever. That's bad. They fixed it by removing an optimization that was never documented to begin with.

3

u/BonzaiThePenguin Nov 18 '13

Constant time requires keeping the old string around in memory, or having a memory leak if you free the old string. The linear time is for copying the bytes into the new string.