Skip to content

Commit 46016bd

Browse files
authored
Merge pull request #1866 from codeflash-ai/codeflash/optimize-pr1860-2026-03-18T10.30.36
⚡️ Speed up method `InitDecorator.visit_Module` by 12% in PR #1860 (`fix/attrs-init-instrumentation`)
2 parents 9fae394 + e66d7c1 commit 46016bd

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

codeflash/languages/python/instrument_codeflash_capture.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ def __init__(
132132
self._starred_args = ast.Starred(value=self._args_name_load, ctx=self._load_ctx)
133133
self._kwargs_keyword = ast.keyword(arg=None, value=self._kwargs_name_load)
134134

135+
# Pre-parse the import statement to avoid repeated parsing in visit_Module
136+
self._import_stmt = ast.parse("from codeflash.verification.codeflash_capture import codeflash_capture").body[0]
137+
135138
def visit_ImportFrom(self, node: ast.ImportFrom) -> ast.ImportFrom:
136139
# Check if our import already exists
137140
if node.module == "codeflash.verification.codeflash_capture" and any(
@@ -155,8 +158,7 @@ def visit_Module(self, node: ast.Module) -> ast.Module:
155158

156159
# Add import statement
157160
if not self.has_import and self.inserted_decorator:
158-
import_stmt = ast.parse("from codeflash.verification.codeflash_capture import codeflash_capture").body[0]
159-
node.body.insert(0, import_stmt)
161+
node.body.insert(0, self._import_stmt)
160162

161163
return node
162164

0 commit comments

Comments
 (0)