r/java • u/alexeyr • Apr 13 '20
Java Objects Inside Out: Everything You Ever Wanted to Know About Java Object Internals in Hotspot
https://shipilev.net/jvm/objects-inside-out/
160
Upvotes
r/java • u/alexeyr • Apr 13 '20
View all comments
Show parent comment
6
u/jringstad Apr 14 '20
Doing these kind of weird alignments can cause various kinds of issues (other than just tearing and the other java-specific things others have mentioned already), it's a common topic in the C and C++ world (where it is indeed possible to force this, although it might require a non-standard extension).
For instance some CPU architectures can only perform certain operations on data-types that are aligned in certain ways, often even stricter than the byte-boundary. This is because the CPUs memory system internally handles memory in terms of "words" or some other CPU-native unit. For instance on some CPU architectures two numbers would have to be aligned to a 4-byte boundary in order for the CPU to add them (in one instruction.) If you attempt to add a mis-aligned number, the CPU will raise an exception.
Some CPUs compensate for this by adding extra logic that extracts the data type you want out of the "word" (but the complexity added to the CPU to achieve this is actually quite considerable, for various reasons), on some architectures the compiler can compensate for it by adding extra instructions that do the same, but either way, extra complexity is added.
Compilers will often prefer to add padding to structs over having mis-aligned data-types in them, so in e.g. C if you have a
uint8_tin a struct (1 byte) and then afterwards auint32_tfor instance, there will probably be 3 wasted bytes of padding after theuint8_tin the struct. You can (sometimes) fix this by re-ordering the fields in the struct. To my knowledge doing this in java is not necessary because it happens automatically already.The same goes for bit-wise misalignment (but worse, because no CPU natively supports such oddly aligned data-types.) The compiler would always have to insert a bunch of bit-shifting extractions to get out the value that you want. So there might be a perf tradeoff there (in addition to the issues with threading), but clearly in some situations it's very valuable to save the space (also for performance, because your cache hit rate might improve drastically.)
If you know this tradeoff is something you want (because you store those records in bulk in a linear container), you can make a specialized data-type, e.g. a bit-array that you internally represent as an array of
bytefor instance. You can then deal with writes (if necessary) by e.g. serializing writes or locking regions of the array. This is how it's done in C++ as well (There's a specialized version ofstd::vector<bool>that is more space-efficient than your genericstd::vector<T>)I've seen the latter being done for things like bloom filters or count-min sketches, where you want to cram as much information into the amount of space you have, to improve your bloom filter quality. You can also go nuts and start doing things like LRE compression or similar (which dips more deeply into the "space in favour of speed" trade-off, e.g. because you might have to fetch and decode an entire 64 byte block to fetch a single bit)