Jelajahi Sumber

Revert "some optimize"

This reverts commit 31dc34663a0a2ffe45552eec7ceaca58759d228d.
blueloveTH 2 tahun lalu
induk
melakukan
63e869df0c
5 mengubah file dengan 23 tambahan dan 16 penghapusan
  1. 12 5
      include/pocketpy/frame.h
  2. 4 4
      src/ceval.cpp
  3. 5 5
      src/frame.cpp
  4. 1 1
      src/pocketpy.cpp
  5. 1 1
      src/vm.cpp

+ 12 - 5
include/pocketpy/frame.h

@@ -10,15 +10,22 @@ namespace pkpy{
 
 // weak reference fast locals
 struct FastLocals{
+    // this is a weak reference
+    const NameDictInt* varnames_inv;
     PyObject** a;
 
+    int size() const{ return varnames_inv->size();}
+
     PyObject*& operator[](int i){ return a[i]; }
     PyObject* operator[](int i) const { return a[i]; }
 
-    FastLocals(PyObject** a): a(a) {}
+    FastLocals(const CodeObject* co, PyObject** a): varnames_inv(&co->varnames_inv), a(a) {}
+
+    PyObject** try_get_name(StrName name);
+    NameDict_ to_namedict();
 
-    PyObject** try_get_name(const CodeObject* co, StrName name);
-    NameDict_ to_namedict(const CodeObject* co);
+    PyObject** begin() const { return a; }
+    PyObject** end() const { return a + size(); }
 };
 
 template<size_t MAX_SIZE>
@@ -84,13 +91,13 @@ struct Frame {
     PyObject* f_closure_try_get(StrName name);
 
     Frame(PyObject** p0, const CodeObject* co, PyObject* _module, PyObject* _callable)
-            : _ip(-1), _next_ip(0), _sp_base(p0), co(co), _module(_module), _callable(_callable), _locals(p0) { }
+            : _ip(-1), _next_ip(0), _sp_base(p0), co(co), _module(_module), _callable(_callable), _locals(co, p0) { }
 
     Frame(PyObject** p0, const CodeObject* co, PyObject* _module, PyObject* _callable, FastLocals _locals)
             : _ip(-1), _next_ip(0), _sp_base(p0), co(co), _module(_module), _callable(_callable), _locals(_locals) { }
 
     Frame(PyObject** p0, const CodeObject_& co, PyObject* _module)
-            : _ip(-1), _next_ip(0), _sp_base(p0), co(co.get()), _module(_module), _callable(nullptr), _locals(p0) {}
+            : _ip(-1), _next_ip(0), _sp_base(p0), co(co.get()), _module(_module), _callable(nullptr), _locals(co.get(), p0) {}
 
     int next_bytecode() {
         _ip = _next_ip++;

+ 4 - 4
src/ceval.cpp

@@ -118,7 +118,7 @@ __NEXT_STEP:;
         FuncDecl_ decl = co->func_decls[byte.arg];
         PyObject* obj;
         if(decl->nested){
-            NameDict_ captured = frame->_locals.to_namedict(co);
+            NameDict_ captured = frame->_locals.to_namedict();
             obj = VAR(Function(decl, frame->_module, nullptr, captured));
             captured->set(decl->code->name, obj);
         }else{
@@ -136,7 +136,7 @@ __NEXT_STEP:;
     } DISPATCH();
     TARGET(LOAD_NAME) {
         StrName _name(byte.arg);
-        PyObject** slot = frame->_locals.try_get_name(co, _name);
+        PyObject** slot = frame->_locals.try_get_name(_name);
         if(slot != nullptr) {
             if(*slot == PY_NULL) vm->UnboundLocalError(_name);
             PUSH(*slot);
@@ -207,7 +207,7 @@ __NEXT_STEP:;
         StrName _name(byte.arg);
         PyObject* _0 = POPX();
         if(frame->_callable != nullptr){
-            PyObject** slot = frame->_locals.try_get_name(co, _name);
+            PyObject** slot = frame->_locals.try_get_name(_name);
             if(slot == nullptr) vm->UnboundLocalError(_name);
             *slot = _0;
         }else{
@@ -242,7 +242,7 @@ __NEXT_STEP:;
     TARGET(DELETE_NAME){
         StrName _name(byte.arg);
         if(frame->_callable != nullptr){
-            PyObject** slot = frame->_locals.try_get_name(co, _name);
+            PyObject** slot = frame->_locals.try_get_name(_name);
             if(slot == nullptr) vm->UnboundLocalError(_name);
             *slot = PY_NULL;
         }else{

+ 5 - 5
src/frame.cpp

@@ -1,15 +1,15 @@
 #include "pocketpy/frame.h"
 
 namespace pkpy{
-    PyObject** FastLocals::try_get_name(const CodeObject* co, StrName name){
-        int index = co->varnames_inv.try_get(name);
+    PyObject** FastLocals::try_get_name(StrName name){
+        int index = varnames_inv->try_get(name);
         if(index == -1) return nullptr;
         return &a[index];
     }
 
-    NameDict_ FastLocals::to_namedict(const CodeObject* co){
+    NameDict_ FastLocals::to_namedict(){
         NameDict_ dict = std::make_shared<NameDict>();
-        co->varnames_inv.apply([&](StrName name, int index){
+        varnames_inv->apply([&](StrName name, int index){
             PyObject* value = a[index];
             if(value != PY_NULL) dict->set(name, value);
         });
@@ -35,7 +35,7 @@ namespace pkpy{
         // get the stack size of the try block
         int _stack_size = co->blocks[block].base_stack_size;
         if(stack_size(_s) < _stack_size) throw std::runtime_error(_S("invalid state: ", stack_size(_s), '<', _stack_size).str());
-        _s->reset(actual_sp_base() + co->varnames.size() + _stack_size);          // rollback the stack   
+        _s->reset(actual_sp_base() + _locals.size() + _stack_size);          // rollback the stack   
         _s->push(obj);                                      // push exception object
         _next_ip = co->blocks[block].end;
         return true;

+ 1 - 1
src/pocketpy.cpp

@@ -80,7 +80,7 @@ void init_builtins(VM* _vm) {
             FrameId frame = vm->top_frame();
             if(frame->_callable != nullptr){
                 class_arg = PK_OBJ_GET(Function, frame->_callable)._class;
-                if(!frame->co->varnames.empty()) self_arg = frame->_locals[0];
+                if(frame->_locals.size() > 0) self_arg = frame->_locals[0];
             }
             if(class_arg == nullptr || self_arg == nullptr){
                 vm->TypeError("super(): unable to determine the class context, use super(class, self) instead");

+ 1 - 1
src/vm.cpp

@@ -941,7 +941,7 @@ PyObject* VM::vectorcall(int ARGC, int KWARGC, bool op_call){
         for(int j=0; j<co_nlocals; j++) _base[j] = buffer[j];
 
 __FAST_CALL:
-        callstack.emplace(p0, co, fn._module, callable, FastLocals(args.begin()));
+        callstack.emplace(p0, co, fn._module, callable, FastLocals(co, args.begin()));
         if(op_call) return PY_OP_CALL;
         return _run_top_frame();
         /*****************_py_call*****************/