some optim

This commit is contained in:
blueloveTH 2022-11-20 18:09:07 +08:00
parent 08ade52bf1
commit 027473012b
3 changed files with 16 additions and 15 deletions

View File

@ -20,3 +20,9 @@
#include <thread> #include <thread>
#include <atomic> #include <atomic>
#include <iostream> #include <iostream>
#ifdef POCKETPY_H
#define UNREACHABLE() throw std::runtime_error( "L" + std::to_string(__LINE__) + " UNREACHABLE()! This should be a bug, please report it");
#else
#define UNREACHABLE() throw std::runtime_error( __FILE__ + std::string(":") + std::to_string(__LINE__) + " UNREACHABLE()!");
#endif

View File

@ -76,12 +76,6 @@ typedef std::variant<_Int,_Float,bool,_Str,PyVarList,_CppFunc,_Func,std::shared_
const int _SIZEOF_VALUE = sizeof(_Value); const int _SIZEOF_VALUE = sizeof(_Value);
#ifdef POCKETPY_H
#define UNREACHABLE() throw std::runtime_error( "L" + std::to_string(__LINE__) + " UNREACHABLE()! This should be a bug, please report it");
#else
#define UNREACHABLE() throw std::runtime_error( __FILE__ + std::string(":") + std::to_string(__LINE__) + " UNREACHABLE()!");
#endif
struct PyObject { struct PyObject {
PyVarDict attribs; PyVarDict attribs;
_Value _native; _Value _native;

View File

@ -55,21 +55,22 @@ public:
namespace pkpy { namespace pkpy {
const uint16_t MAX_POOLING_N = 16; const uint8_t MAX_POOLING_N = 16;
static std::deque<PyVar*>* _poolArgList = new std::deque<PyVar*>[MAX_POOLING_N]; static std::deque<PyVar*>* _poolArgList = new std::deque<PyVar*>[MAX_POOLING_N];
class ArgList { class ArgList {
PyVar* _args = nullptr; PyVar* _args = nullptr;
uint16_t _size = 0; uint8_t _size = 0;
inline void __checkIndex(uint16_t i) const { inline void __checkIndex(uint8_t i) const {
if (i >= _size){ if (i >= _size){
auto msg = "pkpy:ArgList index out of range, " + std::to_string(i) + " not in [0, " + std::to_string(size()) + ")"; auto msg = "pkpy:ArgList index out of range, " + std::to_string(i) + " not in [0, " + std::to_string(size()) + ")";
throw std::out_of_range(msg); throw std::out_of_range(msg);
} }
} }
void __tryAlloc(uint16_t n){ void __tryAlloc(uint8_t n){
if(n > 255) UNREACHABLE();
if(n >= MAX_POOLING_N || _poolArgList[n].empty()){ if(n >= MAX_POOLING_N || _poolArgList[n].empty()){
this->_size = n; this->_size = n;
this->_args = new PyVar[n]; this->_args = new PyVar[n];
@ -82,7 +83,7 @@ namespace pkpy {
void __tryRelease(){ void __tryRelease(){
if(_size == 0 || _args == nullptr) return; if(_size == 0 || _args == nullptr) return;
if(_size >= MAX_POOLING_N){ if(_size >= MAX_POOLING_N || _poolArgList[_size].size() > 32){
delete[] _args; delete[] _args;
}else{ }else{
for(int i = 0; i < _size; i++) _args[i].reset(); for(int i = 0; i < _size; i++) _args[i].reset();
@ -124,12 +125,12 @@ namespace pkpy {
for(auto& arg: args) this->_args[i++] = arg; for(auto& arg: args) this->_args[i++] = arg;
} }
PyVar& operator[](uint16_t i){ PyVar& operator[](uint8_t i){
__checkIndex(i); __checkIndex(i);
return _args[i]; return _args[i];
} }
const PyVar& operator[](uint16_t i) const { const PyVar& operator[](uint8_t i) const {
__checkIndex(i); __checkIndex(i);
return _args[i]; return _args[i];
} }
@ -146,7 +147,7 @@ namespace pkpy {
return *this; return *this;
} }
uint16_t size() const { uint8_t size() const {
return _size; return _size;
} }