This commit is contained in:
blueloveTH 2022-11-20 03:26:45 +08:00
parent d143564d82
commit b05594ce58
6 changed files with 51 additions and 29 deletions

View File

@ -153,7 +153,7 @@ public:
inline PyVar __pop(){
if(s_data.empty()) throw std::runtime_error("s_data.empty() is true");
PyVar v = s_data.back();
PyVar v = std::move(s_data.back());
s_data.pop_back();
return v;
}
@ -173,10 +173,14 @@ public:
return __deref_pointer(vm, s_data[s_data.size() + n]);
}
inline void push(PyVar v){
inline void push(const PyVar& v){
s_data.push_back(v);
}
inline void push(PyVar&& v){
s_data.emplace_back(std::move(v));
}
void __reportForIter(){
int lastIp = ip - 1;
@ -209,7 +213,7 @@ public:
PyVarList popNValuesReversed(VM* vm, int n){
PyVarList v(n);
for(int i=n-1; i>=0; i--) v[i] = popValue(vm);
for(int i=n-1; i>=0; i--) v[i] = std::move(popValue(vm));
return v;
}

View File

@ -2,7 +2,7 @@
#include "pocketpy.h"
//#define PK_DEBUG_TIME
#define PK_DEBUG_TIME
struct Timer{
const char* title;

View File

@ -32,14 +32,13 @@
_raw = new PyObject(_native); \
_raw->setType(ptype); \
} \
PyVar obj = PyVar(_raw, [this](PyObject* p){\
return PyVar(_raw, [this](PyObject* p){ \
if(_pool##name.size() < max_size){ \
_pool##name.push_back(p); \
}else{ \
delete p; \
} \
}); \
return obj; \
}
typedef void(*PrintFn)(const VM*, const char*);
@ -84,7 +83,7 @@ private:
case OP_STORE_PTR: {
PyVar obj = frame->popValue(this);
_Pointer p = PyPointer_AS_C(frame->__pop());
p->set(this, frame, obj);
p->set(this, frame, std::move(obj));
} break;
case OP_DELETE_PTR: {
_Pointer p = PyPointer_AS_C(frame->__pop());
@ -161,13 +160,13 @@ private:
{
PyVar rhs = frame->popValue(this);
PyVar lhs = frame->popValue(this);
frame->push(fastCall(lhs, BINARY_SPECIAL_METHODS[byte.arg], {lhs,rhs}));
frame->push(fastCall(lhs, BINARY_SPECIAL_METHODS[byte.arg], {lhs,std::move(rhs)}));
} break;
case OP_BITWISE_OP:
{
PyVar rhs = frame->popValue(this);
PyVar lhs = frame->popValue(this);
frame->push(fastCall(lhs, BITWISE_SPECIAL_METHODS[byte.arg], {lhs,rhs}));
frame->push(fastCall(lhs, BITWISE_SPECIAL_METHODS[byte.arg], {lhs,std::move(rhs)}));
} break;
case OP_COMPARE_OP:
{
@ -175,9 +174,9 @@ private:
PyVar lhs = frame->popValue(this);
// for __ne__ we use the negation of __eq__
int op = byte.arg == 3 ? 2 : byte.arg;
PyVar res = fastCall(lhs, CMP_SPECIAL_METHODS[op], {lhs,rhs});
PyVar res = fastCall(lhs, CMP_SPECIAL_METHODS[op], {lhs,std::move(rhs)});
if(op != byte.arg) res = PyBool(!PyBool_AS_C(res));
frame->push(res);
frame->push(std::move(res));
} break;
case OP_IS_OP:
{
@ -240,9 +239,9 @@ private:
{
PyVarList args = frame->popNValuesReversed(this, byte.arg);
PyVar callable = frame->popValue(this);
PyVar ret = call(callable, args, true);
PyVar ret = call(std::move(callable), std::move(args), true);
if(ret == __py2py_call_signal) return ret;
frame->push(ret);
frame->push(std::move(ret));
} break;
case OP_JUMP_ABSOLUTE: frame->jump(byte.arg); break;
case OP_SAFE_JUMP_ABSOLUTE: frame->safeJump(byte.arg); break;
@ -383,13 +382,13 @@ public:
}
PyVar fastCall(const PyVar& obj, const _Str& name, PyVarList args){
PyVar cls = obj->_type;
while(cls != None) {
PyObject* cls = obj->_type.get();
while(cls != None.get()) {
auto it = cls->attribs.find(name);
if(it != cls->attribs.end()){
return call(it->second, args);
}
cls = cls->attribs[__base__];
cls = cls->attribs[__base__].get();
}
attributeError(obj, name);
return nullptr;
@ -548,8 +547,8 @@ public:
auto it = obj->attribs.find(name);
if(it != obj->attribs.end()) return it->second;
PyVar cls = obj->_type;
while(cls != None) {
PyObject* cls = obj->_type.get();
while(cls != None.get()) {
it = cls->attribs.find(name);
if(it != cls->attribs.end()){
PyVar valueFromCls = it->second;
@ -559,16 +558,20 @@ public:
return valueFromCls;
}
}
cls = cls->attribs[__base__];
cls = cls->attribs[__base__].get();
}
if(throw_err) attributeError(obj, name);
return nullptr;
}
inline void setAttr(PyVar& obj, const _Str& name, PyVar value) {
inline void setAttr(PyVar& obj, const _Str& name, const PyVar& value) {
obj->attribs[name] = value;
}
inline void setAttr(PyVar& obj, const _Str& name, PyVar&& value) {
obj->attribs[name] = std::move(value);
}
void bindMethod(_Str typeName, _Str funcName, _CppFunc fn) {
funcName.intern();
PyVar type = _types[typeName];
@ -906,7 +909,7 @@ void CompoundPointer::del(VM* vm, Frame* frame) const{
/***** Frame's Impl *****/
inline PyVar Frame::__deref_pointer(VM* vm, PyVar v){
if(v->isType(vm->_tp_pointer)) v = vm->PyPointer_AS_C(v)->get(vm, this);
if(v->isType(vm->_tp_pointer)) return vm->PyPointer_AS_C(v)->get(vm, this);
return v;
}

7
test_cpp.sh Normal file
View File

@ -0,0 +1,7 @@
g++ -o pocketpy src/main.cpp --std=c++17 -pg -O1 -pthread
./pocketpy tests/1.py
gprof pocketpy gmon.out > gprof.txt
rm gmon.out

View File

@ -1,8 +0,0 @@
g++ -o pocketpy src/main.cpp --std=c++17 -pg -O1 -pthread -Wno-literal-suffix
./pocketpy tests/1.py
gprof pocketpy gmon.out > gprof.txt
#gprof pocketpy | gprof2dot | dot -Tsvg -o output.svg
rm gmon.out

16
tests/1.py Normal file
View File

@ -0,0 +1,16 @@
def is_prime(x):
if x<2:
return False
for i in range(2,x):
if x%i == 0:
return False
return True
def test(n):
k = 0
for i in range(n):
if is_prime(i):
k += 1
return k
print(test(10000))