Skip to content

Commit dfe232d

Browse files
committed
py/builtinimport: Remove weak links.
In order to keep "import umodule" working, the existing mechanism is replaced with a simple fallback to drop the "u". This makes importing of built-ins no longer touch the filesystem, which makes a typical built-in import take ~0.15ms rather than 3-5ms. (Weak links were added in c14a816) This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <[email protected]>
1 parent 30628d1 commit dfe232d

File tree

9 files changed

+5
-59
lines changed

9 files changed

+5
-59
lines changed

docs/develop/porting.rst

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,6 @@ The following is an example of an ``mpconfigport.h`` file:
151151
#define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_TERSE)
152152
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT)
153153
154-
// Enable u-modules to be imported with their standard name, like sys.
155-
#define MICROPY_MODULE_WEAK_LINKS (1)
156-
157154
// Fine control over Python builtins, classes, modules, etc.
158155
#define MICROPY_PY_ASYNC_AWAIT (0)
159156
#define MICROPY_PY_BUILTINS_SET (0)

ports/cc3200/mpconfigport.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@
7171
#define MICROPY_FATFS_SYNC_T SemaphoreHandle_t
7272

7373
#define MICROPY_STREAMS_NON_BLOCK (1)
74-
#define MICROPY_MODULE_WEAK_LINKS (1)
7574
#define MICROPY_CAN_OVERRIDE_BUILTINS (1)
7675
#define MICROPY_USE_INTERNAL_ERRNO (1)
7776
#define MICROPY_VFS (1)

ports/nrf/mpconfigport.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@
137137
#define MICROPY_PY_UOS (0)
138138

139139
#define MICROPY_STREAMS_NON_BLOCK (1)
140-
#define MICROPY_MODULE_WEAK_LINKS (1)
141140
#define MICROPY_CAN_OVERRIDE_BUILTINS (1)
142141
#define MICROPY_USE_INTERNAL_ERRNO (1)
143142
#if MICROPY_HW_USB_CDC_1200BPS_TOUCH

ports/samd/mpconfigport.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
#define MICROPY_PY_BUILTINS_HELP_MODULES (1)
6060
#define MICROPY_ENABLE_SCHEDULER (1)
6161
#define MICROPY_SCHEDULER_STATIC_NODES (1)
62-
#define MICROPY_MODULE_WEAK_LINKS (1)
6362
#define MICROPY_HW_ENABLE_USBDEV (1)
6463
#define MICROPY_HW_USB_CDC_1200BPS_TOUCH (1)
6564

ports/windows/mpconfigport.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@
7272
#endif
7373
#define MICROPY_STREAMS_POSIX_API (1)
7474
#define MICROPY_OPT_COMPUTED_GOTO (0)
75-
#define MICROPY_MODULE_WEAK_LINKS (1)
7675
#define MICROPY_MODULE_OVERRIDE_MAIN_IMPORT (1)
7776
#define MICROPY_CAN_OVERRIDE_BUILTINS (1)
7877
#ifndef MICROPY_ENABLE_SCHEDULER

ports/zephyr/mpconfigport.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969
#define MICROPY_PY_MACHINE_SPI_MSB (SPI_TRANSFER_MSB)
7070
#define MICROPY_PY_MACHINE_SPI_LSB (SPI_TRANSFER_LSB)
7171
#define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new
72-
#define MICROPY_MODULE_WEAK_LINKS (1)
7372
#define MICROPY_PY_STRUCT (0)
7473
#ifdef CONFIG_NETWORKING
7574
// If we have networking, we likely want errno comfort

py/builtinimport.c

Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,6 @@
4545
#define DEBUG_printf(...) (void)0
4646
#endif
4747

48-
#if MICROPY_MODULE_WEAK_LINKS
49-
STATIC qstr make_weak_link_name(vstr_t *buffer, qstr name) {
50-
vstr_reset(buffer);
51-
vstr_add_char(buffer, 'u');
52-
vstr_add_str(buffer, qstr_str(name));
53-
return qstr_from_strn(buffer->buf, buffer->len);
54-
}
55-
#endif
56-
5748
#if MICROPY_ENABLE_EXTERNAL_IMPORT
5849

