久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

為什么不直接比較 e.key 而不是將其分配給變量

Why not compare e.key directly instead of assigning it to a variable?(為什么不直接比較 e.key 而不是將其分配給變量?)
本文介紹了為什么不直接比較 e.key 而不是將其分配給變量?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

在閱讀HashMap的源代碼時,我在public V put(K key, V value)中看到了這個片段:

While reading the source code for 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 分配給 k 進行比較?為什么不直接比較,比如:

Why assign e.key to k for comparing? Why not compare directly, like:

if (e.hash == hash && (e.key == key || key.equals(e.key))

-------- 更新 ------------

------------------- UPDATE ------------------------

根據@seand的回答,我做了更詳細的調查:

According to the answer from @seand, I do more detail investigation:

import com.test.Test;

    public class Main {
        public static void main(String[] args) {
            Test t = new Test();
            int a = t.a;
            int b = a;
        }
    }

類 Test 有一個 int 字段;

class Test has a int filed a;

使用 javap -c Main 獲取類文件內容:

Using javap -c Main to get the class file content:

  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 表示

8:[load the t object]
9:[access the field a]
12:[store the value to a]

參考jvm規范獲取[getfield的信息]

Refer to jvm specification get information of [getfield]

int b = a 表示:

13:[load the local variable]
14:[store the value to b];

訪問局部變量似乎比訪問類字段更合理.

It seems reasonable to access the local variable than the class field.

推薦答案

我猜這是一種優化,可以節省對 e.key 的額外查找.(雖然它實際上不是使用invokevirtual 的方法調用,但它可以節省一定程度的間接性).由于這是一個使用非常頻繁的庫函數,作者可能會使用他們能想到的所有技巧來獲得最大性能.您還可以看到它如何在 k = e.key 中檢查對象身份,這可以避免成本稍高的 equals() 調用.

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 k = e.key which may avoid a slightly more costly equals() call.

這篇關于為什么不直接比較 e.key 而不是將其分配給變量?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Convert List of Strings into Map using Java-8 Streams API(使用 Java-8 Streams API 將字符串列表轉換為 Map)
Getting data from JSON(從 JSON 獲取數據)
java linkedhashmap iteration(javalinkedhashmap迭代)
Converting a list of objects to Map(將對象列表轉換為 Map)
Create a HashMap with a fixed Key corresponding to a HashSet. point of departure(用一個固定的Key對應一個HashSet創建一個HashMap.出發點)
HttpMessageConverter exception : RestClientException: Could not write request: no suitable HttpMessageConverter found(HttpMessageConverter 異常:RestClientException:無法寫入請求:找不到合適的 HttpMessageConverter) - IT屋-程序員
主站蜘蛛池模板: 国内久久 | 91在线一区 | 天堂男人av | 欧美色性| 色视频在线免费观看 | 国产精品久久久久无码av | 亚洲一区中文字幕 | 久久久久久国产精品免费免费男同 | 欧美午夜精品理论片a级按摩 | 亚洲综合成人网 | 亚洲一区二区三区欧美 | 国产精品久久久久久婷婷天堂 | 亚洲一区二区三区四区在线观看 | 久久91精品国产一区二区三区 | 日日操视频| 欧美最猛黑人xxxx黑人 | 亚洲免费成人 | 欧美精品久久久 | 欧美日韩在线综合 | 欧美成人免费在线视频 | 99伊人网| 毛片片| 天天夜碰日日摸日日澡 | 精品伊人| 久久影院一区 | 亚洲二区视频 | 国产亚洲精品久久久优势 | 国产黄色av网站 | 亚洲人成人一区二区在线观看 | 成人精品一区二区户外勾搭野战 | 91精品国产91久久久 | 99精品视频一区二区三区 | 欧美日韩在线免费观看 | 奇米av| 黄色网址在线免费播放 | 国产九九精品视频 | 欧美日韩一区二区电影 | 欧美日韩精品中文字幕 | 国产一区二区三区视频在线观看 | 午夜www| 91高清视频在线观看 |