Revert "some optimize"

This reverts commit 31dc34663a0a2ffe45552eec7ceaca58759d228d.
This commit is contained in:
blueloveTH 2024-03-16 15:54:56 +08:00
parent 31dc34663a
commit 63e869df0c
5 changed files with 23 additions and 16 deletions

View File

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

View File

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

View File

@ -1,15 +1,15 @@
#include "pocketpy/frame.h" #include "pocketpy/frame.h"
namespace pkpy{ namespace pkpy{
PyObject** FastLocals::try_get_name(const CodeObject* co, StrName name){ PyObject** FastLocals::try_get_name(StrName name){
int index = co->varnames_inv.try_get(name); int index = varnames_inv->try_get(name);
if(index == -1) return nullptr; if(index == -1) return nullptr;
return &a[index]; return &a[index];
} }
NameDict_ FastLocals::to_namedict(const CodeObject* co){ NameDict_ FastLocals::to_namedict(){
NameDict_ dict = std::make_shared<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]; PyObject* value = a[index];
if(value != PY_NULL) dict->set(name, value); if(value != PY_NULL) dict->set(name, value);
}); });
@ -35,7 +35,7 @@ namespace pkpy{
// get the stack size of the try block // get the stack size of the try block
int _stack_size = co->blocks[block].base_stack_size; 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()); 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 _s->push(obj); // push exception object
_next_ip = co->blocks[block].end; _next_ip = co->blocks[block].end;
return true; return true;

View File

@ -80,7 +80,7 @@ void init_builtins(VM* _vm) {
FrameId frame = vm->top_frame(); FrameId frame = vm->top_frame();
if(frame->_callable != nullptr){ if(frame->_callable != nullptr){
class_arg = PK_OBJ_GET(Function, frame->_callable)._class; 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){ if(class_arg == nullptr || self_arg == nullptr){
vm->TypeError("super(): unable to determine the class context, use super(class, self) instead"); vm->TypeError("super(): unable to determine the class context, use super(class, self) instead");

View File

@ -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]; for(int j=0; j<co_nlocals; j++) _base[j] = buffer[j];
__FAST_CALL: __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; if(op_call) return PY_OP_CALL;
return _run_top_frame(); return _run_top_frame();
/*****************_py_call*****************/ /*****************_py_call*****************/