noop.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // Copyright (c) 2012-2015 Ugorji Nwoke. All rights reserved.
  2. // Use of this source code is governed by a MIT license found in the LICENSE file.
  3. package codec
  4. import (
  5. "math/rand"
  6. "time"
  7. )
  8. // NoopHandle returns a no-op handle. It basically does nothing.
  9. // It is only useful for benchmarking, as it gives an idea of the
  10. // overhead from the codec framework.
  11. //
  12. // LIBRARY USERS: *** DO NOT USE ***
  13. func NoopHandle(slen int) *noopHandle {
  14. h := noopHandle{}
  15. h.rand = rand.New(rand.NewSource(time.Now().UnixNano()))
  16. h.B = make([][]byte, slen)
  17. h.S = make([]string, slen)
  18. for i := 0; i < len(h.S); i++ {
  19. b := make([]byte, i+1)
  20. for j := 0; j < len(b); j++ {
  21. b[j] = 'a' + byte(i)
  22. }
  23. h.B[i] = b
  24. h.S[i] = string(b)
  25. }
  26. return &h
  27. }
  28. // noopHandle does nothing.
  29. // It is used to simulate the overhead of the codec framework.
  30. type noopHandle struct {
  31. BasicHandle
  32. binaryEncodingType
  33. noopDrv // noopDrv is unexported here, so we can get a copy of it when needed.
  34. }
  35. type noopDrv struct {
  36. d *Decoder
  37. e *Encoder
  38. i int
  39. S []string
  40. B [][]byte
  41. mks []bool // stack. if map (true), else if array (false)
  42. mk bool // top of stack. what container are we on? map or array?
  43. ct valueType // last response for IsContainerType.
  44. cb int // counter for ContainerType
  45. rand *rand.Rand
  46. }
  47. func (h *noopDrv) r(v int) int { return h.rand.Intn(v) }
  48. func (h *noopDrv) m(v int) int { h.i++; return h.i % v }
  49. func (h *noopDrv) newEncDriver(e *Encoder) encDriver { h.e = e; return h }
  50. func (h *noopDrv) newDecDriver(d *Decoder) decDriver { h.d = d; return h }
  51. func (h *noopDrv) reset() {}
  52. func (h *noopDrv) uncacheRead() {}
  53. // --- encDriver
  54. // stack functions (for map and array)
  55. func (h *noopDrv) start(b bool) {
  56. // println("start", len(h.mks)+1)
  57. h.mks = append(h.mks, b)
  58. h.mk = b
  59. }
  60. func (h *noopDrv) end() {
  61. // println("end: ", len(h.mks)-1)
  62. h.mks = h.mks[:len(h.mks)-1]
  63. if len(h.mks) > 0 {
  64. h.mk = h.mks[len(h.mks)-1]
  65. } else {
  66. h.mk = false
  67. }
  68. }
  69. func (h *noopDrv) EncodeBuiltin(rt uintptr, v interface{}) {}
  70. func (h *noopDrv) EncodeNil() {}
  71. func (h *noopDrv) EncodeInt(i int64) {}
  72. func (h *noopDrv) EncodeUint(i uint64) {}
  73. func (h *noopDrv) EncodeBool(b bool) {}
  74. func (h *noopDrv) EncodeFloat32(f float32) {}
  75. func (h *noopDrv) EncodeFloat64(f float64) {}
  76. func (h *noopDrv) EncodeRawExt(re *RawExt, e *Encoder) {}
  77. func (h *noopDrv) EncodeArrayStart(length int) { h.start(true) }
  78. func (h *noopDrv) EncodeMapStart(length int) { h.start(false) }
  79. func (h *noopDrv) EncodeEnd() { h.end() }
  80. func (h *noopDrv) EncodeString(c charEncoding, v string) {}
  81. func (h *noopDrv) EncodeSymbol(v string) {}
  82. func (h *noopDrv) EncodeStringBytes(c charEncoding, v []byte) {}
  83. func (h *noopDrv) EncodeExt(rv interface{}, xtag uint64, ext Ext, e *Encoder) {}
  84. // ---- decDriver
  85. func (h *noopDrv) initReadNext() {}
  86. func (h *noopDrv) CheckBreak() bool { return false }
  87. func (h *noopDrv) IsBuiltinType(rt uintptr) bool { return false }
  88. func (h *noopDrv) DecodeBuiltin(rt uintptr, v interface{}) {}
  89. func (h *noopDrv) DecodeInt(bitsize uint8) (i int64) { return int64(h.m(15)) }
  90. func (h *noopDrv) DecodeUint(bitsize uint8) (ui uint64) { return uint64(h.m(35)) }
  91. func (h *noopDrv) DecodeFloat(chkOverflow32 bool) (f float64) { return float64(h.m(95)) }
  92. func (h *noopDrv) DecodeBool() (b bool) { return h.m(2) == 0 }
  93. func (h *noopDrv) DecodeString() (s string) { return h.S[h.m(8)] }
  94. // func (h *noopDrv) DecodeStringAsBytes(bs []byte) []byte { return h.DecodeBytes(bs) }
  95. func (h *noopDrv) DecodeBytes(bs []byte, isstring, zerocopy bool) []byte { return h.B[h.m(len(h.B))] }
  96. func (h *noopDrv) ReadEnd() { h.end() }
  97. // toggle map/slice
  98. func (h *noopDrv) ReadMapStart() int { h.start(true); return h.m(10) }
  99. func (h *noopDrv) ReadArrayStart() int { h.start(false); return h.m(10) }
  100. func (h *noopDrv) ContainerType() (vt valueType) {
  101. // return h.m(2) == 0
  102. // handle kStruct, which will bomb is it calls this and doesn't get back a map or array.
  103. // consequently, if the return value is not map or array, reset it to one of them based on h.m(7) % 2
  104. // for kstruct: at least one out of every 2 times, return one of valueTypeMap or Array (else kstruct bombs)
  105. // however, every 10th time it is called, we just return something else.
  106. var vals = [...]valueType{valueTypeArray, valueTypeMap}
  107. // ------------ TAKE ------------
  108. // if h.cb%2 == 0 {
  109. // if h.ct == valueTypeMap || h.ct == valueTypeArray {
  110. // } else {
  111. // h.ct = vals[h.m(2)]
  112. // }
  113. // } else if h.cb%5 == 0 {
  114. // h.ct = valueType(h.m(8))
  115. // } else {
  116. // h.ct = vals[h.m(2)]
  117. // }
  118. // ------------ TAKE ------------
  119. // if h.cb%16 == 0 {
  120. // h.ct = valueType(h.cb % 8)
  121. // } else {
  122. // h.ct = vals[h.cb%2]
  123. // }
  124. h.ct = vals[h.cb%2]
  125. h.cb++
  126. return h.ct
  127. // if h.ct == valueTypeNil || h.ct == valueTypeString || h.ct == valueTypeBytes {
  128. // return h.ct
  129. // }
  130. // return valueTypeUnset
  131. // TODO: may need to tweak this so it works.
  132. // if h.ct == valueTypeMap && vt == valueTypeArray || h.ct == valueTypeArray && vt == valueTypeMap {
  133. // h.cb = !h.cb
  134. // h.ct = vt
  135. // return h.cb
  136. // }
  137. // // go in a loop and check it.
  138. // h.ct = vt
  139. // h.cb = h.m(7) == 0
  140. // return h.cb
  141. }
  142. func (h *noopDrv) TryDecodeAsNil() bool {
  143. if h.mk {
  144. return false
  145. } else {
  146. return h.m(8) == 0
  147. }
  148. }
  149. func (h *noopDrv) DecodeExt(rv interface{}, xtag uint64, ext Ext) uint64 {
  150. return 0
  151. }
  152. func (h *noopDrv) DecodeNaked() {
  153. // use h.r (random) not h.m() because h.m() could cause the same value to be given.
  154. var sk int
  155. if h.mk {
  156. // if mapkey, do not support values of nil OR bytes, array, map or rawext
  157. sk = h.r(7) + 1
  158. } else {
  159. sk = h.r(12)
  160. }
  161. n := &h.d.n
  162. switch sk {
  163. case 0:
  164. n.v = valueTypeNil
  165. case 1:
  166. n.v, n.b = valueTypeBool, false
  167. case 2:
  168. n.v, n.b = valueTypeBool, true
  169. case 3:
  170. n.v, n.i = valueTypeInt, h.DecodeInt(64)
  171. case 4:
  172. n.v, n.u = valueTypeUint, h.DecodeUint(64)
  173. case 5:
  174. n.v, n.f = valueTypeFloat, h.DecodeFloat(true)
  175. case 6:
  176. n.v, n.f = valueTypeFloat, h.DecodeFloat(false)
  177. case 7:
  178. n.v, n.s = valueTypeString, h.DecodeString()
  179. case 8:
  180. n.v, n.l = valueTypeBytes, h.B[h.m(len(h.B))]
  181. case 9:
  182. n.v = valueTypeArray
  183. case 10:
  184. n.v = valueTypeMap
  185. default:
  186. n.v = valueTypeExt
  187. n.u = h.DecodeUint(64)
  188. n.l = h.B[h.m(len(h.B))]
  189. }
  190. h.ct = n.v
  191. return
  192. }