覆盖与查找

发布于 2024-12-02 12:40:39 字数 761 浏览 1 评论 0原文

我正在阅读 android 中的 SparseArray 类,并遇到了以下方法:

public void removeAt(int index) {
    if (mValues[index] != DELETED) {
        mValues[index] = DELETED;
        mGarbage = true;
    }
}

显然这也可以被编写:

public void removeAt(int index) {      Or   public void removeAt(int index) {
    if (mValues[index] != DELETED) {            mValues[index] = DELETED;
        mValues[index] = DELETED;               mGarbage = true;
        if (!mGarbage)                      }
            mGarbage = true;         
    }                                
}                                    

看起来 android 开发人员相信数组查找 mValues[index] 比数组写入快,但变量查找并不比变量写入快。

这是真的吗?它取决于VM,还是编译语言中的常识?

I was reading through the SparseArray class in android, and came across the following method:

public void removeAt(int index) {
    if (mValues[index] != DELETED) {
        mValues[index] = DELETED;
        mGarbage = true;
    }
}

Clearly this could as well has been written:

public void removeAt(int index) {      Or   public void removeAt(int index) {
    if (mValues[index] != DELETED) {            mValues[index] = DELETED;
        mValues[index] = DELETED;               mGarbage = true;
        if (!mGarbage)                      }
            mGarbage = true;         
    }                                
}                                    

It would seem the android developers believed the array lookup mValues[index] was faster than an array write, but the variable lookup wasn't faster than a variable write.

Is this really true? Does it depend on the VM, or is it general knowledge in compiled languages too?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泼猴你往哪里跑 2024-12-09 12:40:39

当然,右侧版本等效 - 因为无论值是否已更改,mGarbage 都会设置为 true。

左边和原来的一样,但是没有意义。

基本上,我认为您错过了检查现有值是否允许 DELETED 的副作用:它允许 mGarbage 设置为 true 如果该方法具有确实有效果。这与从数组读取的性能无关。

Certainly the right-hand side version is not equivalent - because then mGarbage is set to true whether or not the value has changed.

The left-hand side is equivalent to the original, but it's pointless.

Basically I think you've missed the side-effect of checking whether or not the existing value was allows DELETED: it allows mGarbage to be set to true only if the method has actually had an effect. That has nothing to do with the performance of reading from the array.

心如狂蝶 2024-12-09 12:40:39

这在很大程度上取决于虚拟机,我猜想这个特定的代码是针对 Dalvik 虚拟机进行调整的(或者它只是 Apache Harmony 碰巧实现的任何代码)。

要记住的一件事是,写入总是意味着一些与缓存和跨线程交互相关的成本(即,您可能需要内存屏障才能正常工作),而读取则更容易执行。

It depends a lot on the VM and I'd guess that this specific code is tuned for the Dalvik VM (or it's just whatever Apache Harmony happened to implement).

One thing to remember is that a write always implies some cost related to caching and cross-thread interaction (i.e. you might need memory barriers for it to work correctly), while a read is much easier to do.

独木成林 2024-12-09 12:40:39

这个假设可能是正确的,尽管它在很大程度上取决于处理器和 JVM 实现。

一般原因与数组和变量关系不大,而与内存访问模式关系更大:

  • 如果 mGarbage 是当前对象的字段值(无论是在寄存器中),则它很可能被本地缓存或 L1 缓存。您可能只是将对象放入缓存中,以便在几个周期前执行诸如虚拟方法查找之类的操作。当本地缓存某些内容时,读取或写入之间不会有太大区别。
  • mValues[index] 是一个数组查找,不太可能在本地缓存(特别是当数组很大或只是偶尔访问时)。由于锁定/内存争用问题,从非本地缓存读取通常比写入更快,因此只有在可以逃脱的情况下才进行读取才有意义。机器中的核心越多,代码中的并发性越高,这种效果就越强。

The assumption is probably true, although it will depend a lot on the processor and JVM implementation.

The general reason is less to do with arrays vs. variables but more to do with memory access patterns:

  • mGarbage is very likely to be locally cached if it's a field value of the current object, either in a register or L1 cache. You probably just rest the object into cache in order to do something like a virtual method lookup a few cycles ago. There won't be much difference between a read or a write when something is locally cached.
  • mValues[index] is an array lookup that is less likely to be locally cached (particularly if the array is large or only gets accessed sporadically). Reads from non-local caches will usually be faster than writes because of locking / memory contention issues so it makes sense to do a read only if you can get away with it. This effect becomes stronger the more cores you have in your machine and the more concurrency you have in your code.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文