You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
⚡️ Speed up function create_trace_replay_test_code by 50% in PR #294 (add-timing-info-to-generated-tests)
Here's an optimized and faster version of your program. The main performance inefficiencies in the original code are.
- **Repeated attribute accesses with `dict.get()` inside loops:** Pre-collecting values boosts efficiency.
- **Frequent string concatenations:** Use f-strings carefully and only when necessary.
- **Unnecessary use of `sorted` on a set each run.** Build this directly from the data.
- **Repeated construction of similar strings:** Precompute or simplify where possible.
- **Using `textwrap.indent` in a loop:** Combine with minimal copies.
- **No need for `textwrap.dedent` if formatting is already explicit.**
Below is the refactored code following these optimizations.
**Summary of the changes:**
- **Single pass for collecting imports and function names.**
- **Directly build up all test code as a list, for O(1) append performance and O(1) final string join.**
- **Minimized repeated calls to attribute-getting, string formatting, and function calls inside large loops.**
- **Efficient, manual indentation instead of `textwrap.indent`.**
- **Templates are constants, dedented only once.**
- **All constants precomputed outside the loop.**
This will make your test code generation much faster and with much less memory overhead for large `functions_data`. No function signature or comments have been changed except for the relevant section reflecting the new optimized approach.
for args_pkl, kwargs_pkl in get_next_arg_and_return(trace_file=trace_file_path, benchmark_function_name="{benchmark_function_name}", function_name="{orig_function_name}", file_path=r"{file_path}", num_to_get={max_run_count}):
for args_pkl, kwargs_pkl in get_next_arg_and_return(trace_file=trace_file_path, benchmark_function_name="{benchmark_function_name}", function_name="{orig_function_name}", file_path=r"{file_path}", class_name="{class_name}", num_to_get={max_run_count}):
for args_pkl, kwargs_pkl in get_next_arg_and_return(trace_file=trace_file_path, benchmark_function_name="{benchmark_function_name}", function_name="{orig_function_name}", file_path=r"{file_path}", class_name="{class_name}", num_to_get={max_run_count}):
' raise ValueError("No arguments provided for the method.")\n'
116
+
' if function_name == "__init__":\n'
117
+
" ret = {class_name_alias}(*args[1:], **kwargs)\n"
118
+
" else:\n"
119
+
" ret = {class_name_alias}{method_name}(*args, **kwargs)\n"
133
120
)
134
-
test_static_method_body=textwrap.dedent(
135
-
"""\
136
-
for args_pkl, kwargs_pkl in get_next_arg_and_return(trace_file=trace_file_path, benchmark_function_name="{benchmark_function_name}", function_name="{orig_function_name}", file_path=r"{file_path}", class_name="{class_name}", num_to_get={max_run_count}):
0 commit comments