Skip to content

Commit

Permalink
Merge pull request #354 from Jille/small-dict-keys
Browse files Browse the repository at this point in the history
Allow 1-2 byte strings in the dictionary
  • Loading branch information
vmihailenco committed Oct 1, 2023
2 parents 0ac2a56 + 4d3d137 commit 96ec1ea
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions intern.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,16 @@ func encodeInternedStringValue(e *Encoder, v reflect.Value) error {

func (e *Encoder) encodeInternedString(s string, intern bool) error {
// Interned string takes at least 3 bytes. Plain string 1 byte + string len.
if len(s) >= minInternedStringLen {
if idx, ok := e.dict[s]; ok {
return e.encodeInternedStringIndex(idx)
}
if idx, ok := e.dict[s]; ok {
return e.encodeInternedStringIndex(idx)
}

if intern && len(e.dict) < maxDictLen {
if e.dict == nil {
e.dict = make(map[string]int)
}
idx := len(e.dict)
e.dict[s] = idx
if intern && len(s) >= minInternedStringLen && len(e.dict) < maxDictLen {
if e.dict == nil {
e.dict = make(map[string]int)
}
idx := len(e.dict)
e.dict[s] = idx
}

return e.encodeNormalString(s)
Expand Down

0 comments on commit 96ec1ea

Please sign in to comment.