mirror of
https://github.com/micropython/micropython.git
synced 2026-01-04 19:20:22 +01:00
py: Optimise types for common case where type has a single parent type.
The common cases for inheritance are 0 or 1 parent types, for both built-in
types (eg built-in exceptions) as well as user defined types. So it makes
sense to optimise the case of 1 parent type by storing just the type and
not a tuple of 1 value (that value being the single parent type).
This patch makes such an optimisation. Even though there is a bit more
code to handle the two cases (either a single type or a tuple with 2 or
more values) it helps reduce overall code size because it eliminates the
need to create a static tuple to hold single parents (eg for the built-in
exceptions). It also helps reduce RAM usage for user defined types that
only derive from a single parent.
Changes in code size (in bytes) due to this patch:
bare-arm: -16
minimal (x86): -176
unix (x86-64): -320
unix nanbox: -384
stmhal: -64
cc3200: -32
esp8266: -108
This commit is contained in:
@@ -197,9 +197,6 @@ const mp_obj_type_t mp_type_BaseException = {
|
||||
.locals_dict = (mp_obj_dict_t*)&exc_locals_dict,
|
||||
};
|
||||
|
||||
#define MP_DEFINE_EXCEPTION_BASE(base_name) \
|
||||
STATIC const mp_rom_obj_tuple_t mp_type_ ## base_name ## _base_tuple = {{&mp_type_tuple}, 1, {MP_ROM_PTR(&mp_type_ ## base_name)}};\
|
||||
|
||||
#define MP_DEFINE_EXCEPTION(exc_name, base_name) \
|
||||
const mp_obj_type_t mp_type_ ## exc_name = { \
|
||||
{ &mp_type_type }, \
|
||||
@@ -207,23 +204,20 @@ const mp_obj_type_t mp_type_ ## exc_name = { \
|
||||
.print = mp_obj_exception_print, \
|
||||
.make_new = mp_obj_exception_make_new, \
|
||||
.attr = exception_attr, \
|
||||
.bases_tuple = (mp_obj_tuple_t*)(mp_rom_obj_tuple_t*)&mp_type_ ## base_name ## _base_tuple, \
|
||||
.parent = &mp_type_ ## base_name, \
|
||||
};
|
||||
|
||||
// List of all exceptions, arranged as in the table at:
|
||||
// http://docs.python.org/3/library/exceptions.html
|
||||
MP_DEFINE_EXCEPTION_BASE(BaseException)
|
||||
MP_DEFINE_EXCEPTION(SystemExit, BaseException)
|
||||
MP_DEFINE_EXCEPTION(KeyboardInterrupt, BaseException)
|
||||
MP_DEFINE_EXCEPTION(GeneratorExit, BaseException)
|
||||
MP_DEFINE_EXCEPTION(Exception, BaseException)
|
||||
MP_DEFINE_EXCEPTION_BASE(Exception)
|
||||
#if MICROPY_PY_ASYNC_AWAIT
|
||||
MP_DEFINE_EXCEPTION(StopAsyncIteration, Exception)
|
||||
#endif
|
||||
MP_DEFINE_EXCEPTION(StopIteration, Exception)
|
||||
MP_DEFINE_EXCEPTION(ArithmeticError, Exception)
|
||||
MP_DEFINE_EXCEPTION_BASE(ArithmeticError)
|
||||
//MP_DEFINE_EXCEPTION(FloatingPointError, ArithmeticError)
|
||||
MP_DEFINE_EXCEPTION(OverflowError, ArithmeticError)
|
||||
MP_DEFINE_EXCEPTION(ZeroDivisionError, ArithmeticError)
|
||||
@@ -235,18 +229,15 @@ MP_DEFINE_EXCEPTION(Exception, BaseException)
|
||||
MP_DEFINE_EXCEPTION(ImportError, Exception)
|
||||
//MP_DEFINE_EXCEPTION(IOError, Exception) use OSError instead
|
||||
MP_DEFINE_EXCEPTION(LookupError, Exception)
|
||||
MP_DEFINE_EXCEPTION_BASE(LookupError)
|
||||
MP_DEFINE_EXCEPTION(IndexError, LookupError)
|
||||
MP_DEFINE_EXCEPTION(KeyError, LookupError)
|
||||
MP_DEFINE_EXCEPTION(MemoryError, Exception)
|
||||
MP_DEFINE_EXCEPTION(NameError, Exception)
|
||||
/*
|
||||
MP_DEFINE_EXCEPTION_BASE(NameError)
|
||||
MP_DEFINE_EXCEPTION(UnboundLocalError, NameError)
|
||||
*/
|
||||
MP_DEFINE_EXCEPTION(OSError, Exception)
|
||||
#if MICROPY_PY_BUILTINS_TIMEOUTERROR
|
||||
MP_DEFINE_EXCEPTION_BASE(OSError)
|
||||
MP_DEFINE_EXCEPTION(TimeoutError, OSError)
|
||||
#endif
|
||||
/*
|
||||
@@ -267,30 +258,24 @@ MP_DEFINE_EXCEPTION(Exception, BaseException)
|
||||
MP_DEFINE_EXCEPTION(ReferenceError, Exception)
|
||||
*/
|
||||
MP_DEFINE_EXCEPTION(RuntimeError, Exception)
|
||||
MP_DEFINE_EXCEPTION_BASE(RuntimeError)
|
||||
MP_DEFINE_EXCEPTION(NotImplementedError, RuntimeError)
|
||||
MP_DEFINE_EXCEPTION(SyntaxError, Exception)
|
||||
MP_DEFINE_EXCEPTION_BASE(SyntaxError)
|
||||
MP_DEFINE_EXCEPTION(IndentationError, SyntaxError)
|
||||
/*
|
||||
MP_DEFINE_EXCEPTION_BASE(IndentationError)
|
||||
MP_DEFINE_EXCEPTION(TabError, IndentationError)
|
||||
*/
|
||||
//MP_DEFINE_EXCEPTION(SystemError, Exception)
|
||||
MP_DEFINE_EXCEPTION(TypeError, Exception)
|
||||
#if MICROPY_EMIT_NATIVE
|
||||
MP_DEFINE_EXCEPTION_BASE(TypeError)
|
||||
MP_DEFINE_EXCEPTION(ViperTypeError, TypeError)
|
||||
#endif
|
||||
MP_DEFINE_EXCEPTION(ValueError, Exception)
|
||||
#if MICROPY_PY_BUILTINS_STR_UNICODE
|
||||
MP_DEFINE_EXCEPTION_BASE(ValueError)
|
||||
MP_DEFINE_EXCEPTION(UnicodeError, ValueError)
|
||||
//TODO: Implement more UnicodeError subclasses which take arguments
|
||||
#endif
|
||||
/*
|
||||
MP_DEFINE_EXCEPTION(Warning, Exception)
|
||||
MP_DEFINE_EXCEPTION_BASE(Warning)
|
||||
MP_DEFINE_EXCEPTION(DeprecationWarning, Warning)
|
||||
MP_DEFINE_EXCEPTION(PendingDeprecationWarning, Warning)
|
||||
MP_DEFINE_EXCEPTION(RuntimeWarning, Warning)
|
||||
|
||||
Reference in New Issue
Block a user