1

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.

Mordan
  • 337
  • 3
  • 12
  • 1
    This smells like a premature optimization to me. – Elliott Frisch Feb 03 '17 at 05:02
  • [`BitSet`](https://docs.oracle.com/javase/8/docs/api/java/util/BitSet.html) uses a `long[]` to store the bits internally. – 4castle Feb 03 '17 at 05:05
  • Some `int` fields instead of a `byte` array seems very unlikely to achieve any performance improvement. The JVM will need to calculate an offset into the byte array, but it will also need to perform about equivalent work to find the correct `int` field. – WW. Feb 03 '17 at 05:15
  • @WW. That's the kind of answer I was looking for. Internally for the JVM, finding an int field is about the same as looking up a byte array offset. Could anyone confirm this? I am no JVM expert. However that would contradict the implementation of BitSet as 4castle correctly mentioned. – Mordan Feb 03 '17 at 07:41
  • @4castle appreciated. – Mordan Feb 03 '17 at 07:41
  • that approach may help with bulk operations, but probably not with individual lookups. – the8472 Feb 03 '17 at 08:56
  • I don’t see the contradiction. `BitSet` uses an array, not fields, so it’s unaffected by the statement that using fields is unlikely to outperform an array. Using a `long` array instead of an `int` or `byte` array, on the other hand, has its merits, depending on the actual operation. See [here](http://stackoverflow.com/a/32115681/2711488). – Holger Feb 03 '17 at 16:07

0 Answers0