Skip to content

Commit d19371c

Browse files
smurfixdpgeorge
authored andcommitted
py/builtinimport: Simplify calls to stat_path().
stat_path is only called with stringified vstr_t objects. Thus, pulling the stringification into the function replaces three function calls with one, saving a few bytes. Signed-off-by: Matthias Urlichs <[email protected]>
1 parent f3d1495 commit d19371c

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

py/builtinimport.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,17 @@
5757
// uses mp_vfs_import_stat) to also search frozen modules. Given an exact
5858
// path to a file or directory (e.g. "foo/bar", foo/bar.py" or "foo/bar.mpy"),
5959
// will return whether the path is a file, directory, or doesn't exist.
60-
STATIC mp_import_stat_t stat_path(const char *path) {
60+
STATIC mp_import_stat_t stat_path(vstr_t *path) {
61+
const char *str = vstr_null_terminated_str(path);
6162
#if MICROPY_MODULE_FROZEN
6263
// Only try and load as a frozen module if it starts with .frozen/.
6364
const int frozen_path_prefix_len = strlen(MP_FROZEN_PATH_PREFIX);
64-
if (strncmp(path, MP_FROZEN_PATH_PREFIX, frozen_path_prefix_len) == 0) {
65+
if (strncmp(str, MP_FROZEN_PATH_PREFIX, frozen_path_prefix_len) == 0) {
6566
// Just stat (which is the return value), don't get the data.
66-
return mp_find_frozen_module(path + frozen_path_prefix_len, NULL, NULL);
67+
return mp_find_frozen_module(str + frozen_path_prefix_len, NULL, NULL);
6768
}
6869
#endif
69-
return mp_import_stat(path);
70+
return mp_import_stat(str);
7071
}
7172

7273
// Stat a given filesystem path to a .py file. If the file does not exist,
@@ -75,7 +76,7 @@ STATIC mp_import_stat_t stat_path(const char *path) {
7576
// files. This uses stat_path above, rather than mp_import_stat directly, so
7677
// that the .frozen path prefix is handled.
7778
STATIC mp_import_stat_t stat_file_py_or_mpy(vstr_t *path) {
78-
mp_import_stat_t stat = stat_path(vstr_null_terminated_str(path));
79+
mp_import_stat_t stat = stat_path(path);
7980
if (stat == MP_IMPORT_STAT_FILE) {
8081
return stat;
8182
}
@@ -85,7 +86,7 @@ STATIC mp_import_stat_t stat_file_py_or_mpy(vstr_t *path) {
8586
// Note: There's no point doing this if it's a frozen path, but adding the check
8687
// would be extra code, and no harm letting mp_find_frozen_module fail instead.
8788
vstr_ins_byte(path, path->len - 2, 'm');
88-
stat = stat_path(vstr_null_terminated_str(path));
89+
stat = stat_path(path);
8990
if (stat == MP_IMPORT_STAT_FILE) {
9091
return stat;
9192
}
@@ -99,7 +100,7 @@ STATIC mp_import_stat_t stat_file_py_or_mpy(vstr_t *path) {
99100
// result is a file, the path argument will be updated to include the file
100101
// extension.
101102
STATIC mp_import_stat_t stat_module(vstr_t *path) {
102-
mp_import_stat_t stat = stat_path(vstr_null_terminated_str(path));
103+
mp_import_stat_t stat = stat_path(path);
103104
DEBUG_printf("stat %s: %d\n", vstr_str(path), stat);
104105
if (stat == MP_IMPORT_STAT_DIR) {
105106
return stat;

0 commit comments

Comments
 (0)