some optimize

This commit is contained in:
blueloveTH 2024-04-29 21:45:10 +08:00
parent 1227667735
commit aba90c1eb1
5 changed files with 26 additions and 26 deletions

View File

@ -309,7 +309,8 @@ public:
} }
i64 normalized_index(i64 index, int size); i64 normalized_index(i64 index, int size);
PyObject* py_next(PyObject* obj); PyObject* py_next(PyObject*);
PyObject* _py_next(const PyTypeInfo*, PyObject*);
PyObject* _pack_next_retval(unsigned); PyObject* _pack_next_retval(unsigned);
bool py_callable(PyObject* obj); bool py_callable(PyObject* obj);

View File

@ -48,12 +48,13 @@ void VM::_op_unpack_sequence(uint16_t arg){
}else{ }else{
auto _lock = heap.gc_scope_lock(); // lock the gc via RAII!! auto _lock = heap.gc_scope_lock(); // lock the gc via RAII!!
_0 = py_iter(_0); _0 = py_iter(_0);
const PyTypeInfo* ti = _inst_type_info(_0);
for(int i=0; i<arg; i++){ for(int i=0; i<arg; i++){
PyObject* _1 = py_next(_0); PyObject* _1 = _py_next(ti, _0);
if(_1 == StopIteration) ValueError("not enough values to unpack"); if(_1 == StopIteration) ValueError("not enough values to unpack");
PUSH(_1); PUSH(_1);
} }
if(py_next(_0) != StopIteration) ValueError("too many values to unpack"); if(_py_next(ti, _0) != StopIteration) ValueError("too many values to unpack");
} }
} }
@ -850,15 +851,16 @@ __NEXT_STEP:;
TARGET(UNPACK_EX) { TARGET(UNPACK_EX) {
auto _lock = heap.gc_scope_lock(); // lock the gc via RAII!! auto _lock = heap.gc_scope_lock(); // lock the gc via RAII!!
PyObject* _0 = py_iter(POPX()); PyObject* _0 = py_iter(POPX());
const PyTypeInfo* _ti = _inst_type_info(_0);
PyObject* _1; PyObject* _1;
for(int i=0; i<byte.arg; i++){ for(int i=0; i<byte.arg; i++){
_1 = py_next(_0); _1 = _py_next(_ti, _0);
if(_1 == StopIteration) ValueError("not enough values to unpack"); if(_1 == StopIteration) ValueError("not enough values to unpack");
PUSH(_1); PUSH(_1);
} }
List extras; List extras;
while(true){ while(true){
_1 = py_next(_0); _1 = _py_next(_ti, _0);
if(_1 == StopIteration) break; if(_1 == StopIteration) break;
extras.push_back(_1); extras.push_back(_1);
} }

View File

@ -683,11 +683,12 @@ void init_builtins(VM* _vm) {
const Str& self = _CAST(Str&, args[0]); const Str& self = _CAST(Str&, args[0]);
SStream ss; SStream ss;
PyObject* it = vm->py_iter(args[1]); // strong ref PyObject* it = vm->py_iter(args[1]); // strong ref
PyObject* obj = vm->py_next(it); const PyTypeInfo* info = vm->_inst_type_info(args[1]);
PyObject* obj = vm->_py_next(info, it);
while(obj != vm->StopIteration){ while(obj != vm->StopIteration){
if(!ss.empty()) ss << self; if(!ss.empty()) ss << self;
ss << CAST(Str&, obj); ss << CAST(Str&, obj);
obj = vm->py_next(it); obj = vm->_py_next(info, it);
} }
return VAR(ss.str()); return VAR(ss.str());
}); });
@ -904,10 +905,11 @@ void init_builtins(VM* _vm) {
auto _lock = vm->heap.gc_scope_lock(); auto _lock = vm->heap.gc_scope_lock();
List& self = _CAST(List&, args[0]); List& self = _CAST(List&, args[0]);
PyObject* it = vm->py_iter(args[1]); // strong ref PyObject* it = vm->py_iter(args[1]); // strong ref
PyObject* obj = vm->py_next(it); const PyTypeInfo* info = vm->_inst_type_info(args[1]);
PyObject* obj = vm->_py_next(info, it);
while(obj != vm->StopIteration){ while(obj != vm->StopIteration){
self.push_back(obj); self.push_back(obj);
obj = vm->py_next(it); obj = vm->_py_next(info, it);
} }
return vm->None; return vm->None;
}); });

View File

@ -250,8 +250,7 @@ namespace pkpy{
return index; return index;
} }
PyObject* VM::py_next(PyObject* obj){ PyObject* VM::_py_next(const PyTypeInfo* ti, PyObject* obj){
const PyTypeInfo* ti = _inst_type_info(obj);
if(ti->m__next__){ if(ti->m__next__){
unsigned n = ti->m__next__(this, obj); unsigned n = ti->m__next__(this, obj);
return _pack_next_retval(n); return _pack_next_retval(n);
@ -259,6 +258,11 @@ namespace pkpy{
return call_method(obj, __next__); return call_method(obj, __next__);
} }
PyObject* VM::py_next(PyObject* obj){
const PyTypeInfo* ti = _inst_type_info(obj);
return _py_next(ti, obj);
}
bool VM::py_callable(PyObject* obj){ bool VM::py_callable(PyObject* obj){
Type cls = vm->_tp(obj); Type cls = vm->_tp(obj);
switch(cls.index){ switch(cls.index){
@ -381,10 +385,11 @@ PyObject* VM::py_list(PyObject* it){
auto _lock = heap.gc_scope_lock(); auto _lock = heap.gc_scope_lock();
it = py_iter(it); it = py_iter(it);
List list; List list;
PyObject* obj = py_next(it); const PyTypeInfo* info = _inst_type_info(it);
PyObject* obj = _py_next(info, it);
while(obj != StopIteration){ while(obj != StopIteration){
list.push_back(obj); list.push_back(obj);
obj = py_next(it); obj = _py_next(info, it);
} }
return VAR(std::move(list)); return VAR(std::move(list));
} }
@ -771,10 +776,11 @@ void VM::_unpack_as_list(ArgsView args, List& list){
// maybe this check should be done in the compile time // maybe this check should be done in the compile time
if(w.level != 1) TypeError("expected level 1 star wrapper"); if(w.level != 1) TypeError("expected level 1 star wrapper");
PyObject* _0 = py_iter(w.obj); PyObject* _0 = py_iter(w.obj);
PyObject* _1 = py_next(_0); const PyTypeInfo* info = _inst_type_info(_0);
PyObject* _1 = _py_next(info, _0);
while(_1 != StopIteration){ while(_1 != StopIteration){
list.push_back(_1); list.push_back(_1);
_1 = py_next(_0); _1 = _py_next(info, _0);
} }
}else{ }else{
list.push_back(obj); list.push_back(obj);

View File

@ -268,17 +268,6 @@ assert type(12 * [12]) is list
# /************ tuple ************/ # /************ tuple ************/
# 未完全测试准确性-----------------------------------------------
# 180: 783: _vm->bind_constructor<-1>("tuple", [](VM* vm, ArgsView args) {
# 32: 784: if(args.size() == 1+0) return VAR(Tuple(0));
# 32: 785: if(args.size() == 1+1){
# 32: 786: List list = CAST(List, vm->py_list(args[1]));
# 32: 787: return VAR(Tuple(std::move(list)));
# 32: 788: }
# #####: 789: vm->TypeError("tuple() takes at most 1 argument");
# #####: 790: return vm->None;
# 32: 791: });
# -: 792:
# test tuple: # test tuple:
try: try:
tuple(1,2) tuple(1,2)