This is a question to better understand the Java JVM.
Let's say we have class with an array of 16 bytes. It could more or less than 16. Just 16 as an example. Each byte is used as a collection of 8 flags. Lots of reads. Few writes.
What is the performance impact of creating 4 int fields by fusing 4 bytes into ints
fields. This removes the need for array bound checks when reading those flags. However you need to shift input flags to address the correct bits in the int
.
Flag writes are done both on the byte array and the ints
cache.
Reason might be the byte array is used elsewhere. We don't know. We just know we have those bytes and we have to access flags on it many many times.
Is it worth it to introduce the int
cache in the class implementation?
EDIT: I realized writes by others will not be pushed to the int
cache. So the cache will give false flags. But let's imagine, there is a system wide event for cache refresh.