1
0
mirror of https://github.com/json-iterator/go.git synced 2025-06-24 23:16:47 +02:00

#73 fix interface{} optimization for one ptr field struct and array

This commit is contained in:
Tao Wen
2017-06-26 22:37:24 +08:00
parent 85f7a1b0b3
commit dc44e85a86
5 changed files with 84 additions and 28 deletions

View File

@ -33,10 +33,6 @@ type arrayEncoder struct {
}
func (encoder *arrayEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
if ptr == nil {
stream.WriteNil()
return
}
stream.WriteArrayStart()
elemPtr := uintptr(ptr)
encoder.elemEncoder.Encode(unsafe.Pointer(elemPtr), stream)
@ -52,6 +48,14 @@ func (encoder *arrayEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
}
func (encoder *arrayEncoder) EncodeInterface(val interface{}, stream *Stream) {
// special optimization for interface{}
e := (*emptyInterface)(unsafe.Pointer(&val))
if e.word == nil {
stream.WriteArrayStart()
stream.WriteNil()
stream.WriteArrayEnd()
return
}
WriteToStream(val, stream, encoder)
}