调整用于CPU缓存友好性的Java类 [英] Tweaking java classes for CPU cache friendliness

查看:119
本文介绍了调整用于CPU缓存友好性的Java类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在设计java类时,有什么建议可以实现CPU缓存的友好性?



到目前为止,我学到的是尽可能使用POD即int而不是整数)。因此,分配包含对象时,数据将被连续分配。例如:

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

比缓存更友好

  class NoSoLocal 
{
private Integer data0;
私人整数数据1;
// ...
};

后者将需要对Integer对象进行两次单独的分配,这些分配可以位于内存中的任意位置,esp 。 GC运行后。 OTOH第一种方法可能会导致数据重复使用情况下的数据。



有没有办法让它们在内存中彼此靠近以便父对象及其包含的元素将一次存储在CPU缓存中,而不是随意分布在整个内存中,再加上GC会使它们保持在一起?

解决方案




第一种方法可能导致数据重复使用。 $ b

但是在你提到的情况下。一个 int 是4个字节,引用通常是4个字节,所以你不用通过Integer获得任何东西。对于一个更复杂的类型,它可以产生很大的差别。然而,


有没有办法让它们在内存中彼此靠近所以父对象及其包含的元素将一次存储在CPU缓存中,而不是任意分布在整个内存中,再加上GC会使它们保持在一起?

无论如何,GC都会这样做,只要这些对象只在一个地方使用。如果对象在多个地方使用,它们将接近一个引用。



注意:这不能保证是这种情况,但是当分配对象时,它们通常会由于这是最简单的分配策略,所以在内存中是连续的。复制保留的对象时,HotSpot GC将以与发现相反的顺序复制它们。注意2:对于 int 使用4个字节仍然在使用中比使用整数的28个字节(4个字节用于引用,16个字节用于对象头,4个字节用于值,4个字节用于填充)效率更高

注3 :最重要的是,除非您已经衡量了您的需求并拥有更高性能的解决方案,否则您应该更加关注性能的清晰度。在这种情况下, int 不能为空,但整数可以为null。如果你想要一个不应该是 null 的值,可以使用 int ,但不是为了表现,而是为了清晰。


When designing java classes, what are the recommendations for achieving CPU cache friendliness?

What I have learned so far is that one should use POD as much as possible (i.e. int instead of integer). Thus, the data will be allocated consecutively when allocating the containing object. E.g.

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

is more cache friendly than

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

The latter will require two separate allocations for the Integer objects that can be at arbitrary locations in memory, esp. after a GC run. OTOH the first approach might lead to duplication of data in cases where the data can be reused.

Is there a way to have them located close to each other in memory so that the parent object and its' containing elements will be in the CPU cache at once and not distributed arbitrarily over the whole memory plus the GC will keep them together?

解决方案

the first approach might lead to duplication of data in cases where the data can be reused.

But not in the case you mention. An int is 4 bytes, a reference is typically 4-bytes so you don't gain anything by using Integer. For a more complex type, it can make a big difference however.

Is there a way to have them located close to each other in memory so that the parent object and its' containing elements will be in the CPU cache at once and not distributed arbitrarily over the whole memory plus the GC will keep them together?

The GC will do this anyway, provided the objects are only used in one place. If the objects are used in multiple places, they will be close to one reference.

Note: this is not guaranteed to be the case, however when allocating objects they will typically be continuous in memory as this is the simplest allocation strategy. When copying retained objects, the HotSpot GC will copy them in reverse order of discovery. i.e. they are still together but in reverse order.

Note 2: Using 4 bytes for an int is still going to be more efficient than using 28 bytes for an Integer (4 bytes for reference, 16 bytes for object header, 4 bytes for value and 4 bytes for padding)

Note 3: Above all, you should favour clarity over performance, unless you have measured you need and have a more performant solution. In this case, an int cannot be a null but an integer can be null. If you want a value which should not be null use int, not for performance but for clarity.

这篇关于调整用于CPU缓存友好性的Java类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