Add a new key decoder

This commit is contained in:
Masaaki Goshima 2021-02-05 20:47:49 +09:00
parent 73a3af734f
commit f1664b5c1f
1 changed files with 146 additions and 81 deletions

View File

@ -16,13 +16,14 @@ type structFieldSet struct {
type structDecoder struct { type structDecoder struct {
fieldMap map[string]*structFieldSet fieldMap map[string]*structFieldSet
keyDecoder *stringDecoder stringDecoder *stringDecoder
structName string structName string
fieldName string fieldName string
isTriedOptimize bool isTriedOptimize bool
isOptimizedKeyDecoder bool keyBitmapInt8 [][256]int8
keyBitMap [][256]int16 keyBitmapInt16 [][256]int16
sortedFieldSets []*structFieldSet sortedFieldSets []*structFieldSet
keyDecoder func(*structDecoder, []byte, int64) (int64, *structFieldSet, error)
} }
var ( var (
@ -48,9 +49,10 @@ func init() {
func newStructDecoder(structName, fieldName string, fieldMap map[string]*structFieldSet) *structDecoder { func newStructDecoder(structName, fieldName string, fieldMap map[string]*structFieldSet) *structDecoder {
return &structDecoder{ return &structDecoder{
fieldMap: fieldMap, fieldMap: fieldMap,
keyDecoder: newStringDecoder(structName, fieldName), stringDecoder: newStringDecoder(structName, fieldName),
structName: structName, structName: structName,
fieldName: fieldName, fieldName: fieldName,
keyDecoder: decodeKey,
} }
} }
@ -88,22 +90,35 @@ func (d *structDecoder) tryOptimize() {
sortedKeys = append(sortedKeys, key) sortedKeys = append(sortedKeys, key)
} }
sort.Strings(sortedKeys) sort.Strings(sortedKeys)
keyBitMap := make([][256]int16, maxKeyLen) if len(sortedKeys) <= 8 {
keyBitmap := make([][256]int8, maxKeyLen)
for i, key := range sortedKeys { for i, key := range sortedKeys {
for j := 0; j < len(key); j++ { for j := 0; j < len(key); j++ {
c := key[j] c := key[j]
keyBitMap[j][c] |= (1 << uint(i)) keyBitmap[j][c] |= (1 << uint(i))
} }
d.sortedFieldSets = append(d.sortedFieldSets, fieldMap[key]) d.sortedFieldSets = append(d.sortedFieldSets, fieldMap[key])
} }
d.keyBitMap = keyBitMap d.keyBitmapInt8 = keyBitmap
d.isOptimizedKeyDecoder = true d.keyDecoder = decodeKeyByBitmapInt8
} else {
keyBitmap := make([][256]int16, maxKeyLen)
for i, key := range sortedKeys {
for j := 0; j < len(key); j++ {
c := key[j]
keyBitmap[j][c] |= (1 << uint(i))
}
d.sortedFieldSets = append(d.sortedFieldSets, fieldMap[key])
}
d.keyBitmapInt16 = keyBitmap
d.keyDecoder = decodeKeyByBitmapInt16
}
} }
func (d *structDecoder) decodeKeyOptimized(buf []byte, cursor int64) (int64, *structFieldSet, error) { func decodeKeyByBitmapInt8(d *structDecoder, buf []byte, cursor int64) (int64, *structFieldSet, error) {
var ( var (
field *structFieldSet field *structFieldSet
curBit int16 = math.MaxInt16 curBit int8 = math.MaxInt8
) )
for { for {
switch buf[cursor] { switch buf[cursor] {
@ -111,33 +126,17 @@ func (d *structDecoder) decodeKeyOptimized(buf []byte, cursor int64) (int64, *st
cursor++ cursor++
case '"': case '"':
cursor++ cursor++
keyIdx := 0
c := buf[cursor] c := buf[cursor]
switch c { switch c {
case '"': case '"':
cursor++ cursor++
goto KEY_END return cursor, field, nil
case nul: case nul:
return 0, nil, errUnexpectedEndOfJSON("string", cursor) return 0, nil, errUnexpectedEndOfJSON("string", cursor)
} }
curBit &= d.keyBitMap[0][largeToSmallTable[c]] keyIdx := 0
if curBit == 0 { bitmap := d.keyBitmapInt8
for { keyBitmapLen := len(bitmap)
cursor++
switch buf[cursor] {
case '"':
cursor++
goto KEY_END
case '\\':
cursor++
if buf[cursor] == nul {
return 0, nil, errUnexpectedEndOfJSON("string", cursor)
}
case nul:
return 0, nil, errUnexpectedEndOfJSON("string", cursor)
}
}
}
for { for {
c := buf[cursor] c := buf[cursor]
switch c { switch c {
@ -146,17 +145,17 @@ func (d *structDecoder) decodeKeyOptimized(buf []byte, cursor int64) (int64, *st
fieldSetIndex := bitHashTable[(x*0x03F566ED27179461)>>58] fieldSetIndex := bitHashTable[(x*0x03F566ED27179461)>>58]
field = d.sortedFieldSets[fieldSetIndex] field = d.sortedFieldSets[fieldSetIndex]
cursor++ cursor++
goto KEY_END return cursor, field, nil
case nul: case nul:
return 0, nil, errUnexpectedEndOfJSON("string", cursor) return 0, nil, errUnexpectedEndOfJSON("string", cursor)
default: default:
if keyIdx >= len(d.keyBitMap) { if keyIdx >= keyBitmapLen {
for { for {
cursor++ cursor++
switch buf[cursor] { switch buf[cursor] {
case '"': case '"':
cursor++ cursor++
goto KEY_END return cursor, field, nil
case '\\': case '\\':
cursor++ cursor++
if buf[cursor] == nul { if buf[cursor] == nul {
@ -167,14 +166,14 @@ func (d *structDecoder) decodeKeyOptimized(buf []byte, cursor int64) (int64, *st
} }
} }
} }
curBit &= d.keyBitMap[keyIdx][largeToSmallTable[c]] curBit &= bitmap[keyIdx][largeToSmallTable[c]]
if curBit == 0 { if curBit == 0 {
for { for {
cursor++ cursor++
switch buf[cursor] { switch buf[cursor] {
case '"': case '"':
cursor++ cursor++
goto KEY_END return cursor, field, nil
case '\\': case '\\':
cursor++ cursor++
if buf[cursor] == nul { if buf[cursor] == nul {
@ -193,14 +192,97 @@ func (d *structDecoder) decodeKeyOptimized(buf []byte, cursor int64) (int64, *st
return cursor, nil, errNotAtBeginningOfValue(cursor) return cursor, nil, errNotAtBeginningOfValue(cursor)
} }
} }
KEY_END: }
cursor = skipWhiteSpace(buf, cursor)
if buf[cursor] != ':' { func decodeKeyByBitmapInt16(d *structDecoder, buf []byte, cursor int64) (int64, *structFieldSet, error) {
return 0, nil, errExpected("colon after object key", cursor) var (
field *structFieldSet
curBit int16 = math.MaxInt16
)
for {
switch buf[cursor] {
case ' ', '\n', '\t', '\r':
cursor++
case '"':
cursor++
c := buf[cursor]
switch c {
case '"':
cursor++
return cursor, field, nil
case nul:
return 0, nil, errUnexpectedEndOfJSON("string", cursor)
}
keyIdx := 0
bitmap := d.keyBitmapInt16
keyBitmapLen := len(bitmap)
for {
c := buf[cursor]
switch c {
case '"':
x := uint64(curBit & -curBit)
fieldSetIndex := bitHashTable[(x*0x03F566ED27179461)>>58]
field = d.sortedFieldSets[fieldSetIndex]
cursor++
return cursor, field, nil
case nul:
return 0, nil, errUnexpectedEndOfJSON("string", cursor)
default:
if keyIdx >= keyBitmapLen {
for {
cursor++
switch buf[cursor] {
case '"':
cursor++
return cursor, field, nil
case '\\':
cursor++
if buf[cursor] == nul {
return 0, nil, errUnexpectedEndOfJSON("string", cursor)
}
case nul:
return 0, nil, errUnexpectedEndOfJSON("string", cursor)
}
}
}
curBit &= bitmap[keyIdx][largeToSmallTable[c]]
if curBit == 0 {
for {
cursor++
switch buf[cursor] {
case '"':
cursor++
return cursor, field, nil
case '\\':
cursor++
if buf[cursor] == nul {
return 0, nil, errUnexpectedEndOfJSON("string", cursor)
}
case nul:
return 0, nil, errUnexpectedEndOfJSON("string", cursor)
}
}
}
keyIdx++
} }
cursor++ cursor++
if cursor >= int64(len(buf)) { }
return 0, nil, errExpected("object value after colon", cursor) default:
return cursor, nil, errNotAtBeginningOfValue(cursor)
}
}
}
func decodeKey(d *structDecoder, buf []byte, cursor int64) (int64, *structFieldSet, error) {
key, c, err := d.stringDecoder.decodeByte(buf, cursor)
if err != nil {
return 0, nil, err
}
cursor = c
k := *(*string)(unsafe.Pointer(&key))
field, exists := d.fieldMap[k]
if !exists {
return cursor, nil, nil
} }
return cursor, field, nil return cursor, field, nil
} }
@ -227,7 +309,7 @@ func (d *structDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
} }
for { for {
s.reset() s.reset()
key, err := d.keyDecoder.decodeStreamByte(s) key, err := d.stringDecoder.decodeStreamByte(s)
if err != nil { if err != nil {
return err return err
} }
@ -273,31 +355,6 @@ func (d *structDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
} }
} }
func (d *structDecoder) decodeKey(buf []byte, cursor int64) (int64, *structFieldSet, error) {
if d.isOptimizedKeyDecoder {
return d.decodeKeyOptimized(buf, cursor)
}
key, c, err := d.keyDecoder.decodeByte(buf, cursor)
if err != nil {
return 0, nil, err
}
cursor = c
cursor = skipWhiteSpace(buf, cursor)
if buf[cursor] != ':' {
return 0, nil, errExpected("colon after object key", cursor)
}
cursor++
if cursor >= int64(len(buf)) {
return 0, nil, errExpected("object value after colon", cursor)
}
k := *(*string)(unsafe.Pointer(&key))
field, exists := d.fieldMap[k]
if !exists {
return cursor, nil, nil
}
return cursor, field, nil
}
func (d *structDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) { func (d *structDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) {
buflen := int64(len(buf)) buflen := int64(len(buf))
cursor = skipWhiteSpace(buf, cursor) cursor = skipWhiteSpace(buf, cursor)
@ -327,11 +384,19 @@ func (d *structDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int6
} }
cursor++ cursor++
for ; cursor < buflen; cursor++ { for ; cursor < buflen; cursor++ {
c, field, err := d.decodeKey(buf, cursor) c, field, err := d.keyDecoder(d, buf, cursor)
if err != nil { if err != nil {
return 0, err return 0, err
} }
cursor = c cursor = c
cursor = skipWhiteSpace(buf, cursor)
if buf[cursor] != ':' {
return 0, errExpected("colon after object key", cursor)
}
cursor++
if cursor >= int64(len(buf)) {
return 0, errExpected("object value after colon", cursor)
}
if field != nil { if field != nil {
c, err := field.dec.decode(buf, cursor, unsafe.Pointer(uintptr(p)+field.offset)) c, err := field.dec.decode(buf, cursor, unsafe.Pointer(uintptr(p)+field.offset))
if err != nil { if err != nil {