-
-
Notifications
You must be signed in to change notification settings - Fork 32
Add ReadOnlyMemory<char> support #100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Conversation
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
Line text can be implicitly converted from string and from ReadonlyMemory<char>
Optimizations applied: - Use ArrayPool<char> instead of allocating new char[] per line in Grammar.Tokenize() - Replace new Stopwatch() with Stopwatch.GetTimestamp() in LineTokenizer.Scan() - Pool List<LocalStackElement> and List<WhileStack> in LineTokenizer - Cache GetScopeNames() result in AttributedScopeStack - Avoid List<string> allocation for single-scope PushAtributed() Benchmark results (133K line file): - Execution time: 4.75s → 2.89s (39% faster) - Memory allocated: 658 MB → 488 MB (26% less) - Gen0 collections: 82K → 61K (26% fewer) - Gen1 collections: 8K → 4K (50% fewer)
…ilures The ArrayPool<char> optimization in Grammar.Tokenize() caused test failures on x64 Linux/Windows CI while passing on ARM64 macOS locally. Root cause: The rented buffer was returned to the pool in the finally block while LineTokens still held a ReadOnlyMemory<char> reference to it. On x64 platforms with aggressive buffer reuse, subsequent tokenize calls would reuse and overwrite the buffer, corrupting previous results. The other performance optimizations from commit 0c1c0aa remain intact: - Stopwatch.GetTimestamp() instead of new Stopwatch() - Pooled List<LocalStackElement> and List<WhileStack> in LineTokenizer - Cached GetScopeNames() in AttributedScopeStack - Single-scope PushAtributed() optimization
… test failures" This reverts commit 8e20d42.
… by 39%" This reverts commit 0c1c0aa.
…Tokenizer.Scan()" This reverts commit 54a330c.
This was referenced Dec 15, 2025
This was referenced Dec 15, 2025
This was referenced Dec 22, 2025
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.
Summary
This PR introduces
ReadOnlyMemory<char>support throughout the tokenization pipeline, enabling zero-allocation text handling. Combined with several allocation reduction optimizations, this delivers significant performance and memory improvements.What's New
New
LineTextTypeA new
LineTextstruct wrapsReadOnlyMemory<char>, providing a clean API for text handling without string allocations:Updated Public APIs
IGrammar.TokenizeLine()andTokenizeLine2()now acceptLineTextinstead ofstringIModelLines.GetLineText()returnsLineTextinstead ofstringstringtoLineTextmaintain backward compatibilityBenchmark Results
Tested with a 133,439 line C# file (5.8 MB):
Optimizations Applied
Stopwatch.GetTimestamp()instead ofnew Stopwatch()HandleCaptures,CheckWhileConditions)GetScopeNames()result to avoid repeated list creationList<string>allocation for single scope pushesIModelLines.GetLineText()now returnsLineTextinstead ofstringIGrammar.TokenizeLine()signature changed to acceptLineTextMigration: Replace
stringwithLineTextin implementations. The implicit conversion fromstringmeans most call sites work unchanged.Testing