From 2e38515110f49f95ba1e967da70f4075fe3fa1d0 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sat, 19 Nov 2022 17:05:39 +0800 Subject: [PATCH] optimize _type --- src/main.cpp | 5 +++-- src/obj.h | 10 ++++++++-- src/pocketpy.h | 2 +- src/str.h | 11 ++++------- src/vm.h | 24 ++++++++++++------------ 5 files changed, 28 insertions(+), 24 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index fbed0bb6..286e869e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -42,8 +42,6 @@ extern "C" { int main(int argc, char** argv){ if(argc == 1){ VM* vm = pkpy_new_vm(true); - // for(auto& kv : _strIntern) - // std::cout << kv.first << ", "; REPL repl(vm); while(true){ std::string line; @@ -76,6 +74,9 @@ int main(int argc, char** argv){ vm->exec(code); }); + // for(auto& kv : _strIntern) + // std::cout << kv.first << ", "; + // Timer("Running time").run([=]{ // vm->startExec(code); // while(pkpy_tvm_get_state(vm) != THREAD_FINISHED){ diff --git a/src/obj.h b/src/obj.h index 3827a75e..b2396844 100644 --- a/src/obj.h +++ b/src/obj.h @@ -84,9 +84,15 @@ const int _SIZEOF_VALUE = sizeof(_Value); struct PyObject { PyVarDict attribs; _Value _native; + 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' @@ -96,7 +102,7 @@ struct PyObject { } _Str getTypeName(){ - return attribs[__class__]->getName(); + return _type->getName(); } PyObject(_Value val): _native(val) {} diff --git a/src/pocketpy.h b/src/pocketpy.h index 22cb3ca7..d0d7ab2d 100644 --- a/src/pocketpy.h +++ b/src/pocketpy.h @@ -126,7 +126,7 @@ void __initializeBuiltinFunctions(VM* _vm) { _vm->bindMethod("type", "__new__", [](VM* vm, PyVarList args) { vm->__checkArgSize(args, 1); - return args[0]->attribs[__class__]; + return args[0]->_type; }); _vm->bindMethod("range", "__new__", [](VM* vm, PyVarList args) { diff --git a/src/str.h b/src/str.h index 92f7e928..d93e3650 100644 --- a/src/str.h +++ b/src/str.h @@ -99,20 +99,17 @@ public: void construct(std::string s){ auto it = _strIntern.find(s); - if(it == _strIntern.end()){ - this->_s = std::make_shared<_StrMemory>(s); - if(s.size() <= 2){ - _strIntern[s] = this->_s; - interned = true; - } - }else{ + if(it != _strIntern.end()){ this->_s = it->second; interned = true; + }else{ + this->_s = std::make_shared<_StrMemory>(std::move(s)); } } // force the string to be interned void intern(){ + if(interned) return; auto it = _strIntern.find(*this->_s); if(it == _strIntern.end()) _strIntern[*this->_s] = this->_s; else this->_s = it->second; diff --git a/src/vm.h b/src/vm.h index 4290703c..e0ba3a14 100644 --- a/src/vm.h +++ b/src/vm.h @@ -30,7 +30,7 @@ }else{ \ __checkType(ptype, _tp_type); \ _raw = new PyObject(_native); \ - _raw->attribs[__class__] = ptype; \ + _raw->setType(ptype); \ } \ PyVar obj = PyVar(_raw, [this](PyObject* p){\ if(_pool##name.size() < max_size){ \ @@ -371,10 +371,9 @@ public: PyVar asBool(const PyVar& obj){ if(obj == None) return False; - PyVar tp = obj->attribs[__class__]; - if(tp == _tp_bool) return obj; - if(tp == _tp_int) return PyBool(PyInt_AS_C(obj) != 0); - if(tp == _tp_float) return PyBool(PyFloat_AS_C(obj) != 0.0); + if(obj->_type == _tp_bool) return obj; + if(obj->_type == _tp_int) return PyBool(PyInt_AS_C(obj) != 0); + if(obj->_type == _tp_float) return PyBool(PyFloat_AS_C(obj) != 0.0); PyVarOrNull len_fn = getAttr(obj, __len__, false); if(len_fn != nullptr){ PyVar ret = call(len_fn, {}); @@ -384,7 +383,7 @@ public: } PyVar fastCall(const PyVar& obj, const _Str& name, PyVarList args){ - PyVar cls = obj->attribs[__class__]; + PyVar cls = obj->_type; while(cls != None) { auto it = cls->attribs.find(name); if(it != cls->attribs.end()){ @@ -414,6 +413,7 @@ public: if(callable->isType(_tp_bounded_method)){ auto& bm = PyBoundedMethod_AS_C(callable); + // TODO: avoid insertion here, bad performance args.insert(args.begin(), bm.obj); callable = bm.method; } @@ -524,7 +524,7 @@ public: PyVar newClassType(_Str name, PyVar base=nullptr) { if(base == nullptr) base = _tp_object; PyVar obj = std::make_shared((_Int)0); - setAttr(obj, __class__, _tp_type); + obj->setType(_tp_type); setAttr(obj, __base__, base); _types[name] = obj; return obj; @@ -533,7 +533,7 @@ public: PyVar newObject(PyVar type, _Value _native) { __checkType(type, _tp_type); PyVar obj = std::make_shared(_native); - setAttr(obj, __class__, type); + obj->setType(type); return obj; } @@ -548,7 +548,7 @@ public: auto it = obj->attribs.find(name); if(it != obj->attribs.end()) return it->second; - PyVar cls = obj->attribs[__class__]; + PyVar cls = obj->_type; while(cls != None) { it = cls->attribs.find(name); if(it != cls->attribs.end()){ @@ -595,7 +595,7 @@ public: bool isInstance(PyVar obj, PyVar type){ __checkType(type, _tp_type); - PyVar t = obj->attribs[__class__]; + PyVar t = obj->_type; while (t != None){ if (t == type) return true; t = t->attribs[__base__]; @@ -696,9 +696,9 @@ public: this->_main = newModule("__main__"c, false); 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, __class__, _tp_type); + _tp_object->setType(_tp_type); for (auto& [name, type] : _types) { setAttr(type, __name__, PyStr(name));