From 4d8f127b68a77a7410ec0a82ee9004ae9a72d48c Mon Sep 17 00:00:00 2001 From: Daniel Duan Date: Thu, 5 Feb 2026 23:11:34 -0800 Subject: [PATCH] Reserve parser storage upfront Estimate key/value and node counts from input size and reserve parser arrays before tokenization. Cut dynamic array growth and copy churn in hot parse paths. --- Sources/TOMLDecoder/Parsing/Parser.swift | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Sources/TOMLDecoder/Parsing/Parser.swift b/Sources/TOMLDecoder/Parsing/Parser.swift index 5474ae9..ccbf5ea 100644 --- a/Sources/TOMLDecoder/Parsing/Parser.swift +++ b/Sources/TOMLDecoder/Parsing/Parser.swift @@ -13,6 +13,15 @@ struct Parser: ~Copyable { var keyTransform: (@Sendable (String) -> String)? mutating func parse(bytes: UnsafeBufferPointer) throws(TOMLError) { + let estimatedKeyValues = max(32, bytes.count / 48) + let estimatedNodes = max(8, estimatedKeyValues / 8) + tables.reserveCapacity(max(8, estimatedNodes)) + arrays.reserveCapacity(estimatedNodes) + keyTables.reserveCapacity(estimatedNodes) + keyArrays.reserveCapacity(estimatedNodes) + keyValues.reserveCapacity(estimatedKeyValues) + tablePath.reserveCapacity(8) + while token.kind != .eof { switch token.kind { case .newline: