r/java • • Apr 30 '20

How much memory was wasted when an additional boolean field was added to java.lang.String in Java 13? None at all.

https://www.javaspecialists.eu/archive/Issue278.html
205 Upvotes

12 comments sorted by

36

u/zeldaccordion Apr 30 '20

Love how this post proved its claim with simple object introspection results in an easy to understand way. Never knew about 8 byte object alignment before, but it makes total sense now that it’s spelled out.

31

u/cl4es Apr 30 '20

Nice write-up!

And no, we wouldn't have added the hashIsZero field if it wasn't footprint neutral, which was checked thoroughly using JOL.

2

u/kaperni Apr 30 '20

Any customer stories about people actually gotten bitten by this?

7

u/cl4es Apr 30 '20

None that come to mind, no. I think of this particular enhancement as a defense in-depth against performance surprises, rather than an optimization customers are likely to observe.

6

u/Hangman4358 May 01 '20

Looks like there are still 2 more bytes of functionality to cram into String!

4

u/[deleted] Apr 30 '20

But if its "the unlikely case that it was 0" why even bother with it? It still takes time to check it.

6

u/morph23 Apr 30 '20

I think 0 is the initial value of the stored hash, before the hash is computed the first time. So if the stored hash is 0, the hash will be recomputed and stored. But it's just 0 again, so this flag will just prevent unnecessary recomputes.

1

u/[deleted] May 01 '20

Then maybe call it doesTheHashNeedToBeComputed

4

u/cl4es May 01 '20 edited May 01 '20

Well... it doesn't measurably add any time.

If hash has already been computed to a non-zero value, hashIsZero will not be checked at all.

If hash hasn't yet been computed, hashIsZero is read and checked. But this field shares a cache line with the other fields in String, which we'll be reading anyhow. The expensive part is almost always getting the data into memory. But yes, we are paying an instruction or three to decode and test the hashIsZero field here. This cost will likely disappear into the noise as we start paying the price of loading the value byte array into memory and start calculate the hash. Factor in the branch predictor, and that tiny cost (a few nanoseconds?) is potentially eliminated completely - which is what we see on benchmarks.

1

u/wishper77 May 01 '20

I always wondered why no one ever thought about doing a framework to store every field inside a single bytearray and encode/decode to real object using setters and getters. This way booleans would take 1 bit, not 8 bytes lol (anyone know if such framework exists?)

2

u/gunnarmorling May 01 '20

Not exactly a framework, but we applied this exact technique in one of the most often allocated classes of Hibernate ORM, EntityEntry: https://github.com/hibernate/hibernate-orm/blob/2c12ca401dcc55b296522a54135169c6e5fe57e4/hibernate-core/src/main/java/org/hibernate/engine/internal/AbstractEntityEntry.java#L55-L79. A single int is used to store multiple enums and booleans. Access to the state encoded in the int is encapsulated via some typed accessor methods. This change a few years back helped quite a bit to reduce memory allocation rates for ORM.

1

u/TheStrangeDarkOne May 01 '20

because the additional layer of indirection is probably much higher in cost than a slightly higher memory footprint.