为什么不直接比较e.key而不是将其分配给变量? [英] Why not compare e.key directly instead of assigning it to a variable?
问题描述
在阅读 为什么分配 ---------- --------- UPDATE ------------------------ 据到@seand的答案,我会做更详细的调查: class Test a int filed a; 使用javap -c Main获取类文件内容: 请参阅jvm规范获取[getfield]的信息 访问局部变量比class字段更合理。 我的猜测是这是一个优化,可以为e.key节省额外的查询空间。 (虽然它实际上不是使用invokevirtual的方法调用,但它可以节省一定程度的间接性)。由于这是一个非常常用的库函数,作者可能会使用他们想到的每一个技巧来获得最佳性能。您还可以看到它如何检查 While reading the source code for Why assign ------------------- UPDATE ------------------------ According to the answer from @seand, I do more detail investigation: class Test has a int filed a; Using javap -c Main to get the class file content: Refer to jvm specification get information of [getfield] It seems reasonable to access the local variable than the class field. My guess is it's an optimization which saves an extra lookup to e.key. (Though it's not actually a method call that's using invokevirtual, it may save a level of indirection). Since this is a very heavily used library function the authors likely used every trick they could think of for maximum performance. You can also see how it checks for object identity in 这篇关于为什么不直接比较e.key而不是将其分配给变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! HashMap
的源代码时,我在 public V put(K key,V value)$ c (Entry< K,V> e = table [i]; e!= null; e = e.next){
Object k; (e.hash == hash&&((k = e.key)== key || key.equals(k))){
V oldValue = e.value;
if
e.value = value;
e.recordAccess(this);
返回oldValue;
$ / code> e.key
到 k
用于比较?为什么不直接比较,如:
if(e.hash == hash&&(e.key == key || key.equals(e.key))
import com.test.Test;
public class Main {
public static void main(String [] args){
Test t = new Test();
int a = ta;
int b = a ;
}
}
public static void main(java.lang.String []);
代码:
0:new#2 // class test / Test
3:dup
4:invokespecial#3 < init>:()V
7:astore_1
8:aload_1
9:getfield#4 //现场测试/ Test.a:I
12:istore_2
13:iload_2
14:istore_3
15:返回
int a = ta
表示
8:[加载t对象]
9:[访问字段a]
12:[将值存储为]
int b = a
表示:
13:[加载本地变量]
14: [将值存储到b];
k = e.key
中的对象标识,这可以避免稍微更昂贵的 equals()
电话。HashMap
, I came across this snippet in public V put(K key, V value)
:for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
}
e.key
to k
for comparing? Why not compare directly, like:if (e.hash == hash && (e.key == key || key.equals(e.key))
import com.test.Test;
public class Main {
public static void main(String[] args) {
Test t = new Test();
int a = t.a;
int b = a;
}
}
public static void main(java.lang.String[]);
Code:
0: new #2 // class test/Test
3: dup
4: invokespecial #3 // Method test/Test."<init>":()V
7: astore_1
8: aload_1
9: getfield #4 // Field test/Test.a:I
12: istore_2
13: iload_2
14: istore_3
15: return
int a = t.a
represents 8:[load the t object]
9:[access the field a]
12:[store the value to a]
int b = a
represents:13:[load the local variable]
14:[store the value to b];
k = e.key
which may avoid a slightly more costly equals()
call.