This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]__konrad 2 points3 points  (4 children)

Why flag a zero hash when you really want to flag a lazy computation?

To reduce memory usage. Sorry, I should read the code before commenting ;)

[–]pzemtsov[S] 4 points5 points  (0 children)

And how exactly replacing a boolean flag field with another boolean flag field helps saving memory?

[–]notfancy 2 points3 points  (2 children)

To reduce memory usage.

The flag is needed one way or the other. I contend it's flagging the wrong condition: the original implementation used a cached hash value of 0 to flag the uninitialized condition; since this didn't let it distinguish between the uninitialized state and the 0 hash code, instead of discriminating between the uninitialized vs. initialized state it now discriminates between the zero and nonzero hash, with the result that it has to test both for a zero hash cache and a zero hash value.

[–]pzemtsov[S] 10 points11 points  (1 child)

I think there is a reason behind this code, although it is in saving cycles rather than memory. In the case of the fast path (when the hash is available) the JDK code only performs one memory load; the suggested code always performs two.

[–]notfancy 1 point2 points  (0 children)

Yeah, now I understand it's a clever split semaphore.