blueloveTH 2 年 前
コミット
8e21cd4baf
2 ファイル変更29 行追加21 行削除
  1. 27 19
      src/str.h
  2. 2 2
      src/tuplelist.h

+ 27 - 19
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<size; i++){                  \
             data[i] = s[i];                         \
             if(!isascii(s[i])) is_ascii = false;    \
@@ -54,13 +65,19 @@ 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;
-        other.size = 0;
+    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; }
@@ -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 {

+ 2 - 2
src/tuplelist.h

@@ -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*));