Access to the field of the local field and object. Is the document incorrect?

The documentation seems to be wrong. Can someone tell me what is true?

In the "Performance Myths" section :

On devices without JIT, access to the cache window will be 20% faster than re-accessing the field. With JIT, the cost of accessing the field is about the same as the local cost.

B Avoid internal getters / setters :

Without JIT, direct access to a field is about 3x faster than calling a trivial getter. With JIT (where direct access to a field is cheaper than accessing a local one), direct access to fields is 7x faster than calling a trivial getter.

Clearly, without JIT, local access is faster. It is also clear that access to a region is faster when accessing directly than to a getter.

But why in the first case the performance is 20% better, and in the second case the performance is 133% better for the same reason, that is, JIT optimization for the calling object field?

+5
source share
3 answers

I think you are comparing apples and oranges. the Performance Myths link discusses the advantage of JIT for accessing a field, while the second link discusses the advantage of JIT for accessing a method.

, ( , ), , :

class foo {
    int bar = 42;

    int doStuff(){
        int x = 1;
        x += bar;
        x += bar;
        x += bar;
        return x;
    }
}

bar . "" :

int doStuff(){
    int x = 1f;
    int local_bar = bar;
    x += local_bar;
    x += local_bar;
    x += local_bar;
    return x;
}

JIT , 20%.

JIT , JIT bar.

:

class foo {
    int bar = 42;

    int getBar() { return bar; }

    int doStuff(){
        int x = 1;
        x += getBar();
        x += getBar();
        x += getBar();
        return x;
    }
}

. getBar() ( bar), getBar() (.. ). , .

, JIT.

getBar() bar, . JIT 3 , , . JIT , (7x) .

, !

+5

, . :

 caching field accesses is about 20% faster than repeatedly accesssing the field

, JIT . :

int a = this.field;
if (a == 1)
...
if (a == 7) // etc.

,

if (this.field == 1)
....
if (this.field == 7) //etc.

, , .

, JIT, /, , :

if (this.getField()) // etc.

, :

if (this.field) // etc.

, .

+2

, Dalvik. , , . , x% x% , JIT, : (a) 20% , , ( b) JIT' , JIT'd (c) , / JIT. .

VM - , ( , Dalvik). - , ( , ), . , JIT ( , , JIT, - , ) .

, , Dalitik JITs ( ), , , JIT'd: , , . , , , , ( , ). , , .

+1

All Articles