Feature/performance: This PR introduces a high number of performance improvements.#33
Feature/performance: This PR introduces a high number of performance improvements.#33r-Larch wants to merge 42 commits intodmitry-brazhenko:mainfrom
Conversation
…to support broader use
|
Hello @r-Larch Thanks A LOT for this PR. I will review it and merge it within 1-2 days. |
|
I will edit it a little bit and resolve all conflicts and merge after that. Trying to understand some of the changes |
r-Larch
left a comment
There was a problem hiding this comment.
I've added some comments and explanations to code decisions.
| if (searchValues is string[] { Length: 0 }) | ||
| { | ||
| return new FoundMatch { Success = false }; | ||
| } |
There was a problem hiding this comment.
This if is a fast path to not have searchValues.GetEnumerator() heap allocation in case allowSpecials is empty (not provided)
This is the default case so this fast path will be taken most of the time.
I could have added a small comment..
This if could be added to net6.0 and netstandard too.
SharpToken/Lib/Internals/BytePairEncodingCore/BytePairEncodingCore.net8.cs
Show resolved
Hide resolved
|
I am merging this change here: #36 thanks a lot for contribution! If you observe any issues, please let me know or open a new PR |
This PR delivers a suite of performance enhancements, primarily spurred by the discussion in issue #2.
These optimizations were urgently needed by me.
I changed a lot in this repository. I hope this changes are welcome and get merged.
Please tell me if something does not comply with your guidelines or code styles.
Key Highlights:
.NET 8.0due to its superior support for high-performance coding practices..NET 6.0and.NET Standardhave also received optimizations, they do not support certain APIs necessary for achieving the same level of performance enhancement as.NET 8.0.For detailed performance metrics, refer to the Performance Benchmarks.
Additionally, this PR introduces a high-performance, low-allocation
TokenCountmethod. This method is essential for my use case and, I believe, beneficial for others. It primarily facilitates the counting of tokens prior to dispatching a prompt to a LLM, ensuring that the prompt does not surpass the context window size limit. This functionality is also advantageous for segmenting text into manageable chunks for RAG.EDIT
With my latest commits, I was able to improve speed. With these changes, SharpToken effectively becomes the fastest library for .NET with the lowest allocations.
Performance Overview:
Code: CompareBenchmark.cs
After Optimization:
Before Optimization:
Older benchmarks
Before Optimization:
After Optimization: