Enable squeezing property storage in flash

.. and enable it on atmel-samd and raspberrypi. On trinket_m0 this saves
96 net bytes of flash. There are 216 bytes actually saved by reducing
the flash storage size of the property descriptors, but added code in
several paths takes back over half of the 'raw savings'.

By organizing the "get-only" and "get-set" (but no delete) properties
each in a different section, we can represent then more efficiently.

Testing performed: that a get-only property can still be gotten but
can't be set or deleted; that a get-set property can sill be gotten or
set but can't be deleted.  Tested on pygamer.

Because this requires linker file support, I only enabled it on two of
the ports.
This commit is contained in:
Jeff Epler 2022-05-03 08:31:41 -05:00
parent 78cf0a90af
commit 510890b207
No known key found for this signature in database
GPG Key ID: D5BF15AB975AB4DE
11 changed files with 75 additions and 10 deletions

View File

@ -28,6 +28,13 @@ SECTIONS
_sfixed = .;
KEEP(*(.vectors)) /* isr vector table */
__property_getter_start = .;
*(.property_getter)
__property_getter_end = .;
__property_getset_start = .;
*(.property_getset)
__property_getset_end = .;
/* Sort text sections so that they have fewer *fill* bytes needed. */
*(SORT_BY_ALIGNMENT(SORT_BY_NAME(.text))) /* .text sections (code) */
*(SORT_BY_ALIGNMENT(SORT_BY_NAME(.text*))) /* .text* sections (code) */

View File

@ -8,6 +8,7 @@ INTERNAL_LIBM = 1
USB_NUM_ENDPOINT_PAIRS = 8
CIRCUITPY_ROTARYIO_SOFTENCODER = 1
CIRCUITPY_OPTIMIZE_PROPERTY_FLASH_SIZE ?= 1
######################################################################
# Put samd21-only choices here.

View File

@ -68,6 +68,14 @@ SECTIONS
/* bit of a hack right now to exclude all floating point and time critical (e.g. memset, memcpy) code from
* FLASH ... we will include any thing excluded here in .data below by default */
*(.init)
__property_getter_start = .;
*(.property_getter)
__property_getter_end = .;
__property_getset_start = .;
*(.property_getset)
__property_getset_end = .;
*(EXCLUDE_FILE(*libgcc.a: *libc.a:*lib_a-mem*.o *libm.a:) .text*)
*(.fini)
/* Pull all c'tors into .text */

View File

@ -1,6 +1,7 @@
# All raspberrypi ports have longints.
LONGINT_IMPL = MPZ
CIRCUITPY_OPTIMIZE_PROPERTY_FLASH_SIZE ?= 1
CIRCUITPY_ALARM ?= 1
CIRCUITPY_RP2PIO ?= 1

View File

@ -573,4 +573,6 @@ void supervisor_run_background_tasks_if_tick(void);
#define MICROPY_WRAP_MP_EXECUTE_BYTECODE PLACE_IN_ITCM
#endif
#define MICROPY_PY_OPTIMIZE_PROPERTY_FLASH_SIZE (CIRCUITPY_OPTIMIZE_PROPERTY_FLASH_SIZE)
#endif // __INCLUDED_MPCONFIG_CIRCUITPY_H

View File

@ -32,6 +32,11 @@
CIRCUITPY_FULL_BUILD ?= 1
CFLAGS += -DCIRCUITPY_FULL_BUILD=$(CIRCUITPY_FULL_BUILD)
# Reduce the size of in-flash properties. Requires support in the .ld linker
# file, so not enabled by default.
CIRCUITPY_OPTIMIZE_PROPERTY_FLASH_SIZE ?= 0
CFLAGS += -DCIRCUITPY_OPTIMIZE_PROPERTY_FLASH_SIZE=$(CIRCUITPY_OPTIMIZE_PROPERTY_FLASH_SIZE)
# async/await language keyword support
MICROPY_PY_ASYNC_AWAIT ?= $(CIRCUITPY_FULL_BUILD)
CFLAGS += -DMICROPY_PY_ASYNC_AWAIT=$(MICROPY_PY_ASYNC_AWAIT)

View File

@ -1078,6 +1078,11 @@ typedef double mp_float_t;
#define MICROPY_PY_BUILTINS_PROPERTY (MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_CORE_FEATURES)
#endif
// Whether to optimize property flash storage size (requires linker script support)
#ifndef MICROPY_PY_BUILTINS_PROPERTY
#define MICROPY_PY_BUILTINS_PROPERTY (0)
#endif
// Whether to implement the start/stop/step attributes (readback) on
// the "range" builtin type. Rarely used, and costs ~60 bytes (x86).
#ifndef MICROPY_PY_BUILTINS_RANGE_ATTRS

View File

