Skip to content

Commit fda19ea

Browse files
Optimize discover_parameters_unittest
The optimized code achieves a 252% speedup through two key optimizations: **1. Early Exit for No Underscores**: Added a check `if '_' not in function_name:` that immediately returns `False` for strings without underscores. This avoids unnecessary splitting operations for simple function names, providing significant speedups (51-69% faster) for cases like single words or empty strings. **2. Right-Split Optimization**: Replaced `split("_")` with `rsplit("_", 1)` which only splits from the right once, creating exactly 2 parts instead of potentially hundreds. This dramatically reduces memory allocation and processing time, especially for long function names with many underscores. **Performance Impact by Test Type**: - **Simple cases** (no underscores): 51-69% faster due to early exit - **Valid numeric suffixes**: 21-50% faster from efficient rsplit - **Large-scale tests**: 660-3584% faster - the rsplit optimization shines here, avoiding expensive operations on strings with hundreds of parts - **Edge cases with non-numeric suffixes**: Slight 2-13% slowdown due to the additional underscore check, but this is minimal compared to the gains The optimizations preserve exact functionality while being most effective for complex function names with many parts, which are common in parameterized test scenarios.
1 parent ce72cfd commit fda19ea

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

codeflash/discovery/discover_unit_tests.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -523,9 +523,12 @@ def get_test_details(_test: unittest.TestCase) -> TestsInFile | None:
523523

524524

525525
def discover_parameters_unittest(function_name: str) -> tuple[bool, str, str | None]:
526-
function_parts = function_name.split("_")
527-
if len(function_parts) > 1 and function_parts[-1].isdigit():
528-
return True, "_".join(function_parts[:-1]), function_parts[-1]
526+
if "_" not in function_name:
527+
return False, function_name, None
528+
529+
function_parts = function_name.rsplit("_", 1)
530+
if len(function_parts) == 2 and function_parts[1].isdigit():
531+
return True, function_parts[0], function_parts[1]
529532

530533
return False, function_name, None
531534

0 commit comments

Comments
 (0)