Skip to content

Speed up "from X import A; from X import B; from X import C; ..." #98

@gvanrossum

Description

@gvanrossum

Over in Pyston-land someone remarked that this:

from X import A, B, C, ..., Z

is much faster than

from X import A
from X import B
from X import C
.
.
.
from X import Z

We can't easily rewrite the latter as the former, because in edge cases the semantics are different (though maybe Black could take the liberty to do it anyway...?). But we could try to make the latter execute faster -- in theory everything needed is just in sys.modules['X'], though in practice there appear to be some caches that are reset between from X import statements but that are kept around between A, B, ...., Z. Maybe there's some wiggle room or a quicker way to check for cache invalidation (which is probably rare).

Note that X could well be a package path like foo.bar.baz. Also, if X is a package, then A, B, ..., Z could be submodules; but if X is a module, A...Z must be variables (or functions, classes, etc.) defined in X.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions