diff options
| author | Jim Mussared <jim.mussared@gmail.com> | 2023-05-10 13:02:09 +1000 |
|---|---|---|
| committer | Damien George <damien@micropython.org> | 2023-06-01 16:21:21 +1000 |
| commit | 525557738cccb73d7b00d2048b9fd47e4caeeec6 (patch) | |
| tree | 7bdcfc552a57d1fffe6bd207759f36bd566e1f09 /py/objmodule.h | |
| parent | 42f3f66431bb3131f5ee78f3e121491e9d0fb91d (diff) | |
py/builtinimport: Optimise sub-package loading.
This makes it so that sub-packages are resolved relative to their parent's
`__path__`, rather than re-resolving each parent's filesystem path.
The previous behavior was that `import foo.bar` would first re-search
`sys.path` for `foo`, then use the resulting path to find `bar`.
For already-loaded and u-prefixed modules, because we no longer need to
build the path from level to level, we no longer unnecessarily search
the filesystem. This should improve startup time.
Explicitly makes the resolving process clear:
- Loaded modules are returned immediately without touching the filesystem.
- Exact-match of builtins are also returned immediately.
- Then the filesystem search happens.
- If that fails, then the weak-link handling is applied.
This maintains the existing behavior: if a user writes `import time` they
will get time.py if it exits, otherwise the built-in utime. Whereas `import
utime` will always return the built-in.
This also fixes a regression from a7fa18c203a241f670f12ab507aa8b349fcd45a1
where we search the filesystem for built-ins. It is now only possible to
override u-prefixed builtins. This will remove a lot of filesystem stats
at startup, as micropython-specific modules (e.g. `pyb`) will no longer
attempt to look at the filesystem.
Added several improvements to the comments and some minor renaming and
refactoring to make it clearer how the import mechanism works. Overall
code size diff is +56 bytes on STM32.
This work was funded through GitHub Sponsors.
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
Diffstat (limited to 'py/objmodule.h')
| -rw-r--r-- | py/objmodule.h | 3 |
1 files changed, 0 insertions, 3 deletions
diff --git a/py/objmodule.h b/py/objmodule.h index d11d5bcd7..11bd7bb42 100644 --- a/py/objmodule.h +++ b/py/objmodule.h @@ -33,10 +33,7 @@ extern const mp_map_t mp_builtin_module_map; -mp_obj_t mp_module_get_loaded_or_builtin(qstr module_name); -#if MICROPY_MODULE_WEAK_LINKS mp_obj_t mp_module_get_builtin(qstr module_name); -#endif void mp_module_generic_attr(qstr attr, mp_obj_t *dest, const uint16_t *keys, mp_obj_t *values); |
