Skip to content

Commit 4f8ff2d

Browse files
Optimize _extract_class_body_context
The optimized code achieves an **11% runtime improvement** (93.6μs → 83.6μs) through two key changes: **1. Caching `child.type` in a local variable** ```python child_type = child.type # Cache the attribute access if child_type in ("{", "}", ";", ","): ``` In the loop over `body_node.children`, `child.type` was accessed 3-4 times per iteration. By storing it once in `child_type`, we eliminate repeated attribute lookups on the Node object, which are more expensive than local variable access in Python. **2. Replacing `append("".join(...))` with `extend(...)`** Original: ```python field_lines = lines[javadoc_start : end_line + 1] field_parts.append("".join(field_lines)) # Join then append ``` Optimized: ```python field_parts.extend(lines[javadoc_start : end_line + 1]) # Directly extend ``` This eliminates intermediate string concatenations inside the loop. Instead of creating a joined string for each field/constructor and appending it to the list, we extend the list with the raw line slices. The final `"".join(field_parts)` at the end performs one single join operation over all accumulated lines, which is significantly more efficient than multiple joins. **Performance impact by test case:** - **Large-scale test** (200 fields): 16.5% faster (71.6μs → 61.5μs) - the extend optimization scales particularly well with many fields - **Multiple mixed fields/constructors**: 4.86% faster - benefits from both optimizations - **Basic single field tests**: slight variation (some 0.5-5% slower) - the overhead of the extra local variable assignment is negligible for single-element cases but the optimization still maintains correctness The optimization is most effective when processing Java files with many field declarations or constructors, which is common in real-world codebases. The deferred string joining pattern is a classic Python performance technique that reduces memory allocations and intermediate object creation.
1 parent 41b08a9 commit 4f8ff2d

1 file changed

Lines changed: 11 additions & 11 deletions

File tree

codeflash/languages/java/context.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -413,18 +413,20 @@ def _extract_type_body_context(
413413
enum_constant_parts: list[str] = []
414414

415415
for child in body_node.children:
416+
child_type = child.type
417+
416418
# Skip braces, semicolons, and commas
417-
if child.type in ("{", "}", ";", ","):
419+
if child_type in ("{", "}", ";", ","):
418420
continue
419421

420422
# Handle enum constants (only for enums)
421423
# Extract just the constant name/text, not the whole line
422-
if child.type == "enum_constant" and type_kind == "enum":
424+
if child_type == "enum_constant" and type_kind == "enum":
423425
constant_text = source_bytes[child.start_byte : child.end_byte].decode("utf8")
424426
enum_constant_parts.append(constant_text)
425427

426428
# Handle field declarations
427-
elif child.type == "field_declaration":
429+
elif child_type == "field_declaration":
428430
start_line = child.start_point[0]
429431
end_line = child.end_point[0]
430432

@@ -436,18 +438,16 @@ def _extract_type_body_context(
436438
if comment_text.strip().startswith("/**"):
437439
javadoc_start = prev_sibling.start_point[0]
438440

439-
field_lines = lines[javadoc_start : end_line + 1]
440-
field_parts.append("".join(field_lines))
441+
field_parts.extend(lines[javadoc_start : end_line + 1])
441442

442443
# Handle constant declarations (for interfaces)
443-
elif child.type == "constant_declaration" and type_kind == "interface":
444+
elif child_type == "constant_declaration" and type_kind == "interface":
444445
start_line = child.start_point[0]
445446
end_line = child.end_point[0]
446-
constant_lines = lines[start_line : end_line + 1]
447-
field_parts.append("".join(constant_lines))
447+
field_parts.extend(lines[start_line : end_line + 1])
448448

449449
# Handle constructor declarations
450-
elif child.type == "constructor_declaration":
450+
elif child_type == "constructor_declaration":
451451
start_line = child.start_point[0]
452452
end_line = child.end_point[0]
453453

@@ -459,8 +459,8 @@ def _extract_type_body_context(
459459
if comment_text.strip().startswith("/**"):
460460
javadoc_start = prev_sibling.start_point[0]
461461

462-
constructor_lines = lines[javadoc_start : end_line + 1]
463-
constructor_parts.append("".join(constructor_lines))
462+
constructor_parts.extend(lines[javadoc_start : end_line + 1])
463+
464464

465465
fields_code = "".join(field_parts)
466466
constructors_code = "".join(constructor_parts)

0 commit comments

Comments
 (0)