HashMap of the Go language

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

The data structure of map in the go language, the following only a few key attributes are extracted:

struct Bucket{    uint8  tophash[BUCKETSIZE]; // top 8 bits of hash of each entry (0 = empty)    Bucket *overflow;           // overflow bucket, if any    byte   data[1];             // BUCKETSIZE keys followed by BUCKETSIZE values};  struct Hmap{uint8   B;            // log_2 of # of buckets (can hold up to LOAD * 2^B items)    byte    *buckets;     // array of 2^B Buckets. may be nil if count==0.    byte    *oldbuckets;  // previous bucket array of half the size, non-nil only when growing};

The low part of the hash value of key determines the index position of key in buckets.
When the corresponding bucket overflows, the new item is inserted through the overflow in the bucket.

Grow Bucket

When the number of elements in the entire hmap exceeds 65% of the bucket number, the buckets are allocated twice times, and then the elements in each bucket are split into new buckets.
Moving the elements in the old bucket into the new bucket is a gradual process that preserves the old buckets, which brings the process of traversing the map, as well as inserting and deleting, a feature that many other languages do not have.

Shrink Bucket

Hmap also does not support bucket shrink, which causes some programs to use a very large number of maps in a short period of time, after which memory cannot be effectively released.

Related Article

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.