github.com/wI2L/jettison@v0.7.5-0.20230106001914-c70014c6417a/map.go (about) 1 package jettison 2 3 import ( 4 "bytes" 5 "sync" 6 "unsafe" 7 ) 8 9 var ( 10 hiterPool sync.Pool // *hiter 11 mapElemsPool sync.Pool // *mapElems 12 ) 13 14 // kv represents a map key/value pair. 15 type kv struct { 16 key []byte 17 keyval []byte 18 } 19 20 type mapElems struct{ s []kv } 21 22 // releaseMapElems zeroes the content of the 23 // map elements slice and resets the length to 24 // zero before putting it back to the pool. 25 func releaseMapElems(me *mapElems) { 26 for i := range me.s { 27 me.s[i] = kv{} 28 } 29 me.s = me.s[:0] 30 mapElemsPool.Put(me) 31 } 32 33 func (m mapElems) Len() int { return len(m.s) } 34 func (m mapElems) Swap(i, j int) { m.s[i], m.s[j] = m.s[j], m.s[i] } 35 func (m mapElems) Less(i, j int) bool { return bytes.Compare(m.s[i].key, m.s[j].key) < 0 } 36 37 // hiter is the runtime representation 38 // of a hashmap iteration structure. 39 type hiter struct { 40 key unsafe.Pointer 41 val unsafe.Pointer 42 43 // remaining fields are ignored but 44 // present in the struct so that it 45 // can be zeroed for reuse. 46 // see hiter in src/runtime/map.go 47 _ [6]unsafe.Pointer 48 _ uintptr 49 _ uint8 50 _ bool 51 _ [2]uint8 52 _ [2]uintptr 53 } 54 55 var zeroHiter = &hiter{} 56 57 //go:noescape 58 //go:linkname mapiterinit runtime.mapiterinit 59 func mapiterinit(unsafe.Pointer, unsafe.Pointer, unsafe.Pointer) 60 61 //go:noescape 62 //go:linkname mapiternext reflect.mapiternext 63 func mapiternext(*hiter) 64 65 //go:noescape 66 //go:linkname maplen reflect.maplen 67 func maplen(unsafe.Pointer) int