⚡️ Speed up function get_grammars
by 128% in src/black/parsing.py
#67
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.
📄
get_grammars
insrc/black/parsing.py
✨ Performance Summary:
128%
(1.28x
faster)4.08 microseconds
down to1.79 microseconds
(best of5
runs)📝 Explanation and details
Here is the rewritten program with some optimization techniques.
Optimizations Made.
Inheritance & Initialization.
Grammar
now inherits fromBlibGrammar
to avoid manual initialization of inherited attributes.String Formatting.
assert
inpretty
method to avoid redundant runtime checks.Conditional Checking.
get_grammars
by usingsupports_feature
function efficiently and avoiding redundant calls for same feature checks.Code Maintenance.
__dict__.update
inload
,_update
, andcopy
methods for optimized deep copying of attributes.Type Hints.
These changes should improve the performance and readability of the code.
✅ Correctness verification
The new optimized code was tested for correctness. The results are listed below:
🌀 Generated Regression Tests Details
Click to view details
📣 **Feedback**
If you have any feedback or need assistance, feel free to join our Discord community: