mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 03:20:18 +00:00
optimize _type
This commit is contained in:
parent
367858365a
commit
2e38515110
@ -42,8 +42,6 @@ extern "C" {
|
|||||||
int main(int argc, char** argv){
|
int main(int argc, char** argv){
|
||||||
if(argc == 1){
|
if(argc == 1){
|
||||||
VM* vm = pkpy_new_vm(true);
|
VM* vm = pkpy_new_vm(true);
|
||||||
// for(auto& kv : _strIntern)
|
|
||||||
// std::cout << kv.first << ", ";
|
|
||||||
REPL repl(vm);
|
REPL repl(vm);
|
||||||
while(true){
|
while(true){
|
||||||
std::string line;
|
std::string line;
|
||||||
@ -76,6 +74,9 @@ int main(int argc, char** argv){
|
|||||||
vm->exec(code);
|
vm->exec(code);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// for(auto& kv : _strIntern)
|
||||||
|
// std::cout << kv.first << ", ";
|
||||||
|
|
||||||
// Timer("Running time").run([=]{
|
// Timer("Running time").run([=]{
|
||||||
// vm->startExec(code);
|
// vm->startExec(code);
|
||||||
// while(pkpy_tvm_get_state(vm) != THREAD_FINISHED){
|
// while(pkpy_tvm_get_state(vm) != THREAD_FINISHED){
|
||||||
|
10
src/obj.h
10
src/obj.h
@ -84,9 +84,15 @@ const int _SIZEOF_VALUE = sizeof(_Value);
|
|||||||
struct PyObject {
|
struct PyObject {
|
||||||
PyVarDict attribs;
|
PyVarDict attribs;
|
||||||
_Value _native;
|
_Value _native;
|
||||||
|
PyVar _type;
|
||||||
|
|
||||||
inline bool isType(const PyVar& type){
|
inline bool isType(const PyVar& type){
|
||||||
return attribs[__class__] == type;
|
return this->_type == type;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void setType(const PyVar& type){
|
||||||
|
this->_type = type;
|
||||||
|
this->attribs[__class__] = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
// currently __name__ is only used for 'type'
|
// currently __name__ is only used for 'type'
|
||||||
@ -96,7 +102,7 @@ struct PyObject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_Str getTypeName(){
|
_Str getTypeName(){
|
||||||
return attribs[__class__]->getName();
|
return _type->getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
PyObject(_Value val): _native(val) {}
|
PyObject(_Value val): _native(val) {}
|
||||||
|
@ -126,7 +126,7 @@ void __initializeBuiltinFunctions(VM* _vm) {
|
|||||||
|
|
||||||
_vm->bindMethod("type", "__new__", [](VM* vm, PyVarList args) {
|
_vm->bindMethod("type", "__new__", [](VM* vm, PyVarList args) {
|
||||||
vm->__checkArgSize(args, 1);
|
vm->__checkArgSize(args, 1);
|
||||||
return args[0]->attribs[__class__];
|
return args[0]->_type;
|
||||||
});
|
});
|
||||||
|
|
||||||
_vm->bindMethod("range", "__new__", [](VM* vm, PyVarList args) {
|
_vm->bindMethod("range", "__new__", [](VM* vm, PyVarList args) {
|
||||||
|
11
src/str.h
11
src/str.h
@ -99,20 +99,17 @@ public:
|
|||||||
|
|
||||||
void construct(std::string s){
|
void construct(std::string s){
|
||||||
auto it = _strIntern.find(s);
|
auto it = _strIntern.find(s);
|
||||||
if(it == _strIntern.end()){
|
if(it != _strIntern.end()){
|
||||||
this->_s = std::make_shared<_StrMemory>(s);
|
|
||||||
if(s.size() <= 2){
|
|
||||||
_strIntern[s] = this->_s;
|
|
||||||
interned = true;
|
|
||||||
}
|
|
||||||
}else{
|
|
||||||
this->_s = it->second;
|
this->_s = it->second;
|
||||||
interned = true;
|
interned = true;
|
||||||
|
}else{
|
||||||
|
this->_s = std::make_shared<_StrMemory>(std::move(s));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// force the string to be interned
|
// force the string to be interned
|
||||||
void intern(){
|
void intern(){
|
||||||
|
if(interned) return;
|
||||||
auto it = _strIntern.find(*this->_s);
|
auto it = _strIntern.find(*this->_s);
|
||||||
if(it == _strIntern.end()) _strIntern[*this->_s] = this->_s;
|
if(it == _strIntern.end()) _strIntern[*this->_s] = this->_s;
|
||||||
else this->_s = it->second;
|
else this->_s = it->second;
|
||||||
|
24
src/vm.h
24
src/vm.h
@ -30,7 +30,7 @@
|
|||||||
}else{ \
|
}else{ \
|
||||||
__checkType(ptype, _tp_type); \
|
__checkType(ptype, _tp_type); \
|
||||||
_raw = new PyObject(_native); \
|
_raw = new PyObject(_native); \
|
||||||
_raw->attribs[__class__] = ptype; \
|
_raw->setType(ptype); \
|
||||||
} \
|
} \
|
||||||
PyVar obj = PyVar(_raw, [this](PyObject* p){\
|
PyVar obj = PyVar(_raw, [this](PyObject* p){\
|
||||||
if(_pool##name.size() < max_size){ \
|
if(_pool##name.size() < max_size){ \
|
||||||
@ -371,10 +371,9 @@ public:
|
|||||||
|
|
||||||
PyVar asBool(const PyVar& obj){
|
PyVar asBool(const PyVar& obj){
|
||||||
if(obj == None) return False;
|
if(obj == None) return False;
|
||||||
PyVar tp = obj->attribs[__class__];
|
if(obj->_type == _tp_bool) return obj;
|
||||||
if(tp == _tp_bool) return obj;
|
if(obj->_type == _tp_int) return PyBool(PyInt_AS_C(obj) != 0);
|
||||||
if(tp == _tp_int) return PyBool(PyInt_AS_C(obj) != 0);
|
if(obj->_type == _tp_float) return PyBool(PyFloat_AS_C(obj) != 0.0);
|
||||||
if(tp == _tp_float) return PyBool(PyFloat_AS_C(obj) != 0.0);
|
|
||||||
PyVarOrNull len_fn = getAttr(obj, __len__, false);
|
PyVarOrNull len_fn = getAttr(obj, __len__, false);
|
||||||
if(len_fn != nullptr){
|
if(len_fn != nullptr){
|
||||||
PyVar ret = call(len_fn, {});
|
PyVar ret = call(len_fn, {});
|
||||||
@ -384,7 +383,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
PyVar fastCall(const PyVar& obj, const _Str& name, PyVarList args){
|
PyVar fastCall(const PyVar& obj, const _Str& name, PyVarList args){
|
||||||
PyVar cls = obj->attribs[__class__];
|
PyVar cls = obj->_type;
|
||||||
while(cls != None) {
|
while(cls != None) {
|
||||||
auto it = cls->attribs.find(name);
|
auto it = cls->attribs.find(name);
|
||||||
if(it != cls->attribs.end()){
|
if(it != cls->attribs.end()){
|
||||||
@ -414,6 +413,7 @@ public:
|
|||||||
|
|
||||||
if(callable->isType(_tp_bounded_method)){
|
if(callable->isType(_tp_bounded_method)){
|
||||||
auto& bm = PyBoundedMethod_AS_C(callable);
|
auto& bm = PyBoundedMethod_AS_C(callable);
|
||||||
|
// TODO: avoid insertion here, bad performance
|
||||||
args.insert(args.begin(), bm.obj);
|
args.insert(args.begin(), bm.obj);
|
||||||
callable = bm.method;
|
callable = bm.method;
|
||||||
}
|
}
|
||||||
@ -524,7 +524,7 @@ public:
|
|||||||
PyVar newClassType(_Str name, PyVar base=nullptr) {
|
PyVar newClassType(_Str name, PyVar base=nullptr) {
|
||||||
if(base == nullptr) base = _tp_object;
|
if(base == nullptr) base = _tp_object;
|
||||||
PyVar obj = std::make_shared<PyObject>((_Int)0);
|
PyVar obj = std::make_shared<PyObject>((_Int)0);
|
||||||
setAttr(obj, __class__, _tp_type);
|
obj->setType(_tp_type);
|
||||||
setAttr(obj, __base__, base);
|
setAttr(obj, __base__, base);
|
||||||
_types[name] = obj;
|
_types[name] = obj;
|
||||||
return obj;
|
return obj;
|
||||||
@ -533,7 +533,7 @@ public:
|
|||||||
PyVar newObject(PyVar type, _Value _native) {
|
PyVar newObject(PyVar type, _Value _native) {
|
||||||
__checkType(type, _tp_type);
|
__checkType(type, _tp_type);
|
||||||
PyVar obj = std::make_shared<PyObject>(_native);
|
PyVar obj = std::make_shared<PyObject>(_native);
|
||||||
setAttr(obj, __class__, type);
|
obj->setType(type);
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -548,7 +548,7 @@ public:
|
|||||||
auto it = obj->attribs.find(name);
|
auto it = obj->attribs.find(name);
|
||||||
if(it != obj->attribs.end()) return it->second;
|
if(it != obj->attribs.end()) return it->second;
|
||||||
|
|
||||||
PyVar cls = obj->attribs[__class__];
|
PyVar cls = obj->_type;
|
||||||
while(cls != None) {
|
while(cls != None) {
|
||||||
it = cls->attribs.find(name);
|
it = cls->attribs.find(name);
|
||||||
if(it != cls->attribs.end()){
|
if(it != cls->attribs.end()){
|
||||||
@ -595,7 +595,7 @@ public:
|
|||||||
|
|
||||||
bool isInstance(PyVar obj, PyVar type){
|
bool isInstance(PyVar obj, PyVar type){
|
||||||
__checkType(type, _tp_type);
|
__checkType(type, _tp_type);
|
||||||
PyVar t = obj->attribs[__class__];
|
PyVar t = obj->_type;
|
||||||
while (t != None){
|
while (t != None){
|
||||||
if (t == type) return true;
|
if (t == type) return true;
|
||||||
t = t->attribs[__base__];
|
t = t->attribs[__base__];
|
||||||
@ -696,9 +696,9 @@ public:
|
|||||||
this->_main = newModule("__main__"c, false);
|
this->_main = newModule("__main__"c, false);
|
||||||
|
|
||||||
setAttr(_tp_type, __base__, _tp_object);
|
setAttr(_tp_type, __base__, _tp_object);
|
||||||
setAttr(_tp_type, __class__, _tp_type);
|
_tp_type->setType(_tp_type);
|
||||||
setAttr(_tp_object, __base__, None);
|
setAttr(_tp_object, __base__, None);
|
||||||
setAttr(_tp_object, __class__, _tp_type);
|
_tp_object->setType(_tp_type);
|
||||||
|
|
||||||
for (auto& [name, type] : _types) {
|
for (auto& [name, type] : _types) {
|
||||||
setAttr(type, __name__, PyStr(name));
|
setAttr(type, __name__, PyStr(name));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user