Skip to content
This repository was archived by the owner on Sep 6, 2023. It is now read-only.

Commit c64eb4f

Browse files
committed
extmod/vfs: Replace VLA in proxy func with small, static sized array.
VLAs can be expensive on stack usage due to stack alignment requirements, and also the fact that extra local variables are needed to track the dynamic size of the stack. So using fixed-size arrays when possible can help to reduce code size and stack usage. In this particular case, the maximum value of n_args in the VLA is 2 and so it's more efficient to just allocate this array with a fixed size. This reduces code size by around 30 bytes on Thumb2 and Xtensa archs. It also reduces total stack usage of the function: on Thumb2 the usage with VLA is between 40 and 48 bytes, which is reduced to 32; on Xtensa, VLA usage is between 64 and 80 bytes, reduced to 32; on x86-64 it's at least 88 bytes reduced to 80.
1 parent a33fca9 commit c64eb4f

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

extmod/vfs.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838
#include "extmod/vfs_fat.h"
3939
#endif
4040

41+
// For mp_vfs_proxy_call, the maximum number of additional args that can be passed.
42+
// A fixed maximum size is used to avoid the need for a costly variable array.
43+
#define PROXY_MAX_ARGS (2)
44+
4145
// path is the path to lookup and *path_out holds the path within the VFS
4246
// object (starts with / if an absolute path).
4347
// Returns MP_VFS_ROOT for root dir (and then path_out is undefined) and
@@ -97,6 +101,7 @@ STATIC mp_vfs_mount_t *lookup_path(mp_obj_t path_in, mp_obj_t *path_out) {
97101
}
98102

99103
STATIC mp_obj_t mp_vfs_proxy_call(mp_vfs_mount_t *vfs, qstr meth_name, size_t n_args, const mp_obj_t *args) {
104+
assert(n_args <= PROXY_MAX_ARGS);
100105
if (vfs == MP_VFS_NONE) {
101106
// mount point not found
102107
mp_raise_OSError(MP_ENODEV);
@@ -105,7 +110,7 @@ STATIC mp_obj_t mp_vfs_proxy_call(mp_vfs_mount_t *vfs, qstr meth_name, size_t n_
105110
// can't do operation on root dir
106111
mp_raise_OSError(MP_EPERM);
107112
}
108-
mp_obj_t meth[n_args + 2];
113+
mp_obj_t meth[2 + PROXY_MAX_ARGS];
109114
mp_load_method(vfs->obj, meth_name, meth);
110115
if (args != NULL) {
111116
memcpy(meth + 2, args, n_args * sizeof(*args));

0 commit comments

Comments
 (0)