summaryrefslogtreecommitdiff
path: root/src/shared_string.hh
diff options
context:
space:
mode:
authorMaxime Coste <mawww@kakoune.org>2024-02-28 19:00:51 +1100
committerMaxime Coste <mawww@kakoune.org>2024-02-28 19:02:29 +1100
commit3d5a0c672e6f3cf87944b33712e17531aa42c607 (patch)
treed402461b8d6517072b27e653d1201888366718a8 /src/shared_string.hh
parent57b794ede3cae1e7c21309869a2c617481a55acf (diff)
Templatize StringData::create
This improves performance by letting the compiler optimize most use cases where string count and length are known are compile time.
Diffstat (limited to 'src/shared_string.hh')
-rw-r--r--src/shared_string.hh30
1 files changed, 23 insertions, 7 deletions
diff --git a/src/shared_string.hh b/src/shared_string.hh
index 17bb72d8..bb06c7fc 100644
--- a/src/shared_string.hh
+++ b/src/shared_string.hh
@@ -7,6 +7,7 @@
#include "hash_map.hh"
#include <numeric>
+#include <cstring>
namespace Kakoune
{
@@ -32,12 +33,11 @@ private:
static void inc_ref(StringData* r, void*) noexcept { ++r->refcount; }
static void dec_ref(StringData* r, void*) noexcept
{
- if ((--r->refcount & refcount_mask) == 0)
- {
- if (r->refcount & interned_flag)
- Registry::instance().remove(r->strview());
- StringData::operator delete(r, sizeof(StringData) + r->length + 1);
- }
+ if ((--r->refcount & refcount_mask) > 0)
+ return;
+ if (r->refcount & interned_flag)
+ Registry::instance().remove(r->strview());
+ StringData::operator delete(r, sizeof(StringData) + r->length + 1);
}
static void ptr_moved(StringData*, void*, void*) noexcept {}
};
@@ -57,7 +57,23 @@ public:
HashMap<StringView, StringData*, MemoryDomain::SharedString> m_strings;
};
- static Ptr create(ArrayView<const StringView> strs);
+ static Ptr create(ConvertibleTo<StringView> auto&&... strs)
+ {
+ const int len = ((int)StringView{strs}.length() + ...);
+ void* ptr = StringData::operator new(sizeof(StringData) + len + 1);
+ auto* res = new (ptr) StringData(len);
+ auto* data = reinterpret_cast<char*>(res + 1);
+ auto append = [&](StringView str) {
+ if (str.empty()) // memccpy(..., nullptr, 0) is UB
+ return;
+ memcpy(data, str.begin(), (size_t)str.length());
+ data += (int)str.length();
+ };
+ (append(strs), ...);
+ *data = 0;
+ return RefPtr<StringData, PtrPolicy>{res};
+ }
+
};
using StringDataPtr = StringData::Ptr;