@@ -449,7 +449,37 @@ def generic_visit(self, node: ast.AST) -> None:
449449 if self .found_any_target_function :
450450 return
451451 # Direct base call improves run speed (avoids extra method resolution)
452- ast .NodeVisitor .generic_visit (self , node )
452+ self ._fast_generic_visit (node )
453+
454+ def _fast_generic_visit (self , node : ast .AST ) -> None :
455+ """Faster generic_visit: Inline traversal, avoiding method resolution overhead.
456+ Short-circuits (returns) if found_any_target_function is True.
457+ """
458+ # This logic is derived from ast.NodeVisitor.generic_visit, but with optimizations.
459+ found_flag = self .found_any_target_function
460+ # Micro-optimization: store fATF in local variable for quick repeated early exit
461+ if found_flag :
462+ return
463+ for field in node ._fields :
464+ value = getattr (node , field , None )
465+ if isinstance (value , list ):
466+ for item in value :
467+ if self .found_any_target_function :
468+ return
469+ if isinstance (item , ast .AST ):
470+ meth = getattr (self , "visit_" + item .__class__ .__name__ , None )
471+ if meth is not None :
472+ meth (item )
473+ else :
474+ self ._fast_generic_visit (item )
475+ elif isinstance (value , ast .AST ):
476+ if self .found_any_target_function :
477+ return
478+ meth = getattr (self , "visit_" + value .__class__ .__name__ , None )
479+ if meth is not None :
480+ meth (value )
481+ else :
482+ self ._fast_generic_visit (value )
453483
454484
455485def analyze_imports_in_test_file (test_file_path : Path | str , target_functions : set [str ]) -> bool :
0 commit comments