From e28091e4c7d8285aaac78e7a093f64ac92ba2e13 Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Tue, 25 Aug 2020 17:19:41 +0900 Subject: [PATCH] Improve performance for []byte type --- encode_vm.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/encode_vm.go b/encode_vm.go index a9ebb3b..8714a4c 100644 --- a/encode_vm.go +++ b/encode_vm.go @@ -74,9 +74,12 @@ func (e *Encoder) run(code *opcode) error { if ptr == 0 || header.Data == 0 { e.encodeNull() } else { - s := base64.StdEncoding.EncodeToString(e.ptrToBytes(code.ptr)) + b := e.ptrToBytes(code.ptr) + encodedLen := base64.StdEncoding.EncodedLen(len(b)) e.encodeByte('"') - e.encodeBytes(*(*[]byte)(unsafe.Pointer(&s))) + buf := make([]byte, encodedLen) + base64.StdEncoding.Encode(buf, b) + e.encodeBytes(buf) e.encodeByte('"') } code = code.next