From a979b83d27c1ddd9c347702cfd8b566cb79a73d9 Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Fri, 8 May 2020 23:59:49 +0900 Subject: [PATCH] Add noescape trick for decoding --- decode.go | 5 ++++- encode.go | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/decode.go b/decode.go index 923cdce..4cee2ac 100644 --- a/decode.go +++ b/decode.go @@ -81,7 +81,10 @@ func (d *Decoder) decode(src []byte, header *interfaceHeader) error { typeptr := uintptr(unsafe.Pointer(typ)) dec := cachedDecoder.get(typeptr) if dec == nil { - compiledDec, err := d.compileHead(typ) + // noescape trick for header.typ ( reflect.*rtype ) + copiedType := (*rtype)(unsafe.Pointer(typeptr)) + + compiledDec, err := d.compileHead(copiedType) if err != nil { return err } diff --git a/encode.go b/encode.go index e64780a..6a8db7a 100644 --- a/encode.go +++ b/encode.go @@ -159,7 +159,7 @@ func (e *Encoder) encode(v interface{}) error { return nil } - // to noescape trick for header.typ ( reflect.*rtype ) + // noescape trick for header.typ ( reflect.*rtype ) copiedType := (*rtype)(unsafe.Pointer(typeptr)) codeIndent, err := e.compileHead(copiedType, true)