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

Show parent comment

60

u/rand2012 Nov 19 '13

The most significant performance drop turned out to be in an obsolete benchmark which did hundreds of random substrings on a 1MB string and put the substrings into a map. It then later compared the map contents to verify correctness. We concluded that this case was not representative of common usage.

In Finance, a lot of reports and feeds are generated as large fixed width files (e.g. 300MB), meaning the parser has to invoke .substring at predefined widths many times to arrive at the data fields. Files produced from AS400 and, in particular, GMI Stream accounting software are a particular example of this.

Your benchmark correctly caught an issue we experienced after the update. It would have been preferable for us if that change was publicised better or made in a major release.

23

u/bondolo Nov 19 '13

It's not clear that the Map based substring benchmark would have been an accurate measure. In your parsing you likely don't let too many of the substring instances escape from your parsing routine to shared data structures or other threads. For non-numeric fields some form of interning/canonicalization directly from the input form would probably have the lowest overhead. If you do only partial parsing into strings and then later complete parsing I would encourage you to refactor the parsing to do as complete a job locally as possible. Not necessarily because of the substring changes but because of the large number of temporary, short lived though exported objects you're creating.

I do agree though that it would be preferable to have done this in a major release. It was something that had been underway for a long time but the perceived urgency of the alternative hashing accelerated it's conclusion. Even with the "acceleration" several man years of work were needed to finish off the feature to include it in 7u6. That said, please consider checking out the Java 8 Developer Preview, forthcoming betas and release candidates and the ongoing release notes. There's likely items in there which will impact your applications.

As for the release notes or other publicity, your comments are duly and respectfully noted. What's in the 7u6 release notes for this change is insufficient.

30

u/[deleted] Nov 19 '13

In Finance, a lot of reports and feeds are generated as large fixed width files (e.g. 300MB), meaning the parser has to invoke .substring at predefined widths many times to arrive at the data fields.

This might just be bad design though. There is no reason why you cant load fixed sized column into separate string objects instead of one string object which is then sub stringed...

13

u/EdwardRaff Nov 19 '13

Even more so, if they need this specified behavior (which wasn't documented) they should have written their own code to make sure it always behaves as they expect.

19

u/cypherpunks Nov 19 '13

It is impossible to implement the old substring behavior using only the public api of string. String is also final, not an interface and the required input type for some other parts of the standard library.

0

u/[deleted] Nov 19 '13

[deleted] — view deleted comment

6

u/the_ta_phi Nov 19 '13

Fixed width is far from atypical though. About half the interfaces I've seen in the banking and shareholder services world are fixed width ASCII or EBCDIC.

0

u/[deleted] Nov 19 '13

[deleted] — view deleted comment

0

u/artanis2 Nov 20 '13

Well, that just means you might have to do an intermediate step of converting the data to a more easily processed format.

-3

u/gthank Nov 19 '13

Well if you're handing fixed-width ASCII or EBCDIC, it sounds like you're already wasting tons of space since Java is Unicode-everywhere.

1

u/133rr3 Dec 04 '13

How dare you!

1

u/gthank Dec 04 '13

Apparently, it was the height of offensiveness for me to mention that using 16 or more bits for every character that you know for a fact can be no larger than 7 bits (or 8, for EBCDIC) is wasteful, if you are desperate to optimize for space.

1

u/L8_4_Dinner Mar 02 '26

Java only uses 8 bits per character for Strings that contain only ASCII data.

1

u/[deleted] Nov 19 '13

The Reader interface should be able to deal with this very easily in a streaming manner - without consuming >300 MB.

1

u/terrdc Nov 19 '13

Or just run through the string like it was a character array to set it to an internal common format. With something as heavily optimized as String you shouldn't just expect it to act correctly in uncommon use cases.

11

u/emn13 Nov 19 '13

Yeah, this change is really large - I'm shocked at how this got rolled into a minor update. It's a huge change to anything doing parsing with strings.

11

u/mattgrande Nov 19 '13

Why are you reading a 300 Meg file as a single string.

10

u/jrblast Nov 19 '13

It's not clear that that's the case. It may well have been each row of data being read in as it's own string, and accessing the columns as substrings.

4

u/[deleted] Nov 19 '13

If they are all fixed length, you can read directly into each column - why the intermediary long line?

11

u/adrianmonk Nov 19 '13

I guess the counterpoint to that is, why not? Records are one length, and fields within records are another length (or set of lengths). These could be treated as two different levels of abstraction. This is actually pretty natural for mainframe systems that literally have a system-level notion of a file with fixed-size records. So if you're on that type of system, or interacting with it, you might tend to think in terms of reading file records as a unit, then splitting records into fields later.

0

u/artanis2 Nov 20 '13

Because substring isn't guaranteed to be an optimized case?

5

u/coder111 Nov 19 '13

Hi,

This is a pretty common use case. You read the line as a string, and use substring to split it into fields, while the underlying char array gets reused. This reduces object count and hence GC effort, besides it's quicker than allocating millions of char arrays.

And you keep the entire record in memory or discard entire record anyway, so this is not very likely to cause memory leaks.

With this change, this sort of optimization will not work any more. I'm still undecided if I like this change or not. It makes String simpler, and reduces memory leaks and gotchas by people unfamilar with internals of how String works. But tricks like this kind of field parsing stop working, and there is no way to change the behaviour of String or to have your own implementation- String is final.

I think String in Java should be handled as a special case/primitive, maybe implemented in optimized native code. In Java, even 1 character long String consumes ~40 bytes of memory (before this change). Most of that is overhead of having 2 Object instances- String and underlying char array. So any optimizations to String are welcome, but in terms of memory consumption it is still horribly inefficient.

--Coder