33import sys
44import time
55import os
6- import json
6+ from micropython import const
77
88Any = object
99from ..common .constants import (
10+ STEP_INTO ,
11+ STEP_OUT ,
12+ STEP_OVER ,
1013 TRACE_CALL ,
1114 TRACE_LINE ,
1215 TRACE_RETURN ,
1518 SCOPE_GLOBALS ,
1619)
1720
18- VARREF_LOCALS = 1
19- VARREF_GLOBALS = 2
20- VARREF_LOCALS_SPECIAL = 3
21- VARREF_GLOBALS_SPECIAL = 4
21+ VARREF_LOCALS = const ( 1 )
22+ VARREF_GLOBALS = const ( 2 )
23+ VARREF_LOCALS_SPECIAL = const ( 3 )
24+ VARREF_GLOBALS_SPECIAL = const ( 4 )
2225
2326# New constants for complex variable references
24- VARREF_COMPLEX_BASE = 10000 # Base for complex variable references
25- MAX_CACHE_SIZE = 50 # Limit cache size for memory constraints
27+ VARREF_COMPLEX_BASE = const ( 10000 ) # Base for complex variable references
28+ MAX_CACHE_SIZE = const ( 50 ) # Limit cache size for memory constraints
2629
2730
2831class VariableReferenceCache :
@@ -54,17 +57,12 @@ def _cleanup_oldest(self) -> None:
5457 """Remove oldest entries to free memory - optimized for MicroPython."""
5558 if not self .cache or not self .insertion_order :
5659 return
57-
58- # More aggressive cleanup for memory-constrained environments
59- to_remove = max (1 , len (self .cache ) // 3 ) # Remove 1/3 instead of 1/4
60-
60+ to_remove = max (1 , len (self .cache ) // 3 )
6161 # Direct list slicing is more memory efficient than iteration
6262 keys_to_remove = self .insertion_order [:to_remove ]
63-
6463 # Batch delete for efficiency
6564 for key in keys_to_remove :
6665 self .cache .pop (key , None ) # Use pop with default to avoid KeyError
67-
6866 # Update insertion order in one operation
6967 self .insertion_order = self .insertion_order [to_remove :]
7068
@@ -188,16 +186,8 @@ def set_breakpoints(self, filename: str, breakpoints: list[dict]):
188186 if local_name != filename :
189187 self .breakpoints [local_name ] = {}
190188 self ._debug_print (f"[>>>] Setting breakpoints for local: { local_name } :{ line } " )
191- self .breakpoints [local_name ][line ] = {
192- "line" : line ,
193- "verified" : True ,
194- "source" : {"path" : filename },
195- }
196- self .breakpoints [filename ][line ] = {
197- "line" : line ,
198- "verified" : True ,
199- "source" : {"path" : filename },
200- }
189+ self .breakpoints [local_name ][line ] = {}
190+ self .breakpoints [filename ][line ] = {}
201191 actual_breakpoints .append (
202192 {"line" : line , "verified" : True , "source" : {"path" : filename }}
203193 )
@@ -208,20 +198,19 @@ def set_breakpoints(self, filename: str, breakpoints: list[dict]):
208198
209199 def should_stop (self , frame , event : str , arg ):
210200 """Determine if execution should stop at this point."""
201+ # HOT path - no debug printing here
211202 self .current_frame = frame
212203 self .hit_breakpoint = False
213204
214- # Get frame information
215- filename = frame .f_code .co_filename
205+ # Cache frame attributes to reduce lookup overhead
206+ frame_code = frame .f_code
207+ filename = frame_code .co_filename
216208 lineno = frame .f_lineno
209+
217210 # Check for exact filename match first
218211 if filename in self .breakpoints and lineno in self .breakpoints [filename ]:
219- self ._debug_print (f"[PDB] HIT BREAKPOINT (exact match) at { filename } :{ lineno } " )
220- # Record the path mapping (in this case, they're already the same)
221- # self.file_mappings[filename] = self._filename_as_debugger(filename)
222212 self .hit_breakpoint = True
223213 return True
224- # path/file.py matched - but not the line number - keep running
225214 else :
226215 # file not (yet) matched - this is slow so we do not want to do this often.
227216 # TODO: use builins - sys.path method to find the file
@@ -230,17 +219,17 @@ def should_stop(self, frame, event: str, arg):
230219 self .breakpoints [filename ] = {} # Ensure the filename is in the breakpoints dict
231220 if not filename in self .file_mappings :
232221 self .file_mappings [filename ] = self ._filename_as_debugger (filename )
233- self ._debug_print (
234- f"[PDB] add mapping for :'{ filename } ' -> '{ self .file_mappings [filename ]} '"
235- )
222+ # self._debug_print(
223+ # f"[PDB] add mapping for :'{filename}' -> '{self.file_mappings[filename]}'"
224+ # )
236225
237226 # Check stepping
238- if self .step_mode == "into" :
227+ if self .step_mode == STEP_INTO :
239228 if event in (TRACE_CALL , TRACE_LINE ):
240229 self .step_mode = None
241230 return True
242231
243- elif self .step_mode == "over" :
232+ elif self .step_mode == STEP_OVER :
244233 if event == TRACE_LINE and frame == self .step_frame :
245234 self .step_mode = None
246235 return True
@@ -251,7 +240,7 @@ def should_stop(self, frame, event: str, arg):
251240 else :
252241 self .step_mode = None
253242
254- elif self .step_mode == "out" :
243+ elif self .step_mode == STEP_OUT :
255244 if event == TRACE_RETURN and frame == self .step_frame :
256245 self .step_mode = None
257246 return True
0 commit comments