mapiter.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright 2021 ByteDance Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package alg
  17. import (
  18. "encoding"
  19. "reflect"
  20. "strconv"
  21. "sync"
  22. "unsafe"
  23. "github.com/bytedance/sonic/internal/encoder/vars"
  24. "github.com/bytedance/sonic/internal/rt"
  25. )
  26. type _MapPair struct {
  27. k string // when the map key is integer, k is pointed to m
  28. v unsafe.Pointer
  29. m [32]byte
  30. }
  31. type MapIterator struct {
  32. It rt.GoMapIterator // must be the first field
  33. kv rt.GoSlice // slice of _MapPair
  34. ki int
  35. }
  36. var (
  37. iteratorPool = sync.Pool{}
  38. iteratorPair = rt.UnpackType(reflect.TypeOf(_MapPair{}))
  39. )
  40. func init() {
  41. if unsafe.Offsetof(MapIterator{}.It) != 0 {
  42. panic("_MapIterator.it is not the first field")
  43. }
  44. }
  45. func newIterator() *MapIterator {
  46. if v := iteratorPool.Get(); v == nil {
  47. return new(MapIterator)
  48. } else {
  49. return resetIterator(v.(*MapIterator))
  50. }
  51. }
  52. func resetIterator(p *MapIterator) *MapIterator {
  53. p.ki = 0
  54. p.It = rt.GoMapIterator{}
  55. p.kv.Len = 0
  56. return p
  57. }
  58. func (self *MapIterator) at(i int) *_MapPair {
  59. return (*_MapPair)(unsafe.Pointer(uintptr(self.kv.Ptr) + uintptr(i) * unsafe.Sizeof(_MapPair{})))
  60. }
  61. func (self *MapIterator) add() (p *_MapPair) {
  62. p = self.at(self.kv.Len)
  63. self.kv.Len++
  64. return
  65. }
  66. func (self *MapIterator) data() (p []_MapPair) {
  67. *(*rt.GoSlice)(unsafe.Pointer(&p)) = self.kv
  68. return
  69. }
  70. func (self *MapIterator) append(t *rt.GoType, k unsafe.Pointer, v unsafe.Pointer) (err error) {
  71. p := self.add()
  72. p.v = v
  73. /* check for strings */
  74. if tk := t.Kind(); tk != reflect.String {
  75. return self.appendGeneric(p, t, tk, k)
  76. }
  77. /* fast path for strings */
  78. p.k = *(*string)(k)
  79. return nil
  80. }
  81. func (self *MapIterator) appendGeneric(p *_MapPair, t *rt.GoType, v reflect.Kind, k unsafe.Pointer) error {
  82. switch v {
  83. case reflect.Int : p.k = rt.Mem2Str(strconv.AppendInt(p.m[:0], int64(*(*int)(k)), 10)) ; return nil
  84. case reflect.Int8 : p.k = rt.Mem2Str(strconv.AppendInt(p.m[:0], int64(*(*int8)(k)), 10)) ; return nil
  85. case reflect.Int16 : p.k = rt.Mem2Str(strconv.AppendInt(p.m[:0], int64(*(*int16)(k)), 10)) ; return nil
  86. case reflect.Int32 : p.k = rt.Mem2Str(strconv.AppendInt(p.m[:0], int64(*(*int32)(k)), 10)) ; return nil
  87. case reflect.Int64 : p.k = rt.Mem2Str(strconv.AppendInt(p.m[:0], int64(*(*int64)(k)), 10)) ; return nil
  88. case reflect.Uint : p.k = rt.Mem2Str(strconv.AppendUint(p.m[:0], uint64(*(*uint)(k)), 10)) ; return nil
  89. case reflect.Uint8 : p.k = rt.Mem2Str(strconv.AppendUint(p.m[:0], uint64(*(*uint8)(k)), 10)) ; return nil
  90. case reflect.Uint16 : p.k = rt.Mem2Str(strconv.AppendUint(p.m[:0], uint64(*(*uint16)(k)), 10)) ; return nil
  91. case reflect.Uint32 : p.k = rt.Mem2Str(strconv.AppendUint(p.m[:0], uint64(*(*uint32)(k)), 10)) ; return nil
  92. case reflect.Uint64 : p.k = rt.Mem2Str(strconv.AppendUint(p.m[:0], uint64(*(*uint64)(k)), 10)) ; return nil
  93. case reflect.Uintptr : p.k = rt.Mem2Str(strconv.AppendUint(p.m[:0], uint64(*(*uintptr)(k)), 10)) ; return nil
  94. case reflect.Interface : return self.appendInterface(p, t, k)
  95. case reflect.Struct, reflect.Ptr : return self.appendConcrete(p, t, k)
  96. default : panic("unexpected map key type")
  97. }
  98. }
  99. func (self *MapIterator) appendConcrete(p *_MapPair, t *rt.GoType, k unsafe.Pointer) (err error) {
  100. // compiler has already checked that the type implements the encoding.MarshalText interface
  101. if !t.Indirect() {
  102. k = *(*unsafe.Pointer)(k)
  103. }
  104. eface := rt.GoEface{Value: k, Type: t}.Pack()
  105. out, err := eface.(encoding.TextMarshaler).MarshalText()
  106. if err != nil {
  107. return err
  108. }
  109. p.k = rt.Mem2Str(out)
  110. return
  111. }
  112. func (self *MapIterator) appendInterface(p *_MapPair, t *rt.GoType, k unsafe.Pointer) (err error) {
  113. if len(rt.IfaceType(t).Methods) == 0 {
  114. panic("unexpected map key type")
  115. } else if p.k, err = asText(k); err == nil {
  116. return nil
  117. } else {
  118. return
  119. }
  120. }
  121. func IteratorStop(p *MapIterator) {
  122. iteratorPool.Put(p)
  123. }
  124. func IteratorNext(p *MapIterator) {
  125. i := p.ki
  126. t := &p.It
  127. /* check for unordered iteration */
  128. if i < 0 {
  129. rt.Mapiternext(t)
  130. return
  131. }
  132. /* check for end of iteration */
  133. if p.ki >= p.kv.Len {
  134. t.K = nil
  135. t.V = nil
  136. return
  137. }
  138. /* update the key-value pair, and increase the pointer */
  139. t.K = unsafe.Pointer(&p.at(p.ki).k)
  140. t.V = p.at(p.ki).v
  141. p.ki++
  142. }
  143. func IteratorStart(t *rt.GoMapType, m unsafe.Pointer, fv uint64) (*MapIterator, error) {
  144. it := newIterator()
  145. rt.Mapiterinit(t, m, &it.It)
  146. count := rt.Maplen(m)
  147. /* check for key-sorting, empty map don't need sorting */
  148. if count == 0 || (fv & (1<<BitSortMapKeys)) == 0 {
  149. it.ki = -1
  150. return it, nil
  151. }
  152. /* pre-allocate space if needed */
  153. if count > it.kv.Cap {
  154. it.kv = rt.GrowSlice(iteratorPair, it.kv, count)
  155. }
  156. /* dump all the key-value pairs */
  157. for ; it.It.K != nil; rt.Mapiternext(&it.It) {
  158. if err := it.append(t.Key, it.It.K, it.It.V); err != nil {
  159. IteratorStop(it)
  160. return nil, err
  161. }
  162. }
  163. /* sort the keys, map with only 1 item don't need sorting */
  164. if it.ki = 1; count > 1 {
  165. radixQsort(it.data(), 0, maxDepth(it.kv.Len))
  166. }
  167. /* load the first pair into iterator */
  168. it.It.V = it.at(0).v
  169. it.It.K = unsafe.Pointer(&it.at(0).k)
  170. return it, nil
  171. }
  172. func asText(v unsafe.Pointer) (string, error) {
  173. text := rt.AssertI2I(rt.UnpackType(vars.EncodingTextMarshalerType), *(*rt.GoIface)(v))
  174. r, e := (*(*encoding.TextMarshaler)(unsafe.Pointer(&text))).MarshalText()
  175. return rt.Mem2Str(r), e
  176. }