⚡️ Speed up method ConfigWrapper.core_config by 28% in pydantic/_internal/_config.py#9953
Merged
sydney-runkle merged 3 commits intopydantic:mainfrom Jul 30, 2024
Conversation
To optimize the runtime and memory usage of the provided program, I will implement a few key improvements. 1. **Avoid multiple calls to `dict.get()`**: Instead of repeatedly calling `self.config_dict.get()`, I will preprocess the configuration dictionary once, storing the desired values. 2. **Optimize the `dict_not_none()` function**: Rather than creating an intermediate dictionary with `kwargs`, I’ll directly construct a dictionary with non-`None` values. 3. **Simplify `__repr__` method formatting**: Construct the string more efficiently using list comprehension. Here's the rewritten code with the improvements. Key optimizations. 1. **Reduced dictionary lookups**: Instead of repeatedly calling `config.get()`, the values are fetched once and stored in `core_config_values`. 2. **More efficient dictionary filtering**: The final dictionary with non-`None` values is constructed directly and more efficiently compared to the previous approach. 3. **Efficient string construction in `__repr__`**: Using list comprehension to build the string directly.
CodSpeed Performance ReportMerging #9953 will not alter performanceComparing Summary
|
sydney-runkle
approved these changes
Jul 24, 2024
Contributor
sydney-runkle
left a comment
There was a problem hiding this comment.
Looks great, just a few change requests
Co-authored-by: Sydney Runkle <54324534+sydney-runkle@users.noreply.github.com>
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.
Change Summary
📄
ConfigWrapper.core_config()inpydantic/_internal/_config.py📈 Performance improved by
28%(0.28xfaster)⏱️ Runtime went down from
76.0 microsecondsto59.4 microsecondsExplanation and details
To optimize the runtime and memory usage of the provided program, I will implement a few key improvements.
Avoid multiple calls to
dict.get(): Instead of repeatedly callingself.config_dict.get(), I will preprocess the configuration dictionary once, storing the desired values.Optimize the
dict_not_none()function: Rather than creating an intermediate dictionary withkwargs, I’ll directly construct a dictionary with non-Nonevalues.Here's the rewritten code with the improvements.
Key optimizations.
config.get(), the values are fetched once and stored incore_config_values.Nonevalues is constructed directly and more efficiently compared to the previous approach.This optimization was automatically discovered with codeflash.ai
Correctness verification
The new optimized code was tested for correctness. The results are listed below.
🔘 (none found) − ⚙️ Existing Unit Tests
✅ 19 Passed − 🌀 Generated Regression Tests
(click to show generated tests)
✅ 2 Passed − ⏪ Replay Tests
Checklist