java - Why there is a local variable referencing value array in String#hashCode? -
what purpose of copying value
reference val
variable, oppose using directly?
public int hashcode() { int h = hash; if (h == 0 && value.length > 0) { char val[] = value; // private final char value[]; (int = 0; < value.length; i++) { h = 31 * h + val[i]; } hash = h; } return h; }
this performance optimization according martin buchholz
copying locals produces smallest bytecode
see also:
in arrayblockingqueue, why copy final member field local final variable?
+----------------------+----------------------------+------------------------------+ | h = 31 * h + val[i]; | h = 31 * h + value[i]; | hash = 31 * hash + value[i]; | +----------------------+----------------------------+------------------------------+ | linenumber 1471 l7 | linenumber 14 l6 | linenumber 13 l4 | | bipush 31 | bipush 31 | aload 0 | | iload 1 | iload 1 | bipush 31 | | imul | imul | aload 0 | | aload 2 | aload 0 | getfield string.hash : | | iload 3 | getfield string.value : [c | imul | | caload | iload 2 | aload 0 | | iadd | caload | getfield string.value : [c | | istore 1 | iadd | iload 1 | | | istore 1 | caload | | | | iadd | | | | putfield string.hash : | +----------------------+----------------------------+------------------------------+
Comments
Post a Comment