Skip to content

Commit

Permalink
Optimize Encoder.WriteValue for object names (#68)
Browse files Browse the repository at this point in the history
By specifying the isVerbatim flag to names.insertQuoted,
the method can avoid unquoting the name since it knows that
it only needs to strip off the surrounding double quotes.

Performance:

    name                                           old speed      new speed      delta
    Testdata/CanadaGeometry/Encode/Value/Buffered  1.12GB/s ± 0%  1.15GB/s ± 3%     ~     (p=0.151 n=5+5)
    Testdata/CitmCatalog/Encode/Value/Buffered     1.76GB/s ± 0%  1.99GB/s ± 1%  +13.19%  (p=0.008 n=5+5)
    Testdata/GolangSource/Encode/Value/Buffered     793MB/s ± 4%   949MB/s ± 1%  +19.68%  (p=0.008 n=5+5)
    Testdata/StringEscaped/Encode/Value/Buffered    228MB/s ± 1%   229MB/s ± 0%     ~     (p=0.421 n=5+5)
    Testdata/StringUnicode/Encode/Value/Buffered    983MB/s ± 0%  1014MB/s ± 1%   +3.12%  (p=0.008 n=5+5)
    Testdata/SyntheaFhir/Encode/Value/Buffered     1.39GB/s ± 4%  1.65GB/s ± 1%  +18.43%  (p=0.008 n=5+5)
    Testdata/TwitterStatus/Encode/Value/Buffered   1.03GB/s ± 1%  1.23GB/s ± 0%  +19.81%  (p=0.008 n=5+5)
  • Loading branch information
dsnet authored Dec 8, 2024
1 parent 72794ee commit d0ded75
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions jsontext/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -716,17 +716,18 @@ func (e *encoderState) reformatObject(dst []byte, src Value, depth int) ([]byte,
return dst, n, io.ErrUnexpectedEOF
}
m := jsonwire.ConsumeSimpleString(src[n:])
if m > 0 {
isVerbatim := m > 0
if isVerbatim {
dst = append(dst, src[n:n+m]...)
} else {
dst, m, err = jsonwire.ReformatString(dst, src[n:], &e.Flags)
if err != nil {
return dst, n + m, err
}
}
// TODO: Specify whether the name is verbatim or not.
if !e.Flags.Get(jsonflags.AllowDuplicateNames) && !names.insertQuoted(src[n:n+m], false) {
return dst, n, newDuplicateNameError(src[n : n+m])
quotedName := src[n : n+m]
if !e.Flags.Get(jsonflags.AllowDuplicateNames) && !names.insertQuoted(quotedName, isVerbatim) {
return dst, n, newDuplicateNameError(quotedName)
}
n += m

Expand Down

0 comments on commit d0ded75

Please sign in to comment.