From d96cfd13e3a464862cecffb2718c6286b52c77b0 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 9 Jan 2020 00:00:27 +1100 Subject: py/obj: Add MICROPY_OBJ_IMMEDIATE_OBJS option to reduce code size. This option (enabled by default for object representation A, B, C) makes None/False/True objects immediate objects, ie they are no longer a concrete object in ROM but are rather just values, eg None=0x6 for representation A. Doing this saves a considerable amount of code size, due to these objects being widely used: bare-arm: -392 -0.591% minimal x86: -252 -0.170% [incl +52(data)] unix x64: -624 -0.125% [incl -128(data)] unix nanbox: +0 +0.000% stm32: -1940 -0.510% PYBV10 cc3200: -1216 -0.659% esp8266: -404 -0.062% GENERIC esp32: -732 -0.064% GENERIC[incl +48(data)] nrf: -988 -0.675% pca10040 samd: -564 -0.556% ADAFRUIT_ITSYBITSY_M4_EXPRESS Thanks go to @Jongy aka Yonatan Goldschmidt for the idea. --- py/obj.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'py/obj.c') diff --git a/py/obj.c b/py/obj.c index f2a884754..6aacfb9cf 100644 --- a/py/obj.c +++ b/py/obj.c @@ -46,6 +46,11 @@ const mp_obj_type_t *mp_obj_get_type(mp_const_obj_t o_in) { } else if (mp_obj_is_float(o_in)) { return &mp_type_float; #endif + #if MICROPY_OBJ_IMMEDIATE_OBJS + } else if (mp_obj_is_immediate_obj(o_in)) { + static const mp_obj_type_t *const types[2] = {&mp_type_NoneType, &mp_type_bool}; + return types[MP_OBJ_IMMEDIATE_OBJ_VALUE(o_in) & 1]; + #endif } else { const mp_obj_base_t *o = MP_OBJ_TO_PTR(o_in); return o->type; -- cgit v1.2.3