@ -95,9 +95,24 @@ const mp_obj_type_t mp_type_property = {
.locals_dict = (mp_obj_dict_t *)&property_locals_dict,
};
const mp_obj_t *mp_obj_property_get(mp_obj_t self_in) {
#if MICROPY_PY_OPTIMIZE_PROPERTY_FLASH_SIZE
extern const mp_obj_property_t __property_getter_start, __property_getter_end, __property_getset_start, __property_getset_end;
#endif
const mp_obj_t *mp_obj_property_get(mp_obj_t self_in, size_t *n_proxy) {
mp_check_self(mp_obj_is_type(self_in, &mp_type_property));
mp_obj_property_t *self = MP_OBJ_TO_PTR(self_in);
#if MICROPY_PY_OPTIMIZE_PROPERTY_FLASH_SIZE
if (self >= &__property_getter_start && self < &__property_getter_end) {
*n_proxy = 1;
} else if (self >= &__property_getset_start && self < &__property_getset_end) {
*n_proxy = 2;
} else {
*n_proxy = 3;
}
#else
*n_proxy = 3;
#endif
return self->proxy;
}

View File

@ -35,11 +35,27 @@ typedef struct _mp_obj_property_t {
mp_obj_t proxy[3]; // getter, setter, deleter
} mp_obj_property_t;
#if MICROPY_PY_OPTIMIZE_PROPERTY_FLASH_SIZE
typedef struct _mp_obj_property_getter_t {
mp_obj_base_t base;
mp_obj_t proxy[1]; // getter
} mp_obj_property_getter_t;
typedef struct _mp_obj_property_getset_t {
mp_obj_base_t base;
mp_obj_t proxy[2]; // getter, setter
} mp_obj_property_getset_t;
#define MP_PROPERTY_GETTER(P, G) const mp_obj_property_getter_t P __attribute((section(".property_getter"))) = {.base.type = &mp_type_property, .proxy = {G}}
#define MP_PROPERTY_GETSET(P, G, S) const mp_obj_property_getset_t P __attribute((section(".property_getset"))) = {.base.type = &mp_type_property, .proxy = {G, S}}
#else
typedef struct _mp_obj_property_t mp_obj_property_getter_t;
typedef struct _mp_obj_property_t mp_obj_property_getset_t;
#define MP_PROPERTY_GETTER(P, G) const mp_obj_property_t P = {.base.type = &mp_type_property, .proxy = {G, MP_ROM_NONE, MP_ROM_NONE}}
#define MP_PROPERTY_GETSET(P, G, S) const mp_obj_property_t P = {.base.type = &mp_type_property, .proxy = {G, S, MP_ROM_NONE}}
#endif
#endif // MICROPY_PY_BUILTINS_PROPERTY

View File

@ -660,7 +660,8 @@ STATIC void mp_obj_instance_load_attr(mp_obj_t self_in, qstr attr, mp_obj_t *des
// be called by the descriptor code down below. But that way
// requires overhead for the nested mp_call's and overhead for
// the code.
const mp_obj_t *proxy = mp_obj_property_get(member);
size_t n_proxy;
const mp_obj_t *proxy = mp_obj_property_get(member, &n_proxy);
if (proxy[0] == mp_const_none) {
mp_raise_AttributeError(MP_ERROR_TEXT("unreadable attribute"));
} else {
@ -740,11 +741,12 @@ STATIC bool mp_obj_instance_store_attr(mp_obj_t self_in, qstr attr, mp_obj_t val
// would be called by the descriptor code down below. But that way
// requires overhead for the nested mp_call's and overhead for
// the code.
const mp_obj_t *proxy = mp_obj_property_get(member[0]);
size_t n_proxy;
const mp_obj_t *proxy = mp_obj_property_get(member[0], &n_proxy);
mp_obj_t dest[2] = {self_in, value};
if (value == MP_OBJ_NULL) {
// delete attribute
if (proxy[2] == mp_const_none) {
if (n_proxy < 3 || proxy[2] == mp_const_none) {
// TODO better error message?
return false;
} else {
@ -753,7 +755,7 @@ STATIC bool mp_obj_instance_store_attr(mp_obj_t self_in, qstr attr, mp_obj_t val
}
} else {
// store attribute
if (proxy[1] == mp_const_none) {
if (n_proxy < 2 || proxy[1] == mp_const_none) {
// TODO better error message?
return false;
} else {
@ -1374,7 +1376,8 @@ STATIC void super_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
// here...
#if MICROPY_PY_BUILTINS_PROPERTY
if (mp_obj_is_type(member, &mp_type_property)) {
const mp_obj_t *proxy = mp_obj_property_get(member);
size_t n_proxy;
const mp_obj_t *proxy = mp_obj_property_get(member, &n_proxy);
if (proxy[0] == mp_const_none) {
mp_raise_AttributeError(MP_ERROR_TEXT("unreadable attribute"));
} else {

View File

@ -1087,7 +1087,8 @@ void mp_convert_member_lookup(mp_obj_t self, const mp_obj_type_t *type, mp_obj_t
// be called by the descriptor code down below. But that way
// requires overhead for the nested mp_call's and overhead for
// the code.
const mp_obj_t *proxy = mp_obj_property_get(member);
size_t n_proxy;
const mp_obj_t *proxy = mp_obj_property_get(member, &n_proxy);
if (proxy[0] == mp_const_none) {
mp_raise_AttributeError(MP_ERROR_TEXT("unreadable attribute"));
} else {
@ -1226,15 +1227,16 @@ void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
// would be called by the descriptor code down below. But that way
// requires overhead for the nested mp_call's and overhead for
// the code.
const mp_obj_t *proxy = mp_obj_property_get(elem->value);
size_t n_proxy;
const mp_obj_t *proxy = mp_obj_property_get(elem->value, &n_proxy);
mp_obj_t dest[2] = {base, value};
if (value == MP_OBJ_NULL) {
// delete attribute
if (proxy[2] != mp_const_none) {
if (n_proxy == 3 && proxy[2] != mp_const_none) {
mp_call_function_n_kw(proxy[2], 1, 0, dest);
return;
}
} else if (proxy[1] != mp_const_none) {
} else if (n_proxy > 1 && proxy[1] != mp_const_none) {
mp_call_function_n_kw(proxy[1], 2, 0, dest);
return;
}