@@ -44,25 +44,13 @@ func init() {
4444 }
4545}
4646
47- /*
48- no-map
49- BenchmarkV3 254824 4413 ns/op 1186 B/op 34 allocs/op
50- BenchmarkV3Decode 347130 3244 ns/op 810 B/op 29 allocs/op
51- BenchmarkV3Encode 1000000 1032 ns/op 374 B/op 5 allocs/op
52- BenchmarkV3int64 1239445 932 ns/op 510 B/op 18 allocs/op
53- slice
54- BenchmarkV3 2838296 4335 ns/op 1300 B/op 34 allocs/op
55- BenchmarkV3Decode 3853204 3147 ns/op 810 B/op 29 allocs/op
56- BenchmarkV3Encode 12269514 970 ns/op 445 B/op 5 allocs/op
57- BenchmarkV3int64 13352065 898 ns/op 626 B/op 18 allocs/op
58- */
59-
6047//V3Encoder is the encoder used to encode a ttv3 data stream
6148type V3Encoder struct {
6249 out io.Writer
6350 varintbuf * [binary .MaxVarintLen64 + 1 ]byte
64- sync.Mutex
6551 typeCache map [string ]map [string ]int
52+ isStream bool
53+ sync.Mutex
6654}
6755
6856var v3StreamHeader = []byte {version3 , 1 << 7 }
@@ -72,10 +60,9 @@ var v3NoStreamHeader = []byte{version3, 0}
7260func NewV3Encoder (out io.Writer , isStream bool ) * V3Encoder {
7361 if isStream {
7462 out .Write (v3StreamHeader )
75- } else {
76- out .Write (v3NoStreamHeader )
7763 }
7864 return & V3Encoder {
65+ isStream : isStream ,
7966 out : out ,
8067 varintbuf : & [binary .MaxVarintLen64 + 1 ]byte {},
8168 typeCache : map [string ]map [string ]int {},
@@ -95,9 +82,12 @@ func Encodev3(d interface{}, out io.Writer) error {
9582 return enc .encodeValuev3 (d , v3.KeyValue {})
9683}
9784
98- //Encode encodes an `interface{}`` into a bytebuffer using ttv3
85+ //Encode encodes an `interface{}` into a bytebuffer using ttv3
9986func (enc * V3Encoder ) Encode (d interface {}) error {
10087 enc .Lock ()
88+ if ! enc .isStream {
89+ enc .out .Write (v3NoStreamHeader )
90+ }
10191 ret := enc .encodeValuev3 (d , v3.KeyValue {})
10292 enc .Unlock ()
10393 return ret
@@ -372,6 +362,9 @@ func (enc *V3Encoder) encodeValuev3(d interface{}, k v3.KeyValue) error {
372362 return err
373363 }
374364 }
365+ } else if kind == reflect .Interface || kind == reflect .Ptr {
366+ enc .encodeValuev3_reflect (val .Elem (), k )
367+ alreadyEncoded = true
375368 } else {
376369 return v3 .ErrInvalidInput
377370 }
@@ -394,7 +387,6 @@ func (enc *V3Encoder) encodeValuev3_reflect(d reflect.Value, k v3.KeyValue) erro
394387 case reflect .Interface , reflect .Ptr :
395388 enc .encodeValuev3_reflect (d .Elem (), k )
396389 alreadyEncoded = true
397-
398390 case reflect .String :
399391 value .Value .Value = v3 .StringToBytes (d .String ())
400392 value .Value .Vtype = v3 .StringT
0 commit comments