-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Speed up function _estimate_string_tokens
#2156
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
DouweM
merged 5 commits into
pydantic:main
from
misrasaurabh1:codeflash/optimize-_estimate_string_tokens-mcs8yg4q
Jul 23, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4c73470
⚡️ Speed up function `_estimate_string_tokens` by 221%
codeflash-ai[bot] d33baa4
Update pydantic_ai_slim/pydantic_ai/models/function.py
misrasaurabh1 270bed9
Apply suggestions from code review
misrasaurabh1 adabd7c
Merge branch 'main' into codeflash/optimize-_estimate_string_tokens-m…
misrasaurabh1 1836286
fix comment
misrasaurabh1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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.
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.
this will have some overhead at import time, it's small but it'll add up if we do this with all regular expressions. Should we stick with
re.split(r'[\s",.:]+', part.strip())
as it'll cache the regex the first time it's run.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.
To validate the performance characteristics, I tried an experiment where I replaced the current suggestion with inline re.split and ran it on the generated test set and timed the runtime. So the only change is the global re.compile vs inline re.split.
global re.compile time -> 1.68ms
inline re.split -> 2.57ms
Yes, regex does cache the complied regex for future use, but it has overhead that especially when used in a loop can be high. In my experience with optimizations discovered with codeflash, I've seen re.compile be faster.
In this case, since regex is used multiple times and in a loop i would recommend regex compilation. Although its your decision.
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.
This file needs to be imported explicitly, so when it's imported we can assume the
_estimate_string_tokens
function is going to be used, so compiling the regex at import time is fine. I'd feel different if this was in a file that's always imported by Pydantic AI itself, and we wouldn't know if the regex was actually going to be used.