-
-
Notifications
You must be signed in to change notification settings - Fork 3
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
Add a proof of concept for using GetAlternateLookup
#62
base: main
Are you sure you want to change the base?
Conversation
This permits zero-allocation lookups in the FastEncoder dictionary using segments of an input string.
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
@@ -89,23 +98,40 @@ public int CountTokensNative(string text) | |||
var textSpan = text.AsSpan(); | |||
Span<byte> pieceBytes = stackalloc byte[128]; | |||
#endif | |||
#if NET9_0_OR_GREATER | |||
var fastEncoderLookup = FastEncoder.GetAlternateLookup<ReadOnlySpan<char>>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here you use FastEncoder in addition to FastCacheCounts, but you didn't pass new AlternateStringComparer() to it.
Is this implemented by default for regular Dictionary and is only needed for ConcurrentDictionary?
P.S. I'm a bit out of context for now, I'll look into this in the next few days (I saw links on your blog, I'll look into this)
Thanks a lot for the research in this direction, it's rare to find people who are passionate about such a low level except for Microsoft employees. |
Not entirely sure, but also created this issue to explore another possible improvement - Regex.EnumerateSplits - #64 |
.NET 9 adds support for collection lookups with spans. This permits zero-allocation lookups in the
FastEncoder
dictionary using segments of an input string.This PR is a proof-of-concept to start discussion about supporting this more fully when .NET 9 comes out. The performance gains are small, but the reduction in allocations from the lookups is significant. The downside is a significant increase in conditional compilation directives throughout the code.