mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-22 20:40:18 +00:00
cache some vars
This commit is contained in:
parent
71ce764c19
commit
68de579223
50
src/ceval.h
50
src/ceval.h
@ -26,6 +26,10 @@ inline PyObject* VM::_run_top_frame(){
|
|||||||
*/
|
*/
|
||||||
{
|
{
|
||||||
Bytecode byte = frame->next_bytecode();
|
Bytecode byte = frame->next_bytecode();
|
||||||
|
// cache
|
||||||
|
const CodeObject* co = frame->co;
|
||||||
|
const std::vector<StrName>& co_names = co->names;
|
||||||
|
const List& co_consts = co->consts;
|
||||||
|
|
||||||
#if PK_ENABLE_COMPUTED_GOTO
|
#if PK_ENABLE_COMPUTED_GOTO
|
||||||
static void* OP_LABELS[] = {
|
static void* OP_LABELS[] = {
|
||||||
@ -62,7 +66,7 @@ __NEXT_STEP:;
|
|||||||
/*****************************************/
|
/*****************************************/
|
||||||
TARGET(LOAD_CONST)
|
TARGET(LOAD_CONST)
|
||||||
heap._auto_collect();
|
heap._auto_collect();
|
||||||
frame->push(frame->co->consts[byte.arg]);
|
frame->push(co_consts[byte.arg]);
|
||||||
DISPATCH();
|
DISPATCH();
|
||||||
TARGET(LOAD_NONE) frame->push(None); DISPATCH();
|
TARGET(LOAD_NONE) frame->push(None); DISPATCH();
|
||||||
TARGET(LOAD_TRUE) frame->push(True); DISPATCH();
|
TARGET(LOAD_TRUE) frame->push(True); DISPATCH();
|
||||||
@ -70,7 +74,7 @@ __NEXT_STEP:;
|
|||||||
TARGET(LOAD_ELLIPSIS) frame->push(Ellipsis); DISPATCH();
|
TARGET(LOAD_ELLIPSIS) frame->push(Ellipsis); DISPATCH();
|
||||||
TARGET(LOAD_BUILTIN_EVAL) frame->push(builtins->attr(m_eval)); DISPATCH();
|
TARGET(LOAD_BUILTIN_EVAL) frame->push(builtins->attr(m_eval)); DISPATCH();
|
||||||
TARGET(LOAD_FUNCTION) {
|
TARGET(LOAD_FUNCTION) {
|
||||||
FuncDecl_ decl = frame->co->func_decls[byte.arg];
|
FuncDecl_ decl = co->func_decls[byte.arg];
|
||||||
PyObject* obj = VAR(Function({decl, frame->_module, frame->_locals}));
|
PyObject* obj = VAR(Function({decl, frame->_module, frame->_locals}));
|
||||||
frame->push(obj);
|
frame->push(obj);
|
||||||
} DISPATCH();
|
} DISPATCH();
|
||||||
@ -78,7 +82,7 @@ __NEXT_STEP:;
|
|||||||
/*****************************************/
|
/*****************************************/
|
||||||
TARGET(LOAD_NAME) {
|
TARGET(LOAD_NAME) {
|
||||||
heap._auto_collect();
|
heap._auto_collect();
|
||||||
StrName name = frame->co->names[byte.arg];
|
StrName name = co_names[byte.arg];
|
||||||
PyObject* val;
|
PyObject* val;
|
||||||
val = frame->f_locals().try_get(name);
|
val = frame->f_locals().try_get(name);
|
||||||
if(val != nullptr) { frame->push(val); DISPATCH(); }
|
if(val != nullptr) { frame->push(val); DISPATCH(); }
|
||||||
@ -92,7 +96,7 @@ __NEXT_STEP:;
|
|||||||
} DISPATCH();
|
} DISPATCH();
|
||||||
TARGET(LOAD_GLOBAL) {
|
TARGET(LOAD_GLOBAL) {
|
||||||
heap._auto_collect();
|
heap._auto_collect();
|
||||||
StrName name = frame->co->names[byte.arg];
|
StrName name = co_names[byte.arg];
|
||||||
PyObject* val = frame->f_globals().try_get(name);
|
PyObject* val = frame->f_globals().try_get(name);
|
||||||
if(val != nullptr) { frame->push(val); DISPATCH(); }
|
if(val != nullptr) { frame->push(val); DISPATCH(); }
|
||||||
val = vm->builtins->attr().try_get(name);
|
val = vm->builtins->attr().try_get(name);
|
||||||
@ -101,12 +105,12 @@ __NEXT_STEP:;
|
|||||||
} DISPATCH();
|
} DISPATCH();
|
||||||
TARGET(LOAD_ATTR) {
|
TARGET(LOAD_ATTR) {
|
||||||
PyObject* a = frame->top();
|
PyObject* a = frame->top();
|
||||||
StrName name = frame->co->names[byte.arg];
|
StrName name = co_names[byte.arg];
|
||||||
frame->top() = getattr(a, name);
|
frame->top() = getattr(a, name);
|
||||||
} DISPATCH();
|
} DISPATCH();
|
||||||
TARGET(LOAD_METHOD) {
|
TARGET(LOAD_METHOD) {
|
||||||
PyObject* a = frame->top();
|
PyObject* a = frame->top();
|
||||||
StrName name = frame->co->names[byte.arg];
|
StrName name = co_names[byte.arg];
|
||||||
PyObject* self;
|
PyObject* self;
|
||||||
frame->top() = get_unbound_method(a, name, &self, true, true);
|
frame->top() = get_unbound_method(a, name, &self, true, true);
|
||||||
frame->push(self);
|
frame->push(self);
|
||||||
@ -118,15 +122,15 @@ __NEXT_STEP:;
|
|||||||
frame->top() = fast_call(__getitem__, std::move(args));
|
frame->top() = fast_call(__getitem__, std::move(args));
|
||||||
} DISPATCH();
|
} DISPATCH();
|
||||||
TARGET(STORE_LOCAL) {
|
TARGET(STORE_LOCAL) {
|
||||||
StrName name = frame->co->names[byte.arg];
|
StrName name = co_names[byte.arg];
|
||||||
frame->f_locals().set(name, frame->popx());
|
frame->f_locals().set(name, frame->popx());
|
||||||
} DISPATCH();
|
} DISPATCH();
|
||||||
TARGET(STORE_GLOBAL) {
|
TARGET(STORE_GLOBAL) {
|
||||||
StrName name = frame->co->names[byte.arg];
|
StrName name = co_names[byte.arg];
|
||||||
frame->f_globals().set(name, frame->popx());
|
frame->f_globals().set(name, frame->popx());
|
||||||
} DISPATCH();
|
} DISPATCH();
|
||||||
TARGET(STORE_ATTR) {
|
TARGET(STORE_ATTR) {
|
||||||
StrName name = frame->co->names[byte.arg];
|
StrName name = co_names[byte.arg];
|
||||||
PyObject* a = frame->top();
|
PyObject* a = frame->top();
|
||||||
PyObject* val = frame->top_1();
|
PyObject* val = frame->top_1();
|
||||||
setattr(a, name, val);
|
setattr(a, name, val);
|
||||||
@ -140,7 +144,7 @@ __NEXT_STEP:;
|
|||||||
fast_call(__setitem__, std::move(args));
|
fast_call(__setitem__, std::move(args));
|
||||||
} DISPATCH();
|
} DISPATCH();
|
||||||
TARGET(DELETE_LOCAL) {
|
TARGET(DELETE_LOCAL) {
|
||||||
StrName name = frame->co->names[byte.arg];
|
StrName name = co_names[byte.arg];
|
||||||
if(frame->f_locals().contains(name)){
|
if(frame->f_locals().contains(name)){
|
||||||
frame->f_locals().erase(name);
|
frame->f_locals().erase(name);
|
||||||
}else{
|
}else{
|
||||||
@ -148,7 +152,7 @@ __NEXT_STEP:;
|
|||||||
}
|
}
|
||||||
} DISPATCH();
|
} DISPATCH();
|
||||||
TARGET(DELETE_GLOBAL) {
|
TARGET(DELETE_GLOBAL) {
|
||||||
StrName name = frame->co->names[byte.arg];
|
StrName name = co_names[byte.arg];
|
||||||
if(frame->f_globals().contains(name)){
|
if(frame->f_globals().contains(name)){
|
||||||
frame->f_globals().erase(name);
|
frame->f_globals().erase(name);
|
||||||
}else{
|
}else{
|
||||||
@ -157,7 +161,7 @@ __NEXT_STEP:;
|
|||||||
} DISPATCH();
|
} DISPATCH();
|
||||||
TARGET(DELETE_ATTR) {
|
TARGET(DELETE_ATTR) {
|
||||||
PyObject* a = frame->popx();
|
PyObject* a = frame->popx();
|
||||||
StrName name = frame->co->names[byte.arg];
|
StrName name = co_names[byte.arg];
|
||||||
if(!a->is_attr_valid()) TypeError("cannot delete attribute");
|
if(!a->is_attr_valid()) TypeError("cannot delete attribute");
|
||||||
if(!a->attr().contains(name)) AttributeError(a, name);
|
if(!a->attr().contains(name)) AttributeError(a, name);
|
||||||
a->attr().erase(name);
|
a->attr().erase(name);
|
||||||
@ -301,17 +305,17 @@ __NEXT_STEP:;
|
|||||||
else frame->pop();
|
else frame->pop();
|
||||||
DISPATCH();
|
DISPATCH();
|
||||||
TARGET(LOOP_CONTINUE) {
|
TARGET(LOOP_CONTINUE) {
|
||||||
int target = frame->co->blocks[byte.block].start;
|
int target = co->blocks[byte.block].start;
|
||||||
frame->jump_abs(target);
|
frame->jump_abs(target);
|
||||||
} DISPATCH();
|
} DISPATCH();
|
||||||
TARGET(LOOP_BREAK) {
|
TARGET(LOOP_BREAK) {
|
||||||
int target = frame->co->blocks[byte.block].end;
|
int target = co->blocks[byte.block].end;
|
||||||
frame->jump_abs_break(target);
|
frame->jump_abs_break(target);
|
||||||
} DISPATCH();
|
} DISPATCH();
|
||||||
TARGET(GOTO) {
|
TARGET(GOTO) {
|
||||||
StrName label = frame->co->names[byte.arg];
|
StrName label = co_names[byte.arg];
|
||||||
auto it = frame->co->labels.find(label);
|
auto it = co->labels.find(label);
|
||||||
if(it == frame->co->labels.end()) _error("KeyError", fmt("label ", label.escape(), " not found"));
|
if(it == co->labels.end()) _error("KeyError", fmt("label ", label.escape(), " not found"));
|
||||||
frame->jump_abs_break(it->second);
|
frame->jump_abs_break(it->second);
|
||||||
} DISPATCH();
|
} DISPATCH();
|
||||||
/*****************************************/
|
/*****************************************/
|
||||||
@ -384,13 +388,13 @@ __NEXT_STEP:;
|
|||||||
if(obj != nullptr){
|
if(obj != nullptr){
|
||||||
frame->push(obj);
|
frame->push(obj);
|
||||||
}else{
|
}else{
|
||||||
int target = frame->co->blocks[byte.block].end;
|
int target = co->blocks[byte.block].end;
|
||||||
frame->jump_abs_break(target);
|
frame->jump_abs_break(target);
|
||||||
}
|
}
|
||||||
} DISPATCH();
|
} DISPATCH();
|
||||||
/*****************************************/
|
/*****************************************/
|
||||||
TARGET(IMPORT_NAME) {
|
TARGET(IMPORT_NAME) {
|
||||||
StrName name = frame->co->names[byte.arg];
|
StrName name = co_names[byte.arg];
|
||||||
PyObject* ext_mod = _modules.try_get(name);
|
PyObject* ext_mod = _modules.try_get(name);
|
||||||
if(ext_mod == nullptr){
|
if(ext_mod == nullptr){
|
||||||
Str source;
|
Str source;
|
||||||
@ -447,7 +451,7 @@ __NEXT_STEP:;
|
|||||||
}; DISPATCH();
|
}; DISPATCH();
|
||||||
/*****************************************/
|
/*****************************************/
|
||||||
TARGET(BEGIN_CLASS) {
|
TARGET(BEGIN_CLASS) {
|
||||||
StrName name = frame->co->names[byte.arg];
|
StrName name = co_names[byte.arg];
|
||||||
PyObject* super_cls = frame->popx();
|
PyObject* super_cls = frame->popx();
|
||||||
if(super_cls == None) super_cls = _t(tp_object);
|
if(super_cls == None) super_cls = _t(tp_object);
|
||||||
check_type(super_cls, tp_type);
|
check_type(super_cls, tp_type);
|
||||||
@ -459,7 +463,7 @@ __NEXT_STEP:;
|
|||||||
cls->attr()._try_perfect_rehash();
|
cls->attr()._try_perfect_rehash();
|
||||||
}; DISPATCH();
|
}; DISPATCH();
|
||||||
TARGET(STORE_CLASS_ATTR) {
|
TARGET(STORE_CLASS_ATTR) {
|
||||||
StrName name = frame->co->names[byte.arg];
|
StrName name = co_names[byte.arg];
|
||||||
PyObject* obj = frame->popx();
|
PyObject* obj = frame->popx();
|
||||||
PyObject* cls = frame->top();
|
PyObject* cls = frame->top();
|
||||||
cls->attr().set(name, obj);
|
cls->attr().set(name, obj);
|
||||||
@ -484,13 +488,13 @@ __NEXT_STEP:;
|
|||||||
} DISPATCH();
|
} DISPATCH();
|
||||||
TARGET(EXCEPTION_MATCH) {
|
TARGET(EXCEPTION_MATCH) {
|
||||||
const auto& e = CAST(Exception&, frame->top());
|
const auto& e = CAST(Exception&, frame->top());
|
||||||
StrName name = frame->co->names[byte.arg];
|
StrName name = co_names[byte.arg];
|
||||||
frame->push(VAR(e.match_type(name)));
|
frame->push(VAR(e.match_type(name)));
|
||||||
} DISPATCH();
|
} DISPATCH();
|
||||||
TARGET(RAISE) {
|
TARGET(RAISE) {
|
||||||
PyObject* obj = frame->popx();
|
PyObject* obj = frame->popx();
|
||||||
Str msg = obj == None ? "" : CAST(Str, asStr(obj));
|
Str msg = obj == None ? "" : CAST(Str, asStr(obj));
|
||||||
StrName type = frame->co->names[byte.arg];
|
StrName type = co_names[byte.arg];
|
||||||
_error(type, msg);
|
_error(type, msg);
|
||||||
} DISPATCH();
|
} DISPATCH();
|
||||||
TARGET(RE_RAISE) _raise(); DISPATCH();
|
TARGET(RE_RAISE) _raise(); DISPATCH();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user