From 8e21cd4baf90b1a1aafb53275729a4c829dd9cee Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Thu, 15 Jun 2023 21:46:03 +0800 Subject: [PATCH] ... --- src/str.h | 46 +++++++++++++++++++++++++++------------------- src/tuplelist.h | 4 ++-- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/src/str.h b/src/str.h index 1389d9a3..2eaa3c8d 100644 --- a/src/str.h +++ b/src/str.h @@ -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()(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 { diff --git a/src/tuplelist.h b/src/tuplelist.h index 89e2bca5..687ab3c4 100644 --- a/src/tuplelist.h +++ b/src/tuplelist.h @@ -11,13 +11,13 @@ using List = pod_vector; 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*));