Skip to content

Commit b9ad0a5

Browse files
authored
Fixed issue where mallocs not attributed properly to lines (#879)
1 parent f28e6ac commit b9ad0a5

File tree

1 file changed

+9
-43
lines changed

1 file changed

+9
-43
lines changed

scalene/scalene_profiler.py

+9-43
Original file line numberDiff line numberDiff line change
@@ -376,48 +376,6 @@ def update_profiled() -> None:
376376
)
377377
Scalene.update_line()
378378

379-
@staticmethod
380-
def invalidate_lines_python(
381-
frame: FrameType, _event: str, _arg: str
382-
) -> Any:
383-
"""Mark the last_profiled information as invalid as soon as we execute a different line of code."""
384-
try:
385-
# If we are still on the same line, return.
386-
ff = frame.f_code.co_filename
387-
fl = frame.f_lineno
388-
(fname, lineno, lasti) = Scalene.last_profiled_tuple()
389-
if (ff == fname) and (fl == lineno):
390-
return Scalene.invalidate_lines_python
391-
# Different line: stop tracing this frame.
392-
frame.f_trace = None
393-
frame.f_trace_lines = False
394-
if on_stack(frame, fname, lineno):
395-
# We are still on the same line, but somewhere up the stack
396-
# (since we returned when it was the same line in this
397-
# frame). Stop tracing in this frame.
398-
return None
399-
# We are on a different line; stop tracing and increment the count.
400-
sys.settrace(None)
401-
Scalene.update_profiled()
402-
Scalene.__last_profiled_invalidated = True
403-
404-
Scalene.__last_profiled = [
405-
Filename("NADA"),
406-
LineNumber(0),
407-
ByteCodeIndex(0),
408-
# Filename(ff),
409-
# LineNumber(fl),
410-
# ByteCodeIndex(frame.f_lasti),
411-
]
412-
return None
413-
except AttributeError:
414-
# This can happen when Scalene shuts down.
415-
return None
416-
except Exception as e:
417-
print(f"{Scalene.__error_message}:\n", e, file=sys.stderr)
418-
traceback.print_exc()
419-
return None
420-
421379
@classmethod
422380
def clear_metrics(cls) -> None:
423381
"""Clear the various states for forked processes."""
@@ -575,7 +533,15 @@ def malloc_signal_handler(
575533
):
576534
Scalene.update_profiled()
577535
pywhere.set_last_profiled_invalidated_false()
578-
Scalene.__last_profiled = [
536+
# In the setprofile callback, we rely on
537+
# __last_profiled always having the same memory address.
538+
# This is an optimization to not have to traverse the Scalene profiler
539+
# object's dictionary every time we want to update the last profiled line.
540+
#
541+
# A previous change to this code set Scalene.__last_profiled = [fname, lineno, lasti],
542+
# which created a new list object and set the __last_profiled attribute to the new list. This
543+
# made the object held in `pywhere.cpp` out of date, and caused the profiler to not update the last profiled line.
544+
Scalene.__last_profiled[:] = [
579545
Filename(f.f_code.co_filename),
580546
LineNumber(f.f_lineno),
581547
ByteCodeIndex(f.f_lasti),

0 commit comments

Comments
 (0)