This commit is contained in:
BLUELOVETH 2023-04-06 17:21:22 +00:00
parent 12419105bd
commit 4e63588589
5 changed files with 46 additions and 20 deletions

View File

@ -38,7 +38,7 @@
#define DEBUG_CEVAL_STEP 0 #define DEBUG_CEVAL_STEP 0
#define DEBUG_FULL_EXCEPTION 0 #define DEBUG_FULL_EXCEPTION 0
#define DEBUG_NO_AUTO_GC 1 #define DEBUG_NO_AUTO_GC 1
#define DEBUG_GC_STATS 1 #define DEBUG_GC_STATS 0
#if (defined(__ANDROID__) && __ANDROID_API__ <= 22) || defined(__EMSCRIPTEN__) #if (defined(__ANDROID__) && __ANDROID_API__ <= 22) || defined(__EMSCRIPTEN__)
#define PK_ENABLE_FILEIO 0 #define PK_ENABLE_FILEIO 0

View File

@ -429,7 +429,7 @@ inline void init_builtins(VM* _vm) {
List& self = CAST(List&, args[0]); List& self = CAST(List&, args[0]);
PyObject* obj = vm->asList(args[1]); PyObject* obj = vm->asList(args[1]);
const List& list = CAST(List&, obj); const List& list = CAST(List&, obj);
self.insert(self.end(), list.begin(), list.end()); self.extend(list);
return vm->None; return vm->None;
}); });
@ -444,7 +444,7 @@ inline void init_builtins(VM* _vm) {
int n = CAST(int, args[1]); int n = CAST(int, args[1]);
List result; List result;
result.reserve(self.size() * n); result.reserve(self.size() * n);
for(int i = 0; i < n; i++) result.insert(result.end(), self.begin(), self.end()); for(int i = 0; i < n; i++) result.extend(self);
return VAR(std::move(result)); return VAR(std::move(result));
}); });
@ -454,7 +454,7 @@ inline void init_builtins(VM* _vm) {
if(index < 0) index += self.size(); if(index < 0) index += self.size();
if(index < 0) index = 0; if(index < 0) index = 0;
if(index > self.size()) index = self.size(); if(index > self.size()) index = self.size();
self.insert(self.begin() + index, args[2]); self.insert(index, args[2]);
return vm->None; return vm->None;
}); });
@ -467,10 +467,10 @@ inline void init_builtins(VM* _vm) {
_vm->bind_method<1>("list", "__add__", [](VM* vm, Args& args) { _vm->bind_method<1>("list", "__add__", [](VM* vm, Args& args) {
const List& self = CAST(List&, args[0]); const List& self = CAST(List&, args[0]);
const List& obj = CAST(List&, args[1]); const List& other = CAST(List&, args[1]);
List new_list = self; List new_list(self); // copy construct
new_list.insert(new_list.end(), obj.begin(), obj.end()); new_list.extend(other);
return VAR(new_list); return VAR(std::move(new_list));
}); });
_vm->bind_method<0>("list", "__len__", [](VM* vm, Args& args) { _vm->bind_method<0>("list", "__len__", [](VM* vm, Args& args) {
@ -510,7 +510,7 @@ inline void init_builtins(VM* _vm) {
List& self = CAST(List&, args[0]); List& self = CAST(List&, args[0]);
int index = CAST(int, args[1]); int index = CAST(int, args[1]);
index = vm->normalized_index(index, self.size()); index = vm->normalized_index(index, self.size());
self.erase(self.begin() + index); self.erase(index);
return vm->None; return vm->None;
}); });

View File

@ -6,7 +6,7 @@
namespace pkpy { namespace pkpy {
using List = std::vector<PyObject*>; using List = small_vector<PyObject*, 4>;
class Args { class Args {
inline static THREAD_LOCAL FreeListA<PyObject*, 10, false> _pool; inline static THREAD_LOCAL FreeListA<PyObject*, 10, false> _pool;
@ -60,6 +60,7 @@ public:
List to_list() noexcept { List to_list() noexcept {
List ret(_size); List ret(_size);
// TODO: use move/memcpy
for(int i=0; i<_size; i++) ret[i] = _args[i]; for(int i=0; i<_size; i++) ret[i] = _args[i];
return ret; return ret;
} }

View File

@ -16,6 +16,11 @@ struct small_vector{
_data = _buffer; _data = _buffer;
} }
small_vector(int size): _size(size), _capacity(N){
_data = _buffer;
reserve(size);
}
small_vector(const small_vector& other): _size(other._size), _capacity(other._capacity) { small_vector(const small_vector& other): _size(other._size), _capacity(other._capacity) {
if(other.is_small()){ if(other.is_small()){
_data = _buffer; _data = _buffer;
@ -57,19 +62,25 @@ struct small_vector{
template<typename __ValueT> template<typename __ValueT>
void push_back(__ValueT&& t) { void push_back(__ValueT&& t) {
if (_size == _capacity) { if (_size == _capacity) reserve(_capacity*2);
_capacity *= 2;
if (is_small()) {
_data = (T*)malloc(sizeof(T) * _capacity);
memcpy(_data, _buffer, sizeof(T) * _size);
} else {
_data = (T*)realloc(_data, sizeof(T) * _capacity);
}
}
_data[_size++] = std::forward<__ValueT>(t); _data[_size++] = std::forward<__ValueT>(t);
} }
void reserve(int cap){
if(cap < _capacity) return;
_capacity = cap;
if (is_small()) {
_data = (T*)malloc(sizeof(T) * _capacity);
memcpy(_data, _buffer, sizeof(T) * _size);
} else {
_data = (T*)realloc(_data, sizeof(T) * _capacity);
}
}
void pop_back() { _size--; } void pop_back() { _size--; }
void extend(const small_vector& other){
for(int i=0; i<other.size(); i++) push_back(other[i]);
}
T& operator[](int index) { return _data[index]; } T& operator[](int index) { return _data[index]; }
const T& operator[](int index) const { return _data[index]; } const T& operator[](int index) const { return _data[index]; }
@ -87,6 +98,20 @@ struct small_vector{
const T* data() const { return _data; } const T* data() const { return _data; }
bool is_small() const { return _data == _buffer; } bool is_small() const { return _data == _buffer; }
void pop_back_n(int n) { _size -= n; } void pop_back_n(int n) { _size -= n; }
void clear() { _size=0; }
template<typename __ValueT>
void insert(int i, __ValueT&& val){
if (_size == _capacity) reserve(_capacity*2);
for(int j=_size; j>i; j--) _data[j] = _data[j-1];
_data[i] = std::forward<__ValueT>(val);
_size++;
}
void erase(int i){
for(int j=i; j<_size-1; j++) _data[j] = _data[j+1];
_size--;
}
~small_vector() { ~small_vector() {
if (!is_small()) free(_data); if (!is_small()) free(_data);

View File

@ -780,7 +780,7 @@ inline void VM::unpack_args(Args& args){
if(is_type(args[i], tp_star_wrapper)){ if(is_type(args[i], tp_star_wrapper)){
auto& star = _CAST(StarWrapper&, args[i]); auto& star = _CAST(StarWrapper&, args[i]);
List& list = CAST(List&, asList(star.obj)); List& list = CAST(List&, asList(star.obj));
unpacked.insert(unpacked.end(), list.begin(), list.end()); unpacked.extend(list);
}else{ }else{
unpacked.push_back(args[i]); unpacked.push_back(args[i]);
} }