Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR focuses on reducing allocations and improving throughput in the core TOML parsing path for CsTomlSerializer.Deserialize<TomlDocument>, mainly by pre-sizing table dictionaries, reusing parsed key instances, and tightening hot-path parsing loops.
Changes:
- Add a best-effort key/value count estimator to pre-size
TomlTableNodeDictionaryand reduce rehash/reserve churn during document parse. - Reuse previously-parsed
TomlDottedKeyinstances viaTomlTableNodeHolder/mirror-node lookup to avoid repeated key parsing/allocation. - Optimize array scanning and key parsing hot paths in
CsTomlReader(bitmask classification, reduced branching) and add a multi-segment array regression test.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/CsToml.Tests/ArrayReadMultiSegmentTest.cs | Adds coverage for ReadOnlySequence<byte> multi-segment array parsing edge cases. |
| src/CsToml/Values/TomlTableNodeHolder.cs | Introduces a small holder to enable key reuse from existing table nodes. |
| src/CsToml/Values/TomlTableNodeDictionary.cs | Adds initial capacity estimation support and enables returning the stored key instance. |
| src/CsToml/Values/TomlTableNode.cs | Adds capacity reservation and key/mirror lookup helpers to support key reuse. |
| src/CsToml/Values/TomlTable.GetValue.cs | Removes an unused using. |
| src/CsToml/Values/TomlDottedKey.cs | Removes unused error namespace import. |
| src/CsToml/TomlDocument.cs | Wires key reuse + capacity estimation into the document parse loop. |
| src/CsToml/Formatter/PrimitiveObjectFormatter.cs | Switches type-jump table to FrozenDictionary for faster lookups / fewer allocations. |
| src/CsToml/Formatter/EnumFormatter.cs | Switches enum lookup tables to FrozenDictionary and refactors static initialization. |
| src/CsToml/CsTomlReader.cs | Adds key-count estimation and refactors key/array parsing hot paths. |
| src/CsToml/CsTomlParser.cs | Updates call site for the new generic TrySkipIfNewLine API. |
Comments suppressed due to low confidence (6)
src/CsToml/Values/TomlTableNodeDictionary.cs:207
- The bounds check in TryGetValueCore can allow index == entries.Length, which would throw IndexOutOfRangeException when accessing entries[index]. Use >= to reject that case (and still handle -1 via the uint cast).
if ((uint)index > (uint)entries.Length)
src/CsToml/CsTomlReader.cs:946
- Remove the commented-out return; the method already returns via nodeHolder.GetKey and the commented code is dead.
return nodeHolder.GetKey<T>(bufferWriter.WrittenSpan);
//return T.Parse(bufferWriter.WrittenSpan);
src/CsToml/CsTomlReader.cs:1182
- Remove the commented-out return; it’s dead code and makes the intended behavior ambiguous.
return nodeHolder.GetKey<T>(unreadSpan[..totalLength]);
//return T.Parse(unreadSpan[..totalLength]);
src/CsToml/CsTomlReader.cs:1191
- Remove the commented-out return; it’s dead code and makes the intended behavior ambiguous.
return nodeHolder.GetKey<T>(bufferWriter!.WrittenSpan);
//return T.Parse(bufferWriter!.WrittenSpan);
src/CsToml/CsTomlReader.cs:1385
- Remove the commented-out constructor call; nodeHolder.GetKey is now the single intended path.
return nodeHolder.GetKey<TomlUnquotedDottedKey>(unreadSpan[..totalLength]);
//return new TomlUnquotedDottedKey(unreadSpan[..totalLength]);
src/CsToml/CsTomlReader.cs:1390
- Remove the commented-out constructor call; nodeHolder.GetKey is now the single intended path.
return nodeHolder.GetKey<TomlUnquotedDottedKey>(bufferWriter!.WrittenSpan);
//return new TomlUnquotedDottedKey(bufferWriter!.WrittenSpan);
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR optimize TOML parsing in
CsTomlSerializer.Deserialize<TomlDocument>.This Change reduces memory allocation in main/common use cases.
Changed
TomlTableNodeDictionaryfrom key/value pairsTomlDottedKeyinTomlTableNodeulong) inReadUnquotedString/ReadArrayBenchmark