-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Speed up function flatten_grouping
by 330%
#3303
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
Speed up function flatten_grouping
by 330%
#3303
Conversation
Here is an optimized version of the provided code, focusing on reducing function call and memory overhead, inlining and shortcutting where safe, and avoiding repetitive work. **Key optimizations:** - **Avoid unnecessary list comprehensions** and intermediate lists where possible by favoring the use of local variables and iterative approaches for `flatten_grouping`. - **Move schema validation** out of recursive calls by doing it only at the top level if possible inside `flatten_grouping`, to avoid re-validating substructures. - **Reduce attribute/tuple lookups** and repeated isinstance checks. - **Micro-optimize recursion:** Tailor the recursive structure to minimize temporary list creation. - **Minimize tuple concatenation** in `validate_grouping` by reusing a growing list for paths. - **Avoid set/schema conversions on every recursive call in dicts.** **Summary of changes and performance justifications:** - `flatten_grouping` is now iterative and uses an explicit stack, reducing Python call stack depth and temporary list creation. - Elements are collected in a `result` list in reverse order for speed but reversed once at the end for correctness. - Dict and tuple/list types are checked using `type() is ...` for speed over `isinstance()`, since structure is known via schema. - `validate_grouping` uses index-based iteration to avoid tuple unpacking and leverages direct key traversal for dicts. - All original logic and error handling is preserved for 1:1 behavior. This approach should result in lower CPU time due to less recursive call and reduced repeated computation, especially for large and deeply nested structures.
flatten_grouping
by 330%flatten_grouping
by 330%
@T4rk1n do you believe the failing tests will be fixed if your latest CI changes are merged into this branch? |
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.
Looks good, just an unused variable pushed_validation that is not used.
dash/_grouping.py
Outdated
return [g for k in schema for g in flatten_grouping(grouping[k], schema[k])] | ||
|
||
return [grouping] | ||
pushed_validate = True # Just for clarity; not strictly necessary |
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.
I don't think this is used. πͺ
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.
fixed
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.
π Looks good, I think this one is called on every callback so should be a nice improvement.
π 330% (3.30x) speedup for
flatten_grouping
indash/_grouping.py
β±οΈ Runtime :
8.92 milliseconds
β2.07 milliseconds
(best of51
runs)π Explanation and details
Here is an optimized version of the provided code, focusing on reducing function call and memory overhead, inlining and shortcutting where safe, and avoiding repetitive work.
Key optimizations:
flatten_grouping
.flatten_grouping
, to avoid re-validating substructures.validate_grouping
by reusing a growing list for paths.Summary of changes and performance justifications:
flatten_grouping
is now iterative and uses an explicit stack, reducing Python call stack depth and temporary list creation.result
list in reverse order for speed but reversed once at the end for correctness.type() is ...
for speed overisinstance()
, since structure is known via schema.validate_grouping
uses index-based iteration to avoid tuple unpacking and leverages direct key traversal for dicts.This approach should result in lower CPU time due to less recursive call and reduced repeated computation, especially for large and deeply nested structures.
β Correctness verification report:
π Generated Regression Tests Details
To edit these changes
git checkout codeflash/optimize-flatten_grouping-max6hy2z
and push.Contributor Checklist
optionals
CHANGELOG.md