Skip to content

Commit

Permalink
py/builtinimport: Simplify calls to stat_path().
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
smurfix authored and dpgeorge committed Jan 30, 2024
1 parent f3d1495 commit d19371c
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions py/builtinimport.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,17 @@
// uses mp_vfs_import_stat) to also search frozen modules. Given an exact
// path to a file or directory (e.g. "foo/bar", foo/bar.py" or "foo/bar.mpy"),
// will return whether the path is a file, directory, or doesn't exist.
STATIC mp_import_stat_t stat_path(const char *path) {
STATIC mp_import_stat_t stat_path(vstr_t *path) {
const char *str = vstr_null_terminated_str(path);
#if MICROPY_MODULE_FROZEN
// Only try and load as a frozen module if it starts with .frozen/.
const int frozen_path_prefix_len = strlen(MP_FROZEN_PATH_PREFIX);
if (strncmp(path, MP_FROZEN_PATH_PREFIX, frozen_path_prefix_len) == 0) {
if (strncmp(str, MP_FROZEN_PATH_PREFIX, frozen_path_prefix_len) == 0) {
// Just stat (which is the return value), don't get the data.
return mp_find_frozen_module(path + frozen_path_prefix_len, NULL, NULL);
return mp_find_frozen_module(str + frozen_path_prefix_len, NULL, NULL);
}
#endif
return mp_import_stat(path);
return mp_import_stat(str);
}

// Stat a given filesystem path to a .py file. If the file does not exist,
Expand All @@ -75,7 +76,7 @@ STATIC mp_import_stat_t stat_path(const char *path) {
// files. This uses stat_path above, rather than mp_import_stat directly, so
// that the .frozen path prefix is handled.
STATIC mp_import_stat_t stat_file_py_or_mpy(vstr_t *path) {
mp_import_stat_t stat = stat_path(vstr_null_terminated_str(path));
mp_import_stat_t stat = stat_path(path);
if (stat == MP_IMPORT_STAT_FILE) {
return stat;
}
Expand All @@ -85,7 +86,7 @@ STATIC mp_import_stat_t stat_file_py_or_mpy(vstr_t *path) {
// Note: There's no point doing this if it's a frozen path, but adding the check
// would be extra code, and no harm letting mp_find_frozen_module fail instead.
vstr_ins_byte(path, path->len - 2, 'm');
stat = stat_path(vstr_null_terminated_str(path));
stat = stat_path(path);
if (stat == MP_IMPORT_STAT_FILE) {
return stat;
}
Expand All @@ -99,7 +100,7 @@ STATIC mp_import_stat_t stat_file_py_or_mpy(vstr_t *path) {
// result is a file, the path argument will be updated to include the file
// extension.
STATIC mp_import_stat_t stat_module(vstr_t *path) {
mp_import_stat_t stat = stat_path(vstr_null_terminated_str(path));
mp_import_stat_t stat = stat_path(path);
DEBUG_printf("stat %s: %d\n", vstr_str(path), stat);
if (stat == MP_IMPORT_STAT_DIR) {
return stat;
Expand Down

0 comments on commit d19371c

Please sign in to comment.