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.
This commit is contained in:
parent
a33fca99a1
commit
c64eb4f8ce
|
@ -38,6 +38,10 @@
|
|||
#include "extmod/vfs_fat.h"
|
||||
#endif
|
||||
|
||||
// For mp_vfs_proxy_call, the maximum number of additional args that can be passed.
|
||||
// A fixed maximum size is used to avoid the need for a costly variable array.
|
||||
#define PROXY_MAX_ARGS (2)
|
||||
|
||||
// path is the path to lookup and *path_out holds the path within the VFS
|
||||
// object (starts with / if an absolute path).
|
||||
// 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) {
|
|||
}
|
||||
|
||||
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) {
|
||||
assert(n_args <= PROXY_MAX_ARGS);
|
||||
if (vfs == MP_VFS_NONE) {
|
||||
// mount point not found
|
||||
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_
|
|||
// can't do operation on root dir
|
||||
mp_raise_OSError(MP_EPERM);
|
||||
}
|
||||
mp_obj_t meth[n_args + 2];
|
||||
mp_obj_t meth[2 + PROXY_MAX_ARGS];
|
||||
mp_load_method(vfs->obj, meth_name, meth);
|
||||
if (args != NULL) {
|
||||
memcpy(meth + 2, args, n_args * sizeof(*args));
|
||||
|
|
Loading…
Reference in New Issue