coady / multimethod

Multiple argument dispatching.
https://coady.github.io/multimethod
Other
277 stars 24 forks source link

Make __call__ faster to step through in the debugger #81

Closed mnieber closed 1 year ago

mnieber commented 1 year ago

When you debug into a multimethod, you will first step into the __call__ function and then into the target function. This makes debugging a bit more cumbersome. Unfortunately, pudb (or pdb, I guess) does not allow you to ignore the multimethod code (that would be the best solution). However, the call code could be changed to make it quicker to pass through it in the debugger:

    def __call__(self, *args, **kwargs):
        """Resolve and dispatch to best method."""
        func = self.resolve_func(*args, **kwargs)
        return func(*args, **kwargs)

    def resolve_func(self, *args, **kwargs):
        """Return the best method."""
        try:
            if self.pending:  # check first to avoid function call
                self.evaluate()
            return self[tuple(func(arg) for func, arg in zip(self.type_checkers, args))]
        except TypeError as ex:
            raise DispatchError(f"Function {func.__code__}") from ex
coady commented 1 year ago

The exception handling needs to go around the function call, not the dispatch. But additionally another method call would add overhead in the critical path.

I'm not sure about the goal here: just to step over 1 line instead of 3?

mnieber commented 1 year ago

I'm not sure about the goal here: just to step over 1 line instead of 3?

Yes, that was the idea. But I agree that avoiding another method call is more important (so I will close this).