0

byte: Why, what's the use of doing this

{ byte p000, p001, p002, p003, p004, p005, p006, p007; ... }

/** The namespace for field padding through inheritance. */
final class BLCHeader {
  abstract static class PadDrainStatus<K, V> extends AbstractMap<K, V> {
    byte p000, p001, p002, p003, p004, p005, p006, p007;
    byte p008, p009, p010, p011, p012, p013, p014, p015;
    byte p016, p017, p018, p019, p020, p021, p022, p023;
    
    ...
    byte p088, p089, p090, p091, p092, p093, p094, p095;
    byte p096, p097, p098, p099, p100, p101, p102, p103;
    byte p104, p105, p106, p107, p108, p109, p110, p111;
    byte p112, p113, p114, p115, p116, p117, p118, p119;
  }

  /** Enforces a memory layout to avoid false sharing by padding the drain status. */
  abstract static class DrainStatusRef<K, V> extends PadDrainStatus<K, V> {
    static final VarHandle DRAIN_STATUS;

    ...
}

it comes from caffeine source code:https://github.com/ben-manes/caffeine/blob/master/caffeine/src/main/java/com/github/benmanes/caffeine/cache/BoundedLocalCache.java

ke xu
  • 21
  • 1
  • 1
    Without a context it's hard to guess. What are `BLCHeader` and `PadDrainStatus`? Please, add more details to your question. – Vadik Sirekanyan Aug 09 '21 at 03:47
  • 1
    it comes from caffeine source code:https://github.com/ben-manes/caffeine.git ; com.github.benmanes.caffeine.cache.BLCHeader; it may be used to record cache status – ke xu Aug 09 '21 at 03:58
  • https://github.com/ben-manes/caffeine/blob/master/caffeine/src/main/java/com/github/benmanes/caffeine/cache/BoundedLocalCache.java – ke xu Aug 09 '21 at 03:58

1 Answers1

3

Given that this is a concurrent cache, the padding is there to prevent cache line sharing. A cache line is the unit of memory that's loaded into the cache by the CPU. The actual size will vary by CPU design, but a minimum size of 64 bytes is typical. Just search the net using some of these terms and you'll find more information.

boneill
  • 1,478
  • 1
  • 11
  • 18
  • 1
    Well said. This specific incarnation for the JVM comes from @aleksey-shipilev's article [Java Objects Inside Out](https://shipilev.net/jvm/objects-inside-out/#_observation_hierarchy_tower_padding_trick_collapse_in_jdk_15) – Ben Manes Aug 09 '21 at 05:39