Skip to content

Commit 40b40ff

Browse files
committed
py/compile: Extract parse-node kind at start of func for efficiency.
Otherwise the type of parse-node and its kind has to be re-extracted multiple times. This optimisation reduces code size by a bit (16 bytes on bare-arm).
1 parent fa03bbf commit 40b40ff

1 file changed

Lines changed: 14 additions & 7 deletions

File tree

py/compile.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -586,8 +586,16 @@ STATIC void close_over_variables_etc(compiler_t *comp, scope_t *this_scope, int
586586
}
587587

588588
STATIC void compile_funcdef_lambdef_param(compiler_t *comp, mp_parse_node_t pn) {
589-
if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_star)
590-
|| MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_varargslist_star)) {
589+
// For efficiency of the code below we extract the parse-node kind here
590+
int pn_kind;
591+
if (MP_PARSE_NODE_IS_ID(pn)) {
592+
pn_kind = -1;
593+
} else {
594+
assert(MP_PARSE_NODE_IS_STRUCT(pn));
595+
pn_kind = MP_PARSE_NODE_STRUCT_KIND((mp_parse_node_struct_t*)pn);
596+
}
597+
598+
if (pn_kind == PN_typedargslist_star || pn_kind == PN_varargslist_star) {
591599
comp->have_star = true;
592600
/* don't need to distinguish bare from named star
593601
mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
@@ -598,23 +606,22 @@ STATIC void compile_funcdef_lambdef_param(compiler_t *comp, mp_parse_node_t pn)
598606
}
599607
*/
600608

601-
} else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_dbl_star)
602-
|| MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_varargslist_dbl_star)) {
609+
} else if (pn_kind == PN_typedargslist_dbl_star || pn_kind == PN_varargslist_dbl_star) {
603610
// named double star
604611
// TODO do we need to do anything with this?
605612

606613
} else {
607614
mp_parse_node_t pn_id;
608615
mp_parse_node_t pn_colon;
609616
mp_parse_node_t pn_equal;
610-
if (MP_PARSE_NODE_IS_ID(pn)) {
617+
if (pn_kind == -1) {
611618
// this parameter is just an id
612619

613620
pn_id = pn;
614621
pn_colon = MP_PARSE_NODE_NULL;
615622
pn_equal = MP_PARSE_NODE_NULL;
616623

617-
} else if (MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_typedargslist_name)) {
624+
} else if (pn_kind == PN_typedargslist_name) {
618625
// this parameter has a colon and/or equal specifier
619626

620627
mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;
@@ -623,7 +630,7 @@ STATIC void compile_funcdef_lambdef_param(compiler_t *comp, mp_parse_node_t pn)
623630
pn_equal = pns->nodes[2];
624631

625632
} else {
626-
assert(MP_PARSE_NODE_IS_STRUCT_KIND(pn, PN_varargslist_name)); // should be
633+
assert(pn_kind == PN_varargslist_name); // should be
627634
// this parameter has an equal specifier
628635

629636
mp_parse_node_struct_t *pns = (mp_parse_node_struct_t*)pn;

0 commit comments

Comments
 (0)