Skip to content

Conversation

@gaogaotiantian
Copy link
Member

@gaogaotiantian gaogaotiantian commented Mar 18, 2025

This is one line of well-thought code :)

We basically solved the debugger overhead in a big loop by disabling LINE events, the one (major) thing left was recursive calls - or rather, calls in general. We won't prevent CALL events from happening for every unrelated call because that would require a much larger change, but we can disable CALL events after we hit it once.

Notice that CALL event has a two effects which made it a bit more complicated:

  1. It could be the event that the user actually wants (only happens in step)
  2. It returns the local trace function that enables all the other events on this code object (not function/frame).

No 2. makes it a bit challenging and much more rewarding.

We need to make sure we don't need any new events on this code object, before we can disable the CALL event.

First of all, if there's any breakpoint on the code object, we can't do that. But we already have check for that where the code was added.

Then we'll realize, if it's a next, until or return - it will just work fine, because they all rely on events on existing frame and code objects. It's impossible for those commands to stop at a new frame.

The only command that could stop at a new frame, is step - but if it's a step, it would either

  • hit stop_here() already on this specific CALL event so we can't reach to this point
  • or skip this function because of specified skip argument, which means it won't stop in this function in the future

So really, we don't need to care about anything. If we don't need to trace this function now, we won't need any new events for this code object in the future (until the user interaction), so we can simply disable the CALL event.

This basically gives us a zero overhead debugger. There's no observable overhead for the recursive fib implementation.

@gaogaotiantian gaogaotiantian merged commit e356468 into python:main Mar 21, 2025
43 checks passed
@gaogaotiantian gaogaotiantian deleted the disable-more-events branch March 21, 2025 00:32
seehwan pushed a commit to seehwan/cpython that referenced this pull request Apr 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants