Configuring java classes for the convenience of the processor cache

When developing Java classes, what are the best practices for keeping the processor cache usable?

What I have learned so far is that you need to use POD as much as possible (i.e. int instead of integer). Thus, the data will be allocated sequentially when distributing the containing object. For instance.

class Local
{
    private int data0;
    private int data1;
    // ...
};

caches more than

class NoSoLocal
{
    private Integer data0;
    private Integer data1;
    //...
};

The latter will require two separate distributions for Integer objects, which can be in arbitrary places in memory, especially. after starting the GC. OTOH first approach can lead to duplication of data in cases where data can be reused.

, " " , GC

+4
2

, .

, . int - 4 , 4 , , Integer. .

, " " , GC

GC , . , .

: , , . HotSpot GC . , .

2: 4 int - , 28 Integer (4 , 16 , 4 4 )

3: , , . int , integer . , null, int, , .

+1

JVM ( JVM ). , Java .

.

! Java!
, , .

  • . " " . . , . JDK 8 Adder Accumulator, Striped64.

  • . , . :

    class Entry {
        long key;
        Object value;
    }
    
    Entry[] entries;
    

    long[] keys;
    Object[] values;
    
  • . - 160- SHA1-, byte[]. :

    class Blob {
        long offset;
        int length;
        byte[] sha1_hash;
    }
    

    :

    class Blob {
        long offset;
        int length;
        int hash0, hash1, hash2, hash3, hash4;
    }
    
  • String char[]. , String Java char[] . ?

  • . . . LinkedList ArrayList. HashMap - .

  • . Trove - , , , .. .

  • ByteBuffers. - - . , .

+5

Source: https://habr.com/ru/post/1545681/


All Articles