This commit is contained in:
blueloveTH 2023-01-28 03:46:28 +08:00
parent 5c64b0da81
commit ac332d8122
2 changed files with 26 additions and 17 deletions

View File

@ -40,14 +40,19 @@ namespace pkpy {
static thread_local std::vector<PyVar*>* _poolArgList = new std::vector<PyVar*>[MAX_POOLING_N]; static thread_local std::vector<PyVar*>* _poolArgList = new std::vector<PyVar*>[MAX_POOLING_N];
class ArgList { class ArgList {
PyVar* _args = nullptr; PyVar* _args;
uint8_t _size = 0; uint8_t _size;
void __tryAlloc(size_t n){ void __tryAlloc(size_t n){
if(n == 0){
this->_args = nullptr;
this->_size = 0;
return;
}
if(n > 255) UNREACHABLE(); if(n > 255) UNREACHABLE();
if(n >= MAX_POOLING_N || _poolArgList[n].empty()){ if(n >= MAX_POOLING_N || _poolArgList[n].empty()){
this->_size = n;
this->_args = new PyVar[n]; this->_args = new PyVar[n];
this->_size = n;
}else{ }else{
this->_args = _poolArgList[n].back(); this->_args = _poolArgList[n].back();
this->_size = n; this->_size = n;
@ -67,7 +72,7 @@ namespace pkpy {
public: public:
ArgList(size_t n){ ArgList(size_t n){
if(n != 0) __tryAlloc(n); __tryAlloc(n);
} }
ArgList(const ArgList& other){ ArgList(const ArgList& other){
@ -128,13 +133,23 @@ namespace pkpy {
return ret; return ret;
} }
ArgList move_extended_self(const PyVar& self){ void extended_self(const PyVar& self){
static_assert(std::is_standard_layout_v<PyVar>); static_assert(std::is_standard_layout_v<PyVar>);
pkpy::ArgList ret(size()+1); PyVar* old_args = _args;
ret[0] = self; uint8_t old_size = _size;
memcpy(ret._args+1, _args, sizeof(PyVar)*size()); __tryAlloc(old_size+1);
memset(_args, 0, sizeof(PyVar)*size()); _args[0] = self;
return ret;
if(old_size == 0) return;
memcpy(_args+1, old_args, sizeof(PyVar)*old_size);
memset(old_args, 0, sizeof(PyVar)*old_size);
if(old_size >= MAX_POOLING_N || _poolArgList[old_size].size() > 32){
delete[] old_args;
}else{
_poolArgList[old_size].push_back(old_args);
}
} }
~ArgList(){ ~ArgList(){

View File

@ -450,13 +450,7 @@ public:
if((*callable)->is_type(_tp_bounded_method)){ if((*callable)->is_type(_tp_bounded_method)){
auto& bm = PyBoundedMethod_AS_C((*callable)); auto& bm = PyBoundedMethod_AS_C((*callable));
callable = &bm.method; // get unbound method callable = &bm.method; // get unbound method
args.extended_self(bm.obj);
// TODO: avoid insertion here, bad performance
// pkpy::ArgList new_args(args.size()+1);
// new_args[0] = bm.obj;
// for(int i=0; i<args.size(); i++) new_args[i+1] = args[i];
// args = std::move(new_args);
args = args.move_extended_self(bm.obj);
} }
if((*callable)->is_type(_tp_native_function)){ if((*callable)->is_type(_tp_native_function)){