From 63e869df0c5f2e42cc59a73d7bcd6043e95ca91c Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sat, 16 Mar 2024 15:54:56 +0800 Subject: [PATCH] Revert "some optimize" This reverts commit 31dc34663a0a2ffe45552eec7ceaca58759d228d. --- include/pocketpy/frame.h | 17 ++++++++++++----- src/ceval.cpp | 8 ++++---- src/frame.cpp | 10 +++++----- src/pocketpy.cpp | 2 +- src/vm.cpp | 2 +- 5 files changed, 23 insertions(+), 16 deletions(-) diff --git a/include/pocketpy/frame.h b/include/pocketpy/frame.h index 043f8704..90549026 100644 --- a/include/pocketpy/frame.h +++ b/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(const CodeObject* co, StrName name); - NameDict_ to_namedict(const CodeObject* co); + PyObject** try_get_name(StrName name); + NameDict_ to_namedict(); + + PyObject** begin() const { return a; } + PyObject** end() const { return a + size(); } }; template @@ -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++; diff --git a/src/ceval.cpp b/src/ceval.cpp index 05ba3862..d331ee91 100644 --- a/src/ceval.cpp +++ b/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{ diff --git a/src/frame.cpp b/src/frame.cpp index b995dd9b..38b8c02f 100644 --- a/src/frame.cpp +++ b/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(); - 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; diff --git a/src/pocketpy.cpp b/src/pocketpy.cpp index 2dab3e03..ce4df652 100644 --- a/src/pocketpy.cpp +++ b/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"); diff --git a/src/vm.cpp b/src/vm.cpp index 01d2cf18..e4f38144 100644 --- a/src/vm.cpp +++ b/src/vm.cpp @@ -941,7 +941,7 @@ PyObject* VM::vectorcall(int ARGC, int KWARGC, bool op_call){ for(int j=0; j