-
Notifications
You must be signed in to change notification settings - Fork 2.4k
perf: use list join instead of string concatenation in loop #3829
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
base: develop
Are you sure you want to change the base?
perf: use list join instead of string concatenation in loop #3829
Conversation
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.
Sorry to be nitpicky, suggestion is not a bad one! Also, if you could check the formatting by running make pr
locally, that would help get the workflows to pass.
# OLD APPROACH: String concatenation in loop | ||
# self.function_names.setdefault(api_name, "") | ||
# self.function_names[api_name] += str(resolved_function_name) | ||
# NEW APPROACH: Collect in list |
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 we need the comment with the old approach.
# OLD: self.function_names: Dict[Any, Any] = {} | ||
# NEW: Use List[str] for efficient accumulation, convert to str when needed |
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.
Same suggestion on commenting the old approach.
if api_name not in self.function_names: | ||
self.function_names[api_name]=[] |
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.
if api_name not in self.function_names: | |
self.function_names[api_name]=[] | |
self.function_names.setdefault(api_name, []) |
if api_name not in self.function_names: | ||
self.function_names[api_name]=[] | ||
self.function_names[api_name].append(str(resolved_function_name)) | ||
#backward compatibility |
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.
Comment is also probably unnecessary.
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.
okayy
Issue #, if available
N/A - Performance optimization proposal
Description of changes
Replace O(n^2) string concatenation with O(n) list accumulation in
_get_function_names()
method.Current approach: Uses
+=
operator in loop, creating new string objects on each iterationProposed approach: Accumulate in list, join at end using
''.join()
This maintains backward compatibility.
Description of how you validated changes
Dict[str, List[str]]
)Checklist
Examples?
N/A - Internal performance optimization
Note: This is not a critical fix...
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.