0

字节:为什么,这样做有什么用

{ 字节 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;

    ...
}

它来自咖啡因源代码:https ://github.com/ben-manes/caffeine/blob/master/caffeine/src/main/java/com/github/benmanes/caffeine/cache/BoundedLocalCache.java

4

1 回答 1

3

鉴于这是一个并发缓存,填充是为了防止缓存行共享。高速缓存行是由 CPU 加载到高速缓存中的内存单元。实际大小会因 CPU 设计而异,但典型的最小大小为 64 字节。只需使用其中一些术语搜索网络,您就会找到更多信息。

于 2021-08-09T04:11:13.130 回答