⚡️ Speed up method ConfigWrapper.core_config by 28% in pydantic/_internal/_config.py
#9953
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