Source file src/internal/abi/map_swiss.go

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package abi
     6  
     7  import (
     8  	"unsafe"
     9  )
    10  
    11  // Map constants common to several packages
    12  // runtime/runtime-gdb.py:MapTypePrinter contains its own copy
    13  const (
    14  	// Number of bits in the group.slot count.
    15  	SwissMapGroupSlotsBits = 3
    16  
    17  	// Number of slots in a group.
    18  	SwissMapGroupSlots = 1 << SwissMapGroupSlotsBits // 8
    19  
    20  	// Maximum key or elem size to keep inline (instead of mallocing per element).
    21  	// Must fit in a uint8.
    22  	SwissMapMaxKeyBytes  = 128
    23  	SwissMapMaxElemBytes = 128
    24  
    25  	ctrlEmpty = 0b10000000
    26  	bitsetLSB = 0x0101010101010101
    27  
    28  	// Value of control word with all empty slots.
    29  	SwissMapCtrlEmpty = bitsetLSB * uint64(ctrlEmpty)
    30  )
    31  
    32  type SwissMapType struct {
    33  	Type
    34  	Key   *Type
    35  	Elem  *Type
    36  	Group *Type // internal type representing a slot group
    37  	// function for hashing keys (ptr to key, seed) -> hash
    38  	Hasher    func(unsafe.Pointer, uintptr) uintptr
    39  	GroupSize uintptr // == Group.Size_
    40  	SlotSize  uintptr // size of key/elem slot
    41  	ElemOff   uintptr // offset of elem in key/elem slot
    42  	Flags     uint32
    43  }
    44  
    45  // Flag values
    46  const (
    47  	SwissMapNeedKeyUpdate = 1 << iota
    48  	SwissMapHashMightPanic
    49  	SwissMapIndirectKey
    50  	SwissMapIndirectElem
    51  )
    52  
    53  func (mt *SwissMapType) NeedKeyUpdate() bool { // true if we need to update key on an overwrite
    54  	return mt.Flags&SwissMapNeedKeyUpdate != 0
    55  }
    56  func (mt *SwissMapType) HashMightPanic() bool { // true if hash function might panic
    57  	return mt.Flags&SwissMapHashMightPanic != 0
    58  }
    59  func (mt *SwissMapType) IndirectKey() bool { // store ptr to key instead of key itself
    60  	return mt.Flags&SwissMapIndirectKey != 0
    61  }
    62  func (mt *SwissMapType) IndirectElem() bool { // store ptr to elem instead of elem itself
    63  	return mt.Flags&SwissMapIndirectElem != 0
    64  }
    65  

View as plain text