Skip to content

Commit fbcaf0e

Browse files
committed
py: Slightly simplify compile and emit of star/double-star arguments.
Saves a few bytes of code space and eliminates need for rot_two bytecode (hence saving RAM and execution time, by a tiny bit).
1 parent e6978a4 commit fbcaf0e

File tree

3 files changed

+12
-29
lines changed

3 files changed

+12
-29
lines changed

py/compile.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2252,11 +2252,18 @@ STATIC void compile_trailer_paren_helper(compiler_t *comp, mp_parse_node_t pn_ar
22522252
}
22532253

22542254
// compile the star/double-star arguments if we had them
2255-
if (star_args_node != NULL) {
2256-
compile_node(comp, star_args_node->nodes[0]);
2257-
}
2258-
if (dblstar_args_node != NULL) {
2259-
compile_node(comp, dblstar_args_node->nodes[0]);
2255+
// if we had one but not the other then we load "null" as a place holder
2256+
if (star_flags != 0) {
2257+
if (star_args_node == NULL) {
2258+
EMIT(load_null);
2259+
} else {
2260+
compile_node(comp, star_args_node->nodes[0]);
2261+
}
2262+
if (dblstar_args_node == NULL) {
2263+
EMIT(load_null);
2264+
} else {
2265+
compile_node(comp, dblstar_args_node->nodes[0]);
2266+
}
22602267
}
22612268

22622269
// emit the function/method call

py/emitbc.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -859,14 +859,6 @@ void mp_emit_bc_make_closure(emit_t *emit, scope_t *scope, mp_uint_t n_closed_ov
859859

860860
STATIC void emit_bc_call_function_method_helper(emit_t *emit, mp_int_t stack_adj, mp_uint_t bytecode_base, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags) {
861861
if (star_flags) {
862-
if (!(star_flags & MP_EMIT_STAR_FLAG_SINGLE)) {
863-
// load dummy entry for non-existent pos_seq
864-
mp_emit_bc_load_null(emit);
865-
mp_emit_bc_rot_two(emit);
866-
} else if (!(star_flags & MP_EMIT_STAR_FLAG_DOUBLE)) {
867-
// load dummy entry for non-existent kw_dict
868-
mp_emit_bc_load_null(emit);
869-
}
870862
emit_bc_pre(emit, stack_adj - (mp_int_t)n_positional - 2 * (mp_int_t)n_keyword - 2);
871863
emit_write_bytecode_byte_uint(emit, bytecode_base + 1, (n_keyword << 8) | n_positional); // TODO make it 2 separate uints?
872864
} else {

py/emitnative.c

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2316,14 +2316,6 @@ STATIC void emit_native_call_function(emit_t *emit, mp_uint_t n_positional, mp_u
23162316
} else {
23172317
assert(vtype_fun == VTYPE_PYOBJ);
23182318
if (star_flags) {
2319-
if (!(star_flags & MP_EMIT_STAR_FLAG_SINGLE)) {
2320-
// load dummy entry for non-existent pos_seq
2321-
emit_native_load_null(emit);
2322-
emit_native_rot_two(emit);
2323-
} else if (!(star_flags & MP_EMIT_STAR_FLAG_DOUBLE)) {
2324-
// load dummy entry for non-existent kw_dict
2325-
emit_native_load_null(emit);
2326-
}
23272319
emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword + 3); // pointer to args
23282320
emit_call_with_2_imm_args(emit, MP_F_CALL_METHOD_N_KW_VAR, 0, REG_ARG_1, n_positional | (n_keyword << 8), REG_ARG_2);
23292321
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);
@@ -2340,14 +2332,6 @@ STATIC void emit_native_call_function(emit_t *emit, mp_uint_t n_positional, mp_u
23402332

23412333
STATIC void emit_native_call_method(emit_t *emit, mp_uint_t n_positional, mp_uint_t n_keyword, mp_uint_t star_flags) {
23422334
if (star_flags) {
2343-
if (!(star_flags & MP_EMIT_STAR_FLAG_SINGLE)) {
2344-
// load dummy entry for non-existent pos_seq
2345-
emit_native_load_null(emit);
2346-
emit_native_rot_two(emit);
2347-
} else if (!(star_flags & MP_EMIT_STAR_FLAG_DOUBLE)) {
2348-
// load dummy entry for non-existent kw_dict
2349-
emit_native_load_null(emit);
2350-
}
23512335
emit_get_stack_pointer_to_reg_for_pop(emit, REG_ARG_3, n_positional + 2 * n_keyword + 4); // pointer to args
23522336
emit_call_with_2_imm_args(emit, MP_F_CALL_METHOD_N_KW_VAR, 1, REG_ARG_1, n_positional | (n_keyword << 8), REG_ARG_2);
23532337
emit_post_push_reg(emit, VTYPE_PYOBJ, REG_RET);

0 commit comments

Comments
 (0)