diff --git a/py/compile.c b/py/compile.c index 3aa70e8bf2..ae5c3b2dc8 100644 --- a/py/compile.c +++ b/py/compile.c @@ -980,7 +980,8 @@ STATIC void compile_return_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) { if (MP_PARSE_NODE_IS_NULL(pns->nodes[0])) { // no argument to 'return', so return None EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE); - } else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) { + } else if (MICROPY_COMP_RETURN_IF_EXPR + && MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_test_if_expr)) { // special case when returning an if-expression; to match CPython optimisation mp_parse_node_struct_t *pns_test_if_expr = (mp_parse_node_struct_t*)pns->nodes[0]; mp_parse_node_struct_t *pns_test_if_else = (mp_parse_node_struct_t*)pns_test_if_expr->nodes[1]; diff --git a/py/mpconfig.h b/py/mpconfig.h index b6e24d2055..9122c99163 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -353,6 +353,12 @@ #define MICROPY_COMP_TRIPLE_TUPLE_ASSIGN (0) #endif +// Whether to enable optimisation of: return a if b else c +// Costs about 80 bytes (Thumb2) and saves 2 bytes of bytecode for each use +#ifndef MICROPY_COMP_RETURN_IF_EXPR +#define MICROPY_COMP_RETURN_IF_EXPR (0) +#endif + /*****************************************************************************/ /* Internal debugging stuff */