From e11702aebc88946947ac4c64aa0ed67102ef9516 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Fri, 30 Jun 2023 00:05:11 +0800 Subject: [PATCH] ... --- src/str.h | 13 ++++++++----- src/vm.h | 4 ++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/str.h b/src/str.h index a39c5b66..b8bb76ec 100644 --- a/src/str.h +++ b/src/str.h @@ -23,6 +23,8 @@ struct Str{ char* data; char _inlined[16]; + const char* _cached_c_str = nullptr; + bool is_inlined() const { return data == _inlined; } Str(): size(0), is_ascii(true), data(_inlined) {} @@ -98,6 +100,7 @@ struct Str{ ~Str(){ if(!is_inlined()) pool64.dealloc(data); + if(_cached_c_str != nullptr) free((void*)_cached_c_str); } Str operator+(const Str& other) const { @@ -201,10 +204,11 @@ struct Str{ return p; } - const char* c_str_temp() const { - static THREAD_LOCAL std::string temp; - temp.assign(data, size); - return temp.c_str(); + const char* c_str(){ + if(_cached_c_str == nullptr){ + _cached_c_str = c_str_dup(); + } + return _cached_c_str; } std::string_view sv() const { @@ -429,7 +433,6 @@ struct FastStrStream{ struct CString{ const char* ptr; CString(const char* ptr): ptr(ptr) {} - CString(const Str& str): ptr(str.c_str_temp()) {} operator const char*() const { return ptr; } }; diff --git a/src/vm.h b/src/vm.h index 17c22303..329d8f86 100644 --- a/src/vm.h +++ b/src/vm.h @@ -824,11 +824,11 @@ template<> inline bool _py_cast(VM* vm, PyObject* obj){ template<> inline CString py_cast(VM* vm, PyObject* obj){ vm->check_non_tagged_type(obj, vm->tp_str); - return PK_OBJ_GET(Str, obj).c_str_temp(); + return PK_OBJ_GET(Str, obj).c_str(); } template<> inline CString _py_cast(VM* vm, PyObject* obj){ - return PK_OBJ_GET(Str, obj).c_str_temp(); + return PK_OBJ_GET(Str, obj).c_str(); } inline PyObject* py_var(VM* vm, const char val[]){