This commit is contained in:
blueloveTH 2023-06-15 21:46:03 +08:00
parent ba248ae0f3
commit 8e21cd4baf
2 changed files with 29 additions and 21 deletions

View File

@ -21,15 +21,26 @@ struct Str{
int size;
bool is_ascii;
char* data;
char _inlined[16];
Str(): size(0), is_ascii(true), data(nullptr) {}
bool is_inlined() const { return data == _inlined; }
Str(): size(0), is_ascii(true), data(_inlined) {}
void _alloc(){
if(size <= 16){
this->data = _inlined;
}else{
this->data = (char*)pool64.alloc(size);
}
}
Str(int size, bool is_ascii): size(size), is_ascii(is_ascii) {
data = (char*)pool64.alloc(size);
_alloc();
}
#define STR_INIT() \
data = (char*)pool64.alloc(size); \
_alloc(); \
for(int i=0; i<size; i++){ \
data[i] = s[i]; \
if(!isascii(s[i])) is_ascii = false; \
@ -54,14 +65,20 @@ struct Str{
#undef STR_INIT
Str(const Str& other): size(other.size), is_ascii(other.is_ascii) {
data = (char*)pool64.alloc(size);
_alloc();
memcpy(data, other.data, size);
}
Str(Str&& other): size(other.size), is_ascii(other.is_ascii), data(other.data) {
other.data = nullptr;
Str(Str&& other): size(other.size), is_ascii(other.is_ascii) {
if(other.is_inlined()){
data = _inlined;
for(int i=0; i<size; i++) _inlined[i] = other._inlined[i];
}else{
data = other.data;
other.data = other._inlined;
other.size = 0;
}
}
const char* begin() const { return data; }
const char* end() const { return data + size; }
@ -71,25 +88,16 @@ struct Str{
size_t hash() const{ return std::hash<std::string_view>()(sv()); }
Str& operator=(const Str& other){
if(data!=nullptr) pool64.dealloc(data);
if(!is_inlined()) pool64.dealloc(data);
size = other.size;
is_ascii = other.is_ascii;
data = (char*)pool64.alloc(size);
_alloc();
memcpy(data, other.data, size);
return *this;
}
Str& operator=(Str&& other) noexcept{
if(data!=nullptr) pool64.dealloc(data);
size = other.size;
is_ascii = other.is_ascii;
data = other.data;
other.data = nullptr;
return *this;
}
~Str(){
if(data!=nullptr) pool64.dealloc(data);
if(!is_inlined()) pool64.dealloc(data);
}
Str operator+(const Str& other) const {

View File

@ -11,13 +11,13 @@ using List = pod_vector<PyObject*>;
class Tuple {
PyObject** _args;
PyObject* _inlined[2];
PyObject* _inlined[3];
int _size;
bool is_inlined() const { return _args == _inlined; }
void _alloc(int n){
if(n <= 2){
if(n <= 3){
this->_args = _inlined;
}else{
this->_args = (PyObject**)pool64.alloc(n * sizeof(void*));