From a5cea1e0a66b3208f42d11da0dad9a92c93a4e31 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sat, 12 Nov 2022 21:07:53 +0800 Subject: [PATCH] add pool optim --- src/obj.h | 6 +++--- src/str.h | 3 ++- src/vm.h | 53 +++++++++++++++++++++++++++++------------------------ 3 files changed, 34 insertions(+), 28 deletions(-) diff --git a/src/obj.h b/src/obj.h index 6589fba0..2102b0fb 100644 --- a/src/obj.h +++ b/src/obj.h @@ -66,7 +66,7 @@ struct Function { } }; -struct BoundedMethod { +struct _BoundedMethod { PyVar obj; PyVar method; }; @@ -101,7 +101,7 @@ public: }; typedef std::shared_ptr _Func; -typedef std::variant<_Int,_Float,bool,_Str,PyVarList,_CppFunc,_Func,std::shared_ptr<_Iterator>,BoundedMethod,_Range,_Slice,_Pointer> _Value; +typedef std::variant<_Int,_Float,bool,_Str,PyVarList,_CppFunc,_Func,std::shared_ptr<_Iterator>,_BoundedMethod,_Range,_Slice,_Pointer> _Value; const int _SIZEOF_VALUE = sizeof(_Value); @@ -126,4 +126,4 @@ struct PyObject { } PyObject(_Value val): _native(val) {} -}; \ No newline at end of file +}; diff --git a/src/str.h b/src/str.h index aef6ba04..94322c21 100644 --- a/src/str.h +++ b/src/str.h @@ -7,7 +7,7 @@ typedef std::stringstream _StrStream; class _Str { private: mutable bool utf8_initialized = false; - mutable std::vector _u8_index; // max_len is 65535 + mutable std::vector _u8_index; std::string _s; @@ -16,6 +16,7 @@ private: void utf8_lazy_init() const{ if(utf8_initialized) return; + if(size() > 65535) throw std::runtime_error("String has more than 65535 bytes."); for(uint16_t i = 0; i < size(); i++){ // https://stackoverflow.com/questions/3911536/utf-8-unicode-whats-with-0xc0-and-0x80 if((_s[i] & 0xC0) != 0x80) diff --git a/src/vm.h b/src/vm.h index 08f6f98b..37769daf 100644 --- a/src/vm.h +++ b/src/vm.h @@ -19,14 +19,34 @@ __DEF_PY(type, ctype, ptype) \ __DEF_PY_AS_C(type, ctype, ptype) -typedef void(*PrintFn)(const char*); +#define __DEF_PY_POOL(name, ctype, ptype, max_size) \ + std::vector _pool##name; \ + PyVar Py##name(ctype _native) { \ + PyObject* _raw = nullptr; \ + if(_pool##name.size() > 0) { \ + _raw = _pool##name.back(); \ + _raw->_native = _native; \ + _pool##name.pop_back(); \ + }else{ \ + __checkType(ptype, _tp_type); \ + _raw = new PyObject(_native); \ + _raw->attribs[__class__] = ptype; \ + } \ + PyVar obj = PyVar(_raw, [this](PyObject* p){\ + if(_pool##name.size() < max_size){ \ + _pool##name.push_back(p); \ + }else{ \ + delete p; \ + } \ + }); \ + return obj; \ + } -#define NUM_POOL_MAX_SIZE 1024 +typedef void(*PrintFn)(const char*); class VM{ private: std::stack< std::unique_ptr > callstack; - std::vector numPool; PyVarDict _modules; // 3rd modules PyVar __py2py_call_signal; @@ -488,23 +508,6 @@ public: return obj; } - PyVar newNumber(PyVar type, _Value _native) { - if(type != _tp_int && type != _tp_float) UNREACHABLE(); - PyObject* _raw = nullptr; - if(numPool.size() > 0) { - _raw = numPool.back(); - _raw->_native = _native; - numPool.pop_back(); - }else{ - _raw = new PyObject(_native); - } - PyVar obj = PyVar(_raw, [this](PyObject* p){ - if(numPool.size() < NUM_POOL_MAX_SIZE) numPool.push_back(p); - }); - setAttr(obj, __class__, type); - return obj; - } - PyVar newModule(_Str name, bool saveToPath=true) { PyVar obj = newObject(_tp_module, (_Int)-2); setAttr(obj, "__name__", PyStr(name)); @@ -610,21 +613,23 @@ public: PyVar _tp_function, _tp_native_function, _tp_native_iterator, _tp_bounded_method; PyVar _tp_slice, _tp_range, _tp_module, _tp_pointer; + __DEF_PY_POOL(Int, _Int, _tp_int, 256); __DEF_PY_AS_C(Int, _Int, _tp_int) + __DEF_PY_POOL(Float, _Float, _tp_float, 256); __DEF_PY_AS_C(Float, _Float, _tp_float) + __DEF_PY_POOL(Pointer, _Pointer, _tp_pointer, 512) + __DEF_PY_AS_C(Pointer, _Pointer, _tp_pointer) + DEF_NATIVE(Str, _Str, _tp_str) DEF_NATIVE(List, PyVarList, _tp_list) DEF_NATIVE(Tuple, PyVarList, _tp_tuple) DEF_NATIVE(Function, _Func, _tp_function) DEF_NATIVE(NativeFunction, _CppFunc, _tp_native_function) DEF_NATIVE(Iter, std::shared_ptr<_Iterator>, _tp_native_iterator) - DEF_NATIVE(BoundedMethod, BoundedMethod, _tp_bounded_method) + DEF_NATIVE(BoundedMethod, _BoundedMethod, _tp_bounded_method) DEF_NATIVE(Range, _Range, _tp_range) DEF_NATIVE(Slice, _Slice, _tp_slice) - DEF_NATIVE(Pointer, _Pointer, _tp_pointer) - inline PyVar PyInt(_Int i) { return newNumber(_tp_int, i); } - inline PyVar PyFloat(_Float f) { return newNumber(_tp_float, f); } inline bool PyBool_AS_C(PyVar obj){return obj == True;} inline PyVar PyBool(bool value){return value ? True : False;}