an tuple optimization

This commit is contained in:
blueloveTH 2023-05-27 19:54:29 +08:00
parent 6db30e61ce
commit 9d7204fe3a
2 changed files with 14 additions and 1 deletions

View File

@ -426,7 +426,14 @@ struct TupleExpr: SequenceExpr{
} }
if(starred_i == -1){ if(starred_i == -1){
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); ctx->emit(OP_UNPACK_SEQUENCE, items.size(), line);
}
}else{ }else{
// starred assignment target must be in a tuple // starred assignment target must be in a tuple
if(items.size() == 1) return false; if(items.size() == 1) return false;

View File

@ -1124,6 +1124,12 @@ inline void add_module_math(VM* vm){
inline void add_module_dis(VM* vm){ inline void add_module_dis(VM* vm){
PyObject* mod = vm->new_module("dis"); PyObject* mod = vm->new_module("dis");
vm->bind_func<1>(mod, "dis", [](VM* vm, ArgsView args) { 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, "<dis>", EXEC_MODE);
vm->_stdout(vm, vm->disassemble(code));
return vm->None;
}
PyObject* f = args[0]; PyObject* f = args[0];
if(is_type(f, vm->tp_bound_method)) f = CAST(BoundMethod, args[0]).func; if(is_type(f, vm->tp_bound_method)) f = CAST(BoundMethod, args[0]).func;
CodeObject_ code = CAST(Function&, f).decl->code; CodeObject_ code = CAST(Function&, f).decl->code;