Browse Source

an tuple optimization

blueloveTH 2 years ago
parent
commit
9d7204fe3a
2 changed files with 14 additions and 1 deletions
  1. 8 1
      src/expr.h
  2. 6 0
      src/pocketpy.h

+ 8 - 1
src/expr.h

@@ -426,7 +426,14 @@ struct TupleExpr: SequenceExpr{
         }
         }
 
 
         if(starred_i == -1){
         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{
         }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;

+ 6 - 0
src/pocketpy.h

@@ -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;