typeguard icon indicating copy to clipboard operation
typeguard copied to clipboard

Using typecheck decorator slows module import dramatically

Open alonme opened this issue 1 year ago • 5 comments

Things to check first

  • [X] I have searched the existing issues and didn't find my feature already requested there

Feature description

As a result of researching why the import of the inflect library was slow I found out that the @typechecked decorator was the main reason.

See https://github.com/jaraco/inflect/issues/212

I believe that the same solution suggested in this issue can also be applied generally, the solution being to defer running the typechecked decorator until the decorated function is actually called, and to cache the result of the "decoration" as otherwise every call to the decorated function will be slow

Something like this:

@functools.cache
def cached_typechecked(func: Callable) -> Callable:
    return typechecked(func)


def lazy_typechecked(func: Callable) -> Callable:
    def wrapper(*args, **kwargs) -> Any:
        typechecked_func = cached_typechecked(func)
        return typechecked_func(*args, **kwargs)

    return wrapper

Use case

Faster imports for all direct and indirect users of typeguard

alonme avatar Jun 20 '24 19:06 alonme

I also have performance issues when importing code using typeguard. I switched back to typeguard==2.13.3 and it's much faster

hoorelbeke-jimmy avatar Jul 03 '24 08:07 hoorelbeke-jimmy

It slows down the first import of a module. Subsequent imports should be fine when loaded from bytecode cache. Let me know if this is not the case.

agronholm avatar Jul 05 '24 11:07 agronholm

Oh, sorry, indeed with @typechecked it would be slowed down – only the full import hook would benefit from the byte code cache.

agronholm avatar Jul 05 '24 11:07 agronholm

@agronholm this is the case, however this is an issue nontheless

I encountered this issue while using inflect and the import took more than a second (I mean, more than a second was only running typeguards decorators)

Edit: was written after your first comment

Edit 2: ok now I understand what you mean by first, So yea youre second comment gets it

alonme avatar Jul 05 '24 11:07 alonme

@agronholm Hi! I reached out to you via e-mail. Let me know if you're interested in me taking the full responsibility of resolving this.

johnslavik avatar Jul 11 '24 15:07 johnslavik