perf(parse): cut allocations and validate UTF-8 in the scan

This commit is contained in:
2026-09-20 22:15:10 +02:00
parent adf189aa2c
commit b4d564c682
6 changed files with 427 additions and 117 deletions
+368 -100
View File
@@ -18,10 +18,10 @@ const ctxCheckInterval = 64
// parser is a recursive-descent TOML parser producing a map[string]any tree.
//
// The scanner works on bytes, not runes: the input is validated UTF-8 before
// the parser runs, every character that drives the grammar (quotes,
// separators, newlines, bare-key characters) is ASCII, and multi-byte runes
// matter only as string content, where they are decoded on the spot. Holding
// The scanner works on bytes, not runes: every character that drives the
// grammar (quotes, separators, newlines, bare-key characters) is ASCII, the
// scan validates a multi-byte sequence where it meets one, and multi-byte
// runes matter only as content, where they are decoded on the spot. Holding
// the source as []rune instead would cost a conversion pass plus four bytes
// per rune of extra memory before parsing even starts.
type parser struct {
@@ -45,6 +45,21 @@ type parser struct {
currentPath []string
// keys interns key strings: a document that repeats a key across
// array-of-tables elements stores one string per distinct key instead of
// one per occurrence. The table is parser-local and dies with the parse;
// the tree keeps sharing the strings it was handed.
keys map[string]string
// keyBuf backs the transient single-segment result of parseKeyPath. A
// caller that keeps the path copies it out first, which is what
// retainPath does for the current section.
keyBuf [1]string
// absScratch backs the absolute path of a top-level key, which lives only
// for the statement being parsed.
absScratch [1]string
// wantDoc asks for the node tree the Document is built from; doc is that
// tree, and it stays nil when only the value tree is wanted. currentNode
// is the node of p.current; pending collects the comment lines since the
@@ -87,10 +102,9 @@ func (p *parser) leaveNesting() { p.depth-- }
func (p *parser) parse() (map[string]any, error) {
p.root = map[string]any{}
p.current = p.root
p.headers = map[string]bool{}
p.frozen = map[string]bool{}
p.dotted = map[string]bool{}
p.arrays = map[string]bool{}
// The definition maps start unallocated: a document with no headers, no
// dotted keys and no inline tables never pays for them, and a nil map
// reads as empty. Each is created on its first write.
p.currentPath = nil
if p.wantDoc {
p.doc = newTable(p.root)
@@ -166,6 +180,56 @@ func (p *parser) checkCtx() error {
return p.ctx.Err()
}
// --- definition maps --------------------------------------------------------
// The definition maps record what a document has already defined, so a later
// statement cannot redefine it. Each is created on first write: reads on a
// nil map answer false, which is exactly the state of a map never written.
func (p *parser) markHeader(pk string) {
if p.headers == nil {
p.headers = make(map[string]bool, 4)
}
p.headers[pk] = true
}
func (p *parser) markFrozen(pk string) {
if p.frozen == nil {
p.frozen = make(map[string]bool, 4)
}
p.frozen[pk] = true
}
func (p *parser) markDotted(pk string) {
if p.dotted == nil {
p.dotted = make(map[string]bool, 4)
}
p.dotted[pk] = true
}
func (p *parser) markArray(pk string) {
if p.arrays == nil {
p.arrays = make(map[string]bool, 2)
}
p.arrays[pk] = true
}
// internKey returns the shared string for key bytes. The lookup works on the
// bytes directly, which the compiler lets run without allocating, so a
// repeated key costs no allocation at all and the tree stores one string per
// distinct key.
func (p *parser) internKey(b []byte) string {
if p.keys == nil {
p.keys = make(map[string]string, 16)
}
if s, ok := p.keys[string(b)]; ok {
return s
}
s := string(b)
p.keys[s] = s
return s
}
// --- table headers ---------------------------------------------------------
func (p *parser) parseTableHeader() error {
@@ -176,7 +240,7 @@ func (p *parser) parseTableHeader() error {
p.pos++
}
key, err := p.parseKeyPath()
first, rest, err := p.parseKeyPath()
if err != nil {
return err
}
@@ -193,6 +257,15 @@ func (p *parser) parseTableHeader() error {
p.pos++
}
// The key the rest of the header handling reads. parseKeyPath hands back
// a transient buffer for the single-segment case, the shape every
// repeated array-of-tables header has; anything longer is copied once.
key := p.keyBuf[:1]
key[0] = first
if len(rest) > 0 {
key = append([]string{first}, rest...)
}
if array {
tbl, elem, err := p.appendArrayTable(key)
if err != nil {
@@ -201,9 +274,9 @@ func (p *parser) parseTableHeader() error {
// A new array-of-tables element starts a fresh scope: sub-table headers
// and inline-table freezes from the previous element no longer apply.
p.resetScopeUnder(key)
p.arrays[pathKey(key)] = true
p.markArray(pathKey(key))
p.current = tbl
p.currentPath = key
p.currentPath = p.retainPath(key)
p.currentNode = elem
p.lastTable = elem
return nil
@@ -213,29 +286,52 @@ func (p *parser) parseTableHeader() error {
if p.headers[pk] || p.dotted[pk] || p.arrays[pk] {
return p.errf("table %q is defined more than once", strings.Join(key, "."))
}
p.headers[pk] = true
p.markHeader(pk)
tbl, node, err := p.tableAt(key)
if err != nil {
return err
}
p.current = tbl
p.currentPath = key
p.currentPath = p.retainPath(key)
p.currentNode = node
p.lastTable = node
return nil
}
// retainPath copies key into the parser-owned storage currentPath holds, so
// the transient key buffer is free to serve the next statement.
func (p *parser) retainPath(key []string) []string {
if cap(p.currentPath) < len(key) {
p.currentPath = make([]string, len(key))
} else {
p.currentPath = p.currentPath[:len(key)]
}
copy(p.currentPath, key)
return p.currentPath
}
// tableAt walks (creating intermediate tables) to the table named by key,
// relative to the document root, rejecting any step into a frozen inline table.
func (p *parser) tableAt(key []string) (map[string]any, *Table, error) {
cur := p.root
node := p.doc
path := make([]string, 0, len(key))
// The intermediate-path bookkeeping allocates only when the key actually
// has intermediate segments; a single-segment key checks its own name.
var path []string
if len(key) > 1 {
path = make([]string, 0, len(key))
}
for _, k := range key {
path = append(path, k)
if p.frozen[pathKey(path)] {
return nil, nil, p.errf("cannot extend inline table %q", strings.Join(path, "."))
if len(key) == 1 {
if p.frozen[k] {
return nil, nil, p.errf("cannot extend inline table %q", k)
}
} else {
path = append(path, k)
if p.frozen[pathKey(path)] {
return nil, nil, p.errf("cannot extend inline table %q", strings.Join(path, "."))
}
}
existing, ok := cur[k]
if !ok {
@@ -271,7 +367,12 @@ func (p *parser) tableAt(key []string) (map[string]any, *Table, error) {
func (p *parser) appendArrayTable(key []string) (map[string]any, *Table, error) {
parent := p.root
node := p.doc
path := make([]string, 0, len(key))
// As in tableAt, the path slice exists only for a multi-segment key; the
// loop below runs for those alone.
var path []string
if len(key) > 1 {
path = make([]string, 0, len(key))
}
for _, k := range key[:len(key)-1] {
path = append(path, k)
if p.frozen[pathKey(path)] {
@@ -323,7 +424,7 @@ func (p *parser) appendArrayTable(key []string) (map[string]any, *Table, error)
// --- key/value -------------------------------------------------------------
func (p *parser) parseKeyValue() error {
key, err := p.parseKeyPath()
first, rest, err := p.parseKeyPath()
if err != nil {
return err
}
@@ -340,47 +441,46 @@ func (p *parser) parseKeyValue() error {
}
dest := p.current
// One allocation covers the current section plus the dotted key; a
// top-level statement reuses it for the leaf.
abs := make([]string, 0, len(p.currentPath)+len(key))
abs = append(abs, p.currentPath...)
// The absolute path of the key drives the dotted-key bookkeeping and the
// inline-table freeze. A single top-level key needs it only for the
// freeze, where a one-element path sits in the parser's scratch.
var abs []string
if len(rest) > 0 || len(p.currentPath) > 0 {
abs = make([]string, 0, len(p.currentPath)+len(rest)+1)
abs = append(abs, p.currentPath...)
abs = append(abs, first)
} else {
abs = append(p.absScratch[:0], first)
}
// dests collects the map each dotted key descended into, which the node
// tree needs to build the matching tables around the value.
var dests []map[string]any
for _, k := range key[:len(key)-1] {
abs = append(abs, k)
if p.frozen[pathKey(abs)] {
return p.errf("cannot extend inline table %q", strings.Join(abs, "."))
leaf := first
if len(rest) > 0 {
if err := p.descendKey(&dest, first, abs, &dests); err != nil {
return err
}
if p.headers[pathKey(abs)] {
return p.errf("cannot extend table %q with a dotted key", strings.Join(abs, "."))
for _, k := range rest[:len(rest)-1] {
abs = append(abs, k)
if err := p.descendKey(&dest, k, abs, &dests); err != nil {
return err
}
}
p.dotted[pathKey(abs)] = true
existing, ok := dest[k]
if !ok {
next := map[string]any{}
dest[k] = next
dest = next
dests = append(dests, next)
continue
}
m, ok := existing.(map[string]any)
if !ok {
return p.errf("key %q is not a table", k)
}
dest = m
dests = append(dests, m)
leaf = rest[len(rest)-1]
abs = append(abs, leaf)
}
leaf := key[len(key)-1]
abs = append(abs, leaf)
if _, exists := dest[leaf]; exists {
return p.errf("duplicate key %q", leaf)
}
dest[leaf] = val
if p.doc != nil {
node := p.currentNode
for i, k := range key[:len(key)-1] {
node = node.addTable(k, dests[i])
if len(rest) > 0 {
node = node.addTable(first, dests[0])
for i, k := range rest[:len(rest)-1] {
node = node.addTable(k, dests[i+1])
}
}
_, inline := val.(map[string]any)
entry := node.addValue(leaf, val, inline)
@@ -396,6 +496,35 @@ func (p *parser) parseKeyValue() error {
return nil
}
// descendKey walks dest into the sub-table named key on the dotted path abs,
// recording the path in the definition maps; dests collects the maps
// descended into.
func (p *parser) descendKey(dest *map[string]any, key string, abs []string, dests *[]map[string]any) error {
ak := pathKey(abs)
if p.frozen[ak] {
return p.errf("cannot extend inline table %q", strings.Join(abs, "."))
}
if p.headers[ak] {
return p.errf("cannot extend table %q with a dotted key", strings.Join(abs, "."))
}
p.markDotted(ak)
existing, ok := (*dest)[key]
if !ok {
next := map[string]any{}
(*dest)[key] = next
*dest = next
*dests = append(*dests, next)
return nil
}
m, ok := existing.(map[string]any)
if !ok {
return p.errf("key %q is not a table", key)
}
*dest = m
*dests = append(*dests, m)
return nil
}
// takeInline returns the node of the inline table just parsed, when v is that
// table's value, and clears it so a later value cannot pick it up.
func (p *parser) takeInline(v any) *Table {
@@ -419,16 +548,17 @@ func (p *parser) takeArrayElems(v any) []*Table {
}
// freezeInline marks the path of an inline table (and any nested inline tables)
// as immutable, so a later header or dotted key cannot extend it.
// as immutable, so a later header or dotted key cannot extend it. The
// recursion appends into the caller's path slice; the frozen map keeps the
// joined strings, never the slice, so the backing is free to be reused.
func (p *parser) freezeInline(path []string, val any) {
m, ok := val.(map[string]any)
if !ok {
return
}
p.frozen[pathKey(path)] = true
p.markFrozen(pathKey(path))
for k, v := range m {
child := append(append([]string{}, path...), k)
p.freezeInline(child, v)
p.freezeInline(append(path, k), v)
}
}
@@ -438,33 +568,54 @@ func (p *parser) freezeInline(path []string, val any) {
// fresh in the new element.
func (p *parser) resetScopeUnder(key []string) {
prefix := pathKey(key) + "\x00"
for _, m := range []map[string]bool{p.headers, p.frozen, p.dotted, p.arrays} {
for k := range m {
if strings.HasPrefix(k, prefix) {
delete(m, k)
}
p.resetMapUnder(p.headers, prefix)
p.resetMapUnder(p.frozen, prefix)
p.resetMapUnder(p.dotted, prefix)
p.resetMapUnder(p.arrays, prefix)
}
// resetMapUnder deletes the entries m holds under prefix. An empty or
// unallocated map holds none, so the common case walks nothing.
func (p *parser) resetMapUnder(m map[string]bool, prefix string) {
if len(m) == 0 {
return
}
for k := range m {
if strings.HasPrefix(k, prefix) {
delete(m, k)
}
}
}
// parseKeyPath parses a dotted key into its components.
func (p *parser) parseKeyPath() ([]string, error) {
var parts []string
// parseKeyPath parses a dotted key. The first component comes back directly
// and the rest as a usually nil slice, because a single-component key is the
// common shape and a fresh slice per statement is what the allocation profile
// showed. The single-key slice a caller sees is parser-owned and transient.
func (p *parser) parseKeyPath() (string, []string, error) {
p.skipInline()
first, err := p.parseKeyComponent()
if err != nil {
return "", nil, err
}
p.skipInline()
if p.eof() || p.peek() != '.' {
return first, nil, nil
}
p.pos++
var rest []string
for {
p.skipInline()
part, err := p.parseKeyComponent()
if err != nil {
return nil, err
return "", nil, err
}
parts = append(parts, part)
rest = append(rest, part)
p.skipInline()
if !p.eof() && p.peek() == '.' {
p.pos++
continue
if p.eof() || p.peek() != '.' {
return first, rest, nil
}
break
p.pos++
}
return parts, nil
}
func (p *parser) parseKeyComponent() (string, error) {
@@ -493,11 +644,18 @@ func (p *parser) parseKeyComponent() (string, error) {
}
break
}
// The stopping byte decides the message: a multi-byte sequence that
// does not decode names that, before any grammar message can.
if !p.eof() && p.peek() >= utf8.RuneSelf {
if r, size := utf8.DecodeRune(p.src[p.pos:]); r == utf8.RuneError && size == 1 {
return "", p.errf("invalid UTF-8 in key")
}
}
if p.pos == start {
r, _ := utf8.DecodeRune(p.src[p.pos:])
return "", p.errf("invalid key character %q", string(r))
}
return string(p.src[start:p.pos]), nil
return p.internKey(p.src[start:p.pos]), nil
}
}
@@ -544,6 +702,9 @@ func (p *parser) parseAtom() (any, error) {
if tok == "" {
return nil, p.errf("expected a value")
}
if hasHighByte(tok) && !utf8.ValidString(tok) {
return nil, p.errf("invalid UTF-8 in value")
}
// A date may be followed by a space and a time, forming one date-time.
if isDateToken(tok) && !p.eof() && p.peek() == ' ' {
if next, ok := p.peekAt(1); ok && next >= '0' && next <= '9' {
@@ -576,6 +737,17 @@ func (p *parser) scanBareToken() {
}
}
// hasHighByte reports whether s holds any byte outside ASCII, the cheap gate
// in front of a full UTF-8 check.
func hasHighByte(s string) bool {
for i := range len(s) {
if s[i] >= utf8.RuneSelf {
return true
}
}
return false
}
// --- strings ---------------------------------------------------------------
func (p *parser) parseBasicString() (string, error) {
@@ -583,7 +755,36 @@ func (p *parser) parseBasicString() (string, error) {
return p.parseMultilineString('"', true)
}
p.pos++ // opening quote
start := p.pos
// A run of plain characters up to the closing quote needs no builder, only
// one copy at the end; escapes, controls and multi-byte runes fall through
// to the builder loop, which validates them on the spot.
for p.pos < len(p.src) {
c := p.src[p.pos]
if c == '"' {
s := string(p.src[start:p.pos])
p.pos++
return s, nil
}
if c == '\\' || c == '\n' || c == '\r' || c >= utf8.RuneSelf ||
(c < 0x20 && c != '\t') || c == 0x7f {
break
}
p.pos++
}
if p.eof() {
return "", p.errf("unterminated string")
}
var b strings.Builder
b.Grow(p.pos - start)
b.Write(p.src[start:p.pos])
return p.parseBasicStringRest(&b)
}
// parseBasicStringRest continues a basic string whose fast scan has met a byte
// it does not handle: an escape, a control character, a multi-byte rune, or a
// bare newline, which the loop rejects.
func (p *parser) parseBasicStringRest(b *strings.Builder) (string, error) {
for {
if p.eof() {
return "", p.errf("unterminated string")
@@ -605,7 +806,7 @@ func (p *parser) parseBasicString() (string, error) {
}
b.WriteRune(r)
default:
if err := p.writeContentRune(&b); err != nil {
if err := p.writeContentRune(b); err != nil {
return "", err
}
}
@@ -617,7 +818,27 @@ func (p *parser) parseLiteralString() (string, error) {
return p.parseMultilineString('\'', false)
}
p.pos++ // opening quote
start := p.pos
// The same fast scan as the basic string, without the escape case.
for p.pos < len(p.src) {
c := p.src[p.pos]
if c == '\'' {
s := string(p.src[start:p.pos])
p.pos++
return s, nil
}
if c == '\n' || c == '\r' || c >= utf8.RuneSelf ||
(c < 0x20 && c != '\t') || c == 0x7f {
break
}
p.pos++
}
if p.eof() {
return "", p.errf("unterminated literal string")
}
var b strings.Builder
b.Grow(p.pos - start)
b.Write(p.src[start:p.pos])
for {
if p.eof() {
return "", p.errf("unterminated literal string")
@@ -641,8 +862,8 @@ func (p *parser) parseLiteralString() (string, error) {
// writeContentRune appends the rune at the cursor to b and advances past it.
// An ASCII byte, which includes every control character the grammar forbids,
// is checked and written directly; a multi-byte rune is decoded and can never
// be a control character.
// is checked and written directly; a multi-byte rune is decoded, and a
// sequence that does not decode is the UTF-8 error reported where it sits.
func (p *parser) writeContentRune(b *strings.Builder) error {
c := p.peek()
if c < utf8.RuneSelf {
@@ -654,6 +875,9 @@ func (p *parser) writeContentRune(b *strings.Builder) error {
return nil
}
r, size := utf8.DecodeRune(p.src[p.pos:])
if r == utf8.RuneError && size == 1 {
return p.errf("invalid UTF-8 in string")
}
p.pos += size
b.WriteRune(r)
return nil
@@ -831,7 +1055,10 @@ func (p *parser) parseArray() (val any, err error) {
}
defer p.leaveNesting()
p.pos++ // '['
arr := []any{}
// A small presize covers the arrays documents actually hold, and trades a
// little capacity on tiny arrays for the growth chain an append-from-nil
// costs per array.
arr := make([]any, 0, 4)
// elems carries the node of each element that is an inline table, so the
// caller can keep its key order; the entries are nil for other values.
var elems []*Table
@@ -886,7 +1113,9 @@ func (p *parser) parseInlineTable() (val any, err error) {
defer p.leaveNesting()
p.pos++ // '{'
tbl := map[string]any{}
assigned := map[string]bool{}
// assigned tracks the dotted paths written into this table. It is created
// on the first key, so an empty inline table allocates nothing for it.
var assigned map[string]bool
// The inline table is a node of its own, so the keys keep their order; the
// caller picks the node up when the table parses.
var node *Table
@@ -913,7 +1142,7 @@ func (p *parser) parseInlineTable() (val any, err error) {
if err := p.skipNestedSpace(); err != nil {
return nil, err
}
key, err := p.parseKeyPath()
first, rest, err := p.parseKeyPath()
if err != nil {
return nil, err
}
@@ -929,39 +1158,46 @@ func (p *parser) parseInlineTable() (val any, err error) {
}
dest := tbl
path := make([]string, 0, len(key))
var path []string
var dests []map[string]any
for _, k := range key[:len(key)-1] {
path = append(path, k)
if assigned[pathKey(path)] {
return nil, p.errf("key %q is already defined", strings.Join(path, "."))
leaf := first
if len(rest) > 0 {
path = append(p.absScratch[:0], first)
d, err := p.descendInline(&dest, first, path, assigned)
if err != nil {
return nil, err
}
existing, ok := dest[k]
if !ok {
m := map[string]any{}
dest[k] = m
dest = m
dests = append(dests, m)
continue
dests = append(dests, d)
for _, k := range rest[:len(rest)-1] {
path = append(path, k)
d, err := p.descendInline(&dest, k, path, assigned)
if err != nil {
return nil, err
}
dests = append(dests, d)
}
m, isMap := existing.(map[string]any)
if !isMap {
return nil, p.errf("key %q is already defined", k)
}
dest = m
dests = append(dests, m)
leaf = rest[len(rest)-1]
path = append(path, leaf)
}
leaf := key[len(key)-1]
path = append(path, leaf)
if _, exists := dest[leaf]; exists {
return nil, p.errf("duplicate key %q in inline table", leaf)
}
dest[leaf] = val
assigned[pathKey(path)] = true
if assigned == nil {
assigned = make(map[string]bool, 4)
}
if len(rest) == 0 {
assigned[first] = true
} else {
assigned[pathKey(path)] = true
}
if node != nil {
child := node
for i, k := range key[:len(key)-1] {
child = child.addTable(k, dests[i])
if len(rest) > 0 {
child = child.addTable(first, dests[0])
for i, k := range rest[:len(rest)-1] {
child = child.addTable(k, dests[i+1])
}
}
_, inline := val.(map[string]any)
entry := child.addValue(leaf, val, inline)
@@ -998,6 +1234,28 @@ func (p *parser) parseInlineTable() (val any, err error) {
}
}
// descendInline walks dest into the sub-table named key inside an inline
// table, rejecting a dotted segment the table has already defined.
func (p *parser) descendInline(dest *map[string]any, key string, path []string, assigned map[string]bool) (map[string]any, error) {
pk := pathKey(path)
if assigned[pk] {
return nil, p.errf("key %q is already defined", strings.Join(path, "."))
}
existing, ok := (*dest)[key]
if !ok {
m := map[string]any{}
(*dest)[key] = m
*dest = m
return m, nil
}
m, isMap := existing.(map[string]any)
if !isMap {
return nil, p.errf("key %q is already defined", key)
}
*dest = m
return m, nil
}
// --- scanning helpers ------------------------------------------------------
func (p *parser) eof() bool { return p.pos >= len(p.src) }
@@ -1121,8 +1379,14 @@ func (p *parser) skipComment() (string, error) {
p.pos++
case c < 0x20 || c == 0x7f:
return "", p.errf("control character U+%04X is not allowed in a comment", c)
default:
case c < utf8.RuneSelf:
p.pos++
default:
r, size := utf8.DecodeRune(p.src[p.pos:])
if r == utf8.RuneError && size == 1 {
return "", p.errf("invalid UTF-8 in comment")
}
p.pos += size
}
}
return commentText(string(p.src[start:p.pos])), nil
@@ -1176,7 +1440,10 @@ func (p *parser) expectLineEnd() error {
p.pos++
return nil
}
r, _ := utf8.DecodeRune(p.src[p.pos:])
r, size := utf8.DecodeRune(p.src[p.pos:])
if r == utf8.RuneError && size == 1 {
return p.errf("invalid UTF-8 after value")
}
return p.errf("unexpected %q after value", string(r))
}
@@ -1185,7 +1452,8 @@ func (p *parser) errf(format string, args ...any) error {
}
// pathKey joins key components with a NUL separator so a dotted path can be
// used as a map key for tracking defined tables.
// used as a map key for tracking defined tables. A single component comes
// back as it is, with no join and no copy.
func pathKey(parts []string) string {
return strings.Join(parts, "\x00")
}