From 9d7204fe3abe6b18a38e7e5abd94b6045d7aa8b8 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sat, 27 May 2023 19:54:29 +0800 Subject: [PATCH] an tuple optimization --- src/expr.h | 9 ++++++++- src/pocketpy.h | 6 ++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/expr.h b/src/expr.h index 1a249b9d..c735186d 100644 --- a/src/expr.h +++ b/src/expr.h @@ -426,7 +426,14 @@ struct TupleExpr: SequenceExpr{ } if(starred_i == -1){ - ctx->emit(OP_UNPACK_SEQUENCE, items.size(), line); + Bytecode& prev = ctx->co->codes.back(); + if(prev.op == OP_BUILD_TUPLE && prev.arg == items.size()){ + // build tuple and unpack it is meaningless + prev.op = OP_NO_OP; + prev.arg = BC_NOARG; + }else{ + ctx->emit(OP_UNPACK_SEQUENCE, items.size(), line); + } }else{ // starred assignment target must be in a tuple if(items.size() == 1) return false; diff --git a/src/pocketpy.h b/src/pocketpy.h index 92bb40ed..660d9fec 100644 --- a/src/pocketpy.h +++ b/src/pocketpy.h @@ -1124,6 +1124,12 @@ inline void add_module_math(VM* vm){ inline void add_module_dis(VM* vm){ PyObject* mod = vm->new_module("dis"); vm->bind_func<1>(mod, "dis", [](VM* vm, ArgsView args) { + if(is_type(args[0], vm->tp_str)){ + const Str& source = CAST(Str, args[0]); + CodeObject_ code = vm->compile(source, "", EXEC_MODE); + vm->_stdout(vm, vm->disassemble(code)); + return vm->None; + } PyObject* f = args[0]; if(is_type(f, vm->tp_bound_method)) f = CAST(BoundMethod, args[0]).func; CodeObject_ code = CAST(Function&, f).decl->code;