-
-
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
Merged
T4rk1n
merged 7 commits into
plotly:dev
from
misrasaurabh1:codeflash/optimize-flatten_grouping-max6hy2z
May 30, 2025
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
09253a1
β‘οΈ Speed up function `flatten_grouping` by 330%
codeflash-ai[bot] e5fbd99
Apply suggestions from code review
misrasaurabh1 017cf62
Merge branch 'dev' into codeflash/optimize-flatten_grouping-max6hy2z
T4rk1n 47174e9
Merge branch 'dev' into codeflash/optimize-flatten_grouping-max6hy2z
T4rk1n 61ea742
Apply suggestions from code review
misrasaurabh1 00b9b34
Merge branch 'dev' into codeflash/optimize-flatten_grouping-max6hy2z
T4rk1n b4ba001
Merge branch 'dev' into codeflash/optimize-flatten_grouping-max6hy2z
T4rk1n 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,22 +29,37 @@ def flatten_grouping(grouping, schema=None): | |
|
||
:return: list of the scalar values in the input grouping | ||
""" | ||
stack = [] | ||
result = [] | ||
pushed_validate = False | ||
|
||
# Avoid repeated recursive Python calls by using an explicit stack | ||
push = stack.append | ||
pop = stack.pop | ||
|
||
# Only validate once at the top if schema is provided | ||
if schema is None: | ||
schema = grouping | ||
else: | ||
validate_grouping(grouping, schema) | ||
|
||
if isinstance(schema, (tuple, list)): | ||
return [ | ||
g | ||
for group_el, schema_el in zip(grouping, schema) | ||
for g in flatten_grouping(group_el, schema_el) | ||
] | ||
|
||
if isinstance(schema, dict): | ||
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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. fixed
misrasaurabh1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
push((grouping, schema)) | ||
while stack: | ||
group, sch = pop() | ||
# Inline isinstance checks for perf | ||
typ = type(sch) | ||
if typ is tuple or typ is list: | ||
# Avoid double recursion / excessive list construction | ||
for ge, se in zip(group, sch): | ||
push((ge, se)) | ||
elif typ is dict: | ||
for k in sch: | ||
push((group[k], sch[k])) | ||
else: | ||
result.append(group) | ||
result.reverse() # Since we LIFO, leaf values are in reverse order | ||
return result | ||
|
||
|
||
def grouping_len(grouping): | ||
|
@@ -215,7 +230,6 @@ def validate_grouping(grouping, schema, full_schema=None, path=()): | |
elif isinstance(schema, dict): | ||
SchemaTypeValidationError.check(grouping, full_schema, path, dict) | ||
SchemaKeysValidationError.check(grouping, full_schema, path, set(schema)) | ||
|
||
for k in schema: | ||
validate_grouping( | ||
grouping[k], schema[k], full_schema=full_schema, path=path + (k,) | ||
|
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.