github.com/primecitizens/pcz/std@v0.2.1/builtin/map/types.go (about) 1 // SPDX-License-Identifier: Apache-2.0 2 // Copyright 2023 The Prime Citizens 3 // 4 // Copyright 2014 The Go Authors. All rights reserved. 5 // Use of this source code is governed by a BSD-style 6 // license that can be found in the LICENSE file. 7 8 package stdmap 9 10 import ( 11 "unsafe" 12 13 "github.com/primecitizens/pcz/std/core/abi" 14 "github.com/primecitizens/pcz/std/core/arch" 15 ) 16 17 // A HashMap is the standard implementation of Go `map`. 18 type HashMap struct { 19 // Note: the format of the hmap is also encoded in cmd/compile/internal/reflectdata/reflect.go. 20 // Make sure this stays in sync with the compiler's definition. 21 Count int // # live cells == size of map. Must be first (used by len() builtin) 22 Flags uint8 23 B uint8 // log_2 of # of buckets (can hold up to loadFactor * 2^B items) 24 Noverflow uint16 // approximate number of overflow buckets; see incrnoverflow for details 25 Hash0 uint32 // hash seed 26 27 // NOTE: linker checks `buckets` and `oldbuckets` field for dwarf 28 // DO NOT CHANGE THESE NAMES 29 buckets unsafe.Pointer // array of 2^B Buckets. may be nil if count==0. 30 oldbuckets unsafe.Pointer // previous bucket array of half the size, non-nil only when growing 31 Nevacuate uintptr // progress counter for evacuation (buckets less than this have been evacuated) 32 33 Extra *mapextra // optional fields 34 } 35 36 // MapExtra holds fields that are not present on all maps. 37 type MapExtra struct { 38 // If both key and elem do not contain pointers and are inline, then we mark bucket 39 // type as containing no pointers. This avoids scanning such maps. 40 // However, bmap.overflow is a pointer. In order to keep overflow buckets 41 // alive, we store pointers to all overflow buckets in hmap.extra.overflow and hmap.extra.oldoverflow. 42 // overflow and oldoverflow are only used if key and elem do not contain pointers. 43 // overflow contains overflow buckets for hmap.buckets. 44 // oldoverflow contains overflow buckets for hmap.oldbuckets. 45 // The indirection allows to store a pointer to the slice in hiter. 46 overflow *[]*bmap 47 oldoverflow *[]*bmap 48 49 // nextOverflow holds a pointer to a free overflow bucket. 50 nextOverflow *bmap 51 } 52 53 // A bucket for a Go map. 54 type MapBucket struct { 55 // tophash generally contains the top byte of the hash value 56 // for each key in this bucket. If tophash[0] < minTopHash, 57 // tophash[0] is a bucket evacuation state instead. 58 tophash [bucketCnt]uint8 59 // Followed by bucketCnt keys and then bucketCnt elems. 60 // NOTE: packing all the keys together and then all the elems together makes the 61 // code a bit more complicated than alternating key/elem/key/elem/... but it allows 62 // us to eliminate padding which would be needed for, e.g., map[int64]int8. 63 // Followed by an overflow pointer. 64 } 65 66 func (b *MapBucket) setoverflow(t *abi.MapType, ovf *bmap) { 67 *(**MapBucket)(unsafe.Add(unsafe.Pointer(b), uintptr(t.BucketSize)-arch.PtrSize)) = ovf 68 }