From f5fc4fc43ce74e8aaf35c31c293b8365ec3a95b2 Mon Sep 17 00:00:00 2001 From: Matthew Nibecker Date: Sun, 22 Nov 2026 12:17:33 -0800 Subject: [PATCH] Improve performance of wasm jsonutil.Skip This commit improves the performance about 4x of jsonutil.Skip in the wasm environment by using sonic's native go implementation of Skip as accessed in the API by ast.Searcher. --- pkg/jsonskip/jsonutil_wasm.go | 40 +++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/pkg/jsonskip/jsonutil_wasm.go b/pkg/jsonskip/jsonutil_wasm.go index 3bbc611e8a..26589c891d 100644 --- a/pkg/jsonskip/jsonutil_wasm.go +++ b/pkg/jsonskip/jsonutil_wasm.go @@ -3,20 +3,38 @@ package jsonskip import ( - "bytes" - "encoding/json" - "errors" + "github.com/bytedance/sonic/ast" + + "github.com/superdb/super/pkg/byteconv" ) +// Skip skips a single JSON value in b, returning the offset of its first +// non-whitespace byte and the offset just past its end. If b does not +// begin with a complete JSON value, start is negative. +// +// sonic's decoder.Skip is unavailable on js/wasm, but a Searcher with an +// empty path uses sonic's pure-Go skipper and returns the raw value. func Skip(b []byte) (int, int) { - dec := json.NewDecoder(bytes.NewReader(b)) - if err := dec.Decode(new(json.RawMessage)); err != nil { - var offset int - var serr *json.SyntaxError - if errors.As(err, &serr) { - offset = int(serr.Offset) + s := byteconv.UnsafeString(b) + node, err := ast.NewSearcher(s).GetByPath() + if err != nil { + return -1, 0 + } + raw, err := node.Raw() + if err != nil { + return -1, 0 + } + start := firstNonSpace(b) + return start, start + len(raw) +} + +func firstNonSpace(b []byte) int { + for i, c := range b { + switch c { + case ' ', '\t', '\n', '\r': + default: + return i } - return -1, offset } - return 0, int(dec.InputOffset()) + return len(b) }