0

In Java in-memory there is no difference between byte or int - both will be represented as 4 bytes.

Does for Chronicle Map the difference exist, i.e. does Chronicle Map store byte values as 8 bits or still use 32?

Same question if byte is an object property.

leventov
  • 14,760
  • 11
  • 69
  • 98

2 Answers2

0

I think I know the response. At least at the version 2.3.8 offheap value will be 1 byte for a byte (work done in SerializationBuilder class).

0

In primitive map implementations (fastutil, koloboke, gs, hppc) byte values are implemented as a separate byte[] array, so they actually take only 1 byte. If a byte is a field of another on-heap Java object (which is a Map value), indeed, the object size is rounded up to 8-byte boundary, so a single byte field could "take" 8 bytes. But more often, it "takes" 0 bytes, because the field is placed in the already existing alignment holes.

For Chronicle Map, a value could freely be 1 byte in size. (And even 0 bytes, this is how ChronicleSet is currently implmeneted -- a ChronicleMap with 0-byte dummy values.) This is true for all Chronicle Map versions (2, 3).


Edit -- answer to the comment.

If you have a constantly sized structure e. g. 6 byte fields, easiest and efficient way - to use data value generation mechanishm:

interface MyValue {
    byte getA(); void setA(byte a);
    byte getB(); void setB(byte b);
    byte getC(); void setC(byte c);
    byte getD(); void setD(byte d);
    byte getE(); void setE(byte e);
    byte getF(); void setF(byte f);
}

map = ChronicleMapBuilder.of(Key.class, MyValue.class).entries(1000).create();

// Chronicle Map 2 syntax
MyValue value = DataValueClasses.newDirectReference(MyValue.class);
try (Closeable handle = map.getUsingLocked(key, value)) {
    // access the value here
    System.out.println(value);
}

// Chronicle Map 3 syntax
try (ExternalMapQueryContext<Key, MyValue, ?> q = map.queryContext(key)) {
    // if not sure the key is present in the map, check q.entry() != null
    MyValue value = q.entry().value().get();
    // access the value here
    System.out.println(value);
}

It will take exactly 6 bytes per value.

leventov
  • 14,760
  • 11
  • 69
  • 98
  • I assume there is no byte <-> bit confusion above. In this case Object with 6 fields of type 'byte' will consume (in the worst case) 8 bytes of offheap memory (talking about ChronicleMap here). Correct? – Konstantin Kulagin Oct 22 '15 at 20:18
  • Thanks Roman, the only confusion I have is this phrase: _indeed, the object size is rounded up to 8-byte boundary, so a single byte field could "take" 8 bytes._ In this case it was not rounded up to 8 bytes? – Konstantin Kulagin Oct 22 '15 at 21:18
  • It's Java heap object alignment. Chronicle Map is off-heap. If I understand your question correctly. – leventov Oct 22 '15 at 22:02
  • Yes, that was an original question (pure offheap one), i did not realize you are talking about onheap in response as well. Nevermind. Thanks for the response! – Konstantin Kulagin Oct 23 '15 at 14:56