9

Looking java's hashmap implementation, not able to understand the reason behind some lines. In below code copied from here, in line 365-367, I am not able to understand why have they done the assignment of e.key to k first and then compared == with key [ (k = e.key) == key ]. Why not directly do (e.key == key) . This pattern appears several times in code.

359 
360     final Entry<K,V> getEntry(Object key) {
361         int hash = (key == null) ? 0 : hash(key.hashCode());
362         for (Entry<K,V> e = table[indexFor(hash, table.length)];
363              e != null;
364              e = e.next) {
365             Object k;
366             if (e.hash == hash &&
367                 ((k = e.key) == key || (key != null && key.equals(k))))
368                 return e;
369         }
370         return null;
371     }
Pierre Henry
  • 16,658
  • 22
  • 85
  • 105
Nitiraj
  • 614
  • 1
  • 4
  • 15
  • Well, they use the value of k in the other part of the condition, but they could have written `(e.key == key || (key != null && key.equals(e.key)))` instead and eliminated the `k` variable. I don't see what's the advantage of introducing that local variable. – Eran Apr 27 '15 at 07:02
  • Exactly, but in many similar situations throughout the code they have religiously followed creating a new local variable. – Nitiraj Apr 27 '15 at 08:35
  • 4
    See http://stackoverflow.com/questions/10180656/why-is-lock-captured-to-a-local-variable , http://stackoverflow.com/questions/2785964/in-arrayblockingqueue-why-copy-final-member-field-into-local-final-variable , http://stackoverflow.com/questions/28975415/why-jdk-code-style-uses-a-variable-assignment-and-read-on-the-same-line-eg-i ... – Marco13 Apr 27 '15 at 13:01

2 Answers2

2

This is probably a matter of optimization. Calling e.key adds a level of indirection (as you use the reference on e to get a reference on key). The variable k allows to have a shortcut for e.key and avoids using this unnecessary indirection twice. The implementation also directly use the result of the assignation k = e.key instead of assigning the value to k and then compare k to key.

I do not know if the impact of such an optimization is significant (assigning a new variable vs indirect access). It is probably tricky to evaluate, and it may be dependent on the JVM (as different JVM may perform different optimization on the code).

As HashMap is widely used in Java, I guess the implementation aims to offer maximum performance without expecting any optimization from the execution environment; hence, the common use of this pattern.

archz
  • 1,073
  • 13
  • 19
0

I think @marco13 has posted the right links which answers this question. Just posting the links again. Why is lock captured to a local variable

In ArrayBlockingQueue, why copy final member field into local final variable?

Why jdk code style uses a variable assignment and read on the same line - eg. (i=2) < max

Answer : Using the function local reference of member variable decreases the size of byte code generated.

Community
  • 1
  • 1
Nitiraj
  • 614
  • 1
  • 4
  • 15