Hashcode-related performance optimization

Source: Internet
Author: User

Learn the hashcode performance optimization techniques used in hashmap as notes to lay the foundation for subsequent concurrency.

1. Hash Algorithms for improving performance

When the number of digits to be modeled is 2, bitwise and replace the inefficient modulo operation are used. Bitwise AND efficiency are more efficient than modulo operations.
Example: 15% 4 = 3, instead of 15 & 3 = 1111 & 0011 = 0011 = 3

After obtaining a key in hashmap, you need to find the entry Array under which the operation is as follows:

Static int indexfor (int h, int length ){
Return H & (length-1 );
}

Example:
Two keys. The values after calling the object hash method are:
32, 64, and the entry array size is 16, that is, when indexfor is called, the parameters are [32, 15], [64, 14],
In this case, the indexfor method is called respectively for them:
32. computing process:
100000 & 1111 => 000000 => 0
64 The calculation process is as follows:
1000000 & 1111 => 000000 => 0

You can see that indexfor only performs operations on the low position when the entry array size is not very large. The high value is not involved in the operation (if the entry size is 32, it will only perform operations with the lower five BITs), and it is prone to hash conflicts.

Here, both hash values 32 and 64 are stored at the position 0 of the entry array.


To solve this problem, before performing the indexfor operation, hashmap needs to call the hash method so that the bit values of hash values are evenly distributed at a high or low level. The hash method is as follows:
Static int Hash (INT h ){
// This function ensures that hashcodes that differ only
// Constant multiples at each bit position have a bounded
// Number of collisions (approximately 8 at default load factor ).
H ^ = (H >>> 20) ^ (H >>>> 12 );
Return H ^ (H >>> 7) ^ (H >>> 4 );
}

Or follow the previous key, after the object hash method, respectively for 32, 64 operations:
32. The hash operation is called as follows:
The original H is a binary value of 32:
100000
H >>> 20:
000000
H >>> 12:
000000

Then calculate H ^ (H >>> 20) ^ (H >>>> 12 ):
Result: 100000

Then calculate: H ^ (H >>> 7) ^ (H >>> 4 ),
The process is as follows:
H >>> 7: 000000
H >>> 4: 000010
Final calculation: H ^ (H >>> 7) ^ (H >>> 4 ),
Result: The value is 100010 in decimal format.

Call the indexfor method:
100010 & 1111 => 2, that is, it is stored at the position of entry array subscript 2
------------------------------------

The result of 64 operations is: 1000100, And the decimal value is 68.
Call the indexfor method:
1000100 & 1111 => 4, which is stored at the position of entry array subscript 4

You can see that after the hash method, you can call the indexfor method to reduce conflicts.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.