micropython/ports
Jim Mussared 525557738c 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 a7fa18c203
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>
2023-06-01 16:21:21 +10:00
..
bare-arm
cc3200 all: Fix cases of Python variable assigned but never used. 2023-05-02 16:36:05 +10:00
embed
esp32
esp8266 ports: Enable os.sync() for esp32, esp8266, rp2, mimxrt, samd51. 2023-06-01 11:39:01 +10:00
mimxrt
minimal
nrf
pic16bit
powerpc
qemu-arm
renesas-ra
rp2
samd
stm32 ports: Remove os.sync() implementation from stm32 and renesas-ra. 2023-06-01 11:39:01 +10:00
teensy
unix py/builtinimport: Optimise sub-package loading. 2023-06-01 16:21:21 +10:00
webassembly
windows
zephyr zephyr/modutime: Use extmod version of time module. 2023-04-27 15:11:52 +10:00