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){
|
||||
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){
|
||||
|
10
src/obj.h
10
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) {}
|
||||
|
@ -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) {
|
||||
|
11
src/str.h
11
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;
|
||||
|
24
src/vm.h
24
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<PyObject>((_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<PyObject>(_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));
|
||||
|
Loading…
x
Reference in New Issue
Block a user