5950
// Must be a string of one byte.
@@ -391,13 +382,8 @@ STATIC mp_obj_t process_import_at_level(qstr full_mod_name, qstr level_mod_name,
391382
if (outer_module_obj == MP_OBJ_NULL) {
392383
DEBUG_printf("Searching for top-level module\n");
393384

394-
// An exact match of a built-in will always bypass the filesystem.
395-
// Note that CPython-compatible built-ins are named e.g. utime, so this
396-
// means that an exact match is only for `import utime`, so `import
397-
// time` will search the filesystem and failing that hit the weak
398-
// link handling below. Whereas micropython-specific built-ins like
399-
// `micropython`, `pyb`, `network`, etc will match exactly and cannot
400-
// be overridden by the filesystem.
385+
// An import of a non-extensible built-in will always bypass the
386+
// filesystem. e.g. `import micropython` or `import pyb`.
401387
module_obj = mp_module_get_builtin(level_mod_name);
402388
if (module_obj != MP_OBJ_NULL) {
403389
return module_obj;
@@ -417,20 +403,9 @@ STATIC mp_obj_t process_import_at_level(qstr full_mod_name, qstr level_mod_name,
417403
// relative to all the locations in sys.path.
418404
stat = stat_top_level(level_mod_name, &path);
419405

420-
#if MICROPY_MODULE_WEAK_LINKS
421-
if (stat == MP_IMPORT_STAT_NO_EXIST) {
422-
// No match on the filesystem. (And not a built-in either).
423-
// If "foo" was requested, then try "ufoo" as a built-in. This
424-
// allows `import time` to use built-in `utime`, unless `time`
425-
// exists on the filesystem. This feature was formerly known
426-
// as "weak links".
427-
qstr umodule_name = make_weak_link_name(&path, level_mod_name);
428-
module_obj = mp_module_get_builtin(umodule_name);
429-
if (module_obj != MP_OBJ_NULL) {
430-
return module_obj;
431-
}
432-
}
433-
#endif
406+
// TODO: If stat failed, now try extensible built-in modules.
407+
408+
// TODO: If importing `ufoo`, try `foo`.
434409
} else {
435410
DEBUG_printf("Searching for sub-module\n");
436411

@@ -667,21 +642,6 @@ mp_obj_t mp_builtin___import___default(size_t n_args, const mp_obj_t *args) {
667642
return module_obj;
668643
}
669644

670-
#if MICROPY_MODULE_WEAK_LINKS
671-
// Check if the u-prefixed name is a built-in.
672-
VSTR_FIXED(umodule_path, MICROPY_ALLOC_PATH_MAX);
673-
qstr umodule_name_qstr = make_weak_link_name(&umodule_path, module_name_qstr);
674-
module_obj = mp_module_get_builtin(umodule_name_qstr);
675-
if (module_obj != MP_OBJ_NULL) {
676-
return module_obj;
677-
}
678-
#elif MICROPY_PY_SYS
679-
// Special handling to make `import sys` work even if weak links aren't enabled.
680-
if (module_name_qstr == MP_QSTR_sys) {
681-
return MP_OBJ_FROM_PTR(&mp_module_sys);
682-
}
683-
#endif
684-
685645
// Couldn't find the module, so fail
686646
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
687647
mp_raise_msg(&mp_type_ImportError, MP_ERROR_TEXT("module not found"));

py/mpconfig.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -871,11 +871,6 @@ typedef double mp_float_t;
871871
#define MICROPY_MODULE_GETATTR (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_CORE_FEATURES)
872872
#endif
873873

874-
// Whether module weak links are supported
875-
#ifndef MICROPY_MODULE_WEAK_LINKS
876-
#define MICROPY_MODULE_WEAK_LINKS (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_EXTRA_FEATURES)
877-
#endif
878-
879874
// Whether to enable importing foo.py with __name__ set to '__main__'
880875
// Used by the unix port for the -m flag.
881876
#ifndef MICROPY_MODULE_OVERRIDE_MAIN_IMPORT

py/objmodule.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ STATIC const mp_rom_map_elem_t mp_builtin_module_table[] = {
169169
// builtin modules declared with MP_REGISTER_MODULE()
170170
MICROPY_REGISTERED_MODULES
171171
};
172-
173172
MP_DEFINE_CONST_MAP(mp_builtin_module_map, mp_builtin_module_table);
174173

175174
// Attempts to find (and initialise) a builtin, otherwise returns

0 commit comments

Comments
 (0)