Algorithm 6-4: Hash Table status quo

Source: Internet
Author: User

War stories


A long time ago, there were many war stories about hash functions. The basic principle of those wars is to carefully construct a large number of hash conflicts to occupy a large amount of CPU resources.



The following is an example of the software under attack:

  • Servers with vulnerabilities: attackers carefully construct ha conflicts, and only need a K network speed to crash the server to achieve DOS attacks.

  • Perl 5.8.0: attackers carefully construct ha conflicts and insert them to the associated array.

  • Linux 2.4.20 kernel: attackers carefully construct file names, resulting in a large number of HA conflicts, resulting in a sudden drop in system performance.


    Attack principles


    In Java, the String object is easy to construct ha system conflicts. Demonstrate examples of HA conflicts in Java.



    Solution


    <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + Csq508O4/LzTuN + signature/Signature + u0wdXiwO + signature + cve-vcd4kpha + signature + qz/Signature + signature/Signature + CjxwPgo8YnI + cda-vcd4kpha + CrbAwaLBtLHto7o8L3A + Cjx1bCBjbGFzcz0 = "list-paddingleft-2">

  • Easy to delete Elements

  • As the data volume increases, performance decreases slowly

  • Ha conflicts have little impact on the system.


    Linear probes:

    • Use less memory because there is no linked list

    • Better cache Performance


      Improvement of Hash Functions


      At present, many different hash algorithms have been implemented.


      Dual-value hash:

      A hash function returns two hash values, which are inserted into a short chain when an element is inserted. This method can reduce the expected length of the chain.


      Double hash:

      The linear probe method is used, but different numbers of elements are skipped after each conflict to find the vacancy. This method can eliminate consecutive placeholder values, so that the hash table can be almost filled up, But deletion is difficult to implement.


      Cuckoo hash:

      First generate a hash and calculate a location. If there is a conflict, add some parameters to continue the hash and calculate another location. Until the vacant position is found. In the worst case, the complexity of this method is N.


      Comparison between HA table and binary tree


      Both the hash table and the balance tree can be associated with arrays.


      Hash table:

      • Simple code

      • This method has the best performance

      • This method is faster for simple keywords.

      • It has better system support in java. For example, the hashCode cache is used in String.


        Binary Tree:

        • Better Performance Assurance

        • Supports sequential operations (obtain the ranking of an element, the nth value, and the number of elements X to Y)

        • CompareTo functions are easier to implement than hashCode functions and are not prone to errors.


          Both methods are implemented in the Java library. Java. util. TreeMap java. util. TreeSet is implemented through the red/black tree, and java. util. HashMap java. util. IdentityHashMap is implemented through the hash table.


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.