2015-01-15 14:54:38 +01:00
|
|
|
#include "shared_string.hh"
|
2015-06-06 12:54:48 +02:00
|
|
|
#include "buffer_utils.hh"
|
2015-01-15 14:54:38 +01:00
|
|
|
|
2017-10-09 16:12:42 +02:00
|
|
|
#include <cstring>
|
|
|
|
|
2015-01-15 14:54:38 +01:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2017-02-23 00:56:52 +01:00
|
|
|
StringDataPtr StringData::create(ArrayView<const StringView> strs)
|
2015-01-15 14:54:38 +01:00
|
|
|
{
|
2017-06-07 14:36:47 +02:00
|
|
|
const int len = accumulate(strs, 0, [](int l, StringView s) {
|
|
|
|
return l + (int)s.length();
|
|
|
|
});
|
2017-02-23 00:56:52 +01:00
|
|
|
void* ptr = StringData::operator new(sizeof(StringData) + len + 1);
|
|
|
|
auto* res = new (ptr) StringData(len);
|
|
|
|
auto* data = reinterpret_cast<char*>(res + 1);
|
|
|
|
for (auto& str : strs)
|
2015-01-15 14:54:38 +01:00
|
|
|
{
|
2022-08-21 17:52:51 +02:00
|
|
|
if (str.length() == 0) // memccpy(..., nullptr, 0) is UB
|
|
|
|
continue;
|
2017-02-23 00:56:52 +01:00
|
|
|
memcpy(data, str.begin(), (size_t)str.length());
|
|
|
|
data += (int)str.length();
|
2015-01-15 14:54:38 +01:00
|
|
|
}
|
2017-02-23 00:56:52 +01:00
|
|
|
*data = 0;
|
|
|
|
return RefPtr<StringData, PtrPolicy>{res};
|
2015-01-15 14:54:38 +01:00
|
|
|
}
|
|
|
|
|
2023-11-17 07:01:51 +01:00
|
|
|
StringDataPtr StringData::Registry::intern(StringView str, size_t hash)
|
2015-01-15 14:54:38 +01:00
|
|
|
{
|
2023-11-17 07:01:51 +01:00
|
|
|
kak_assert(hash_value(str) == hash);
|
|
|
|
auto index = m_strings.find_index(str, hash);
|
|
|
|
if (index >= 0)
|
|
|
|
return StringDataPtr{m_strings.item(index).value};
|
2017-02-23 00:56:52 +01:00
|
|
|
|
|
|
|
auto data = StringData::create(str);
|
|
|
|
data->refcount |= interned_flag;
|
2023-11-17 07:01:51 +01:00
|
|
|
m_strings.insert({data->strview(), data.get()}, hash);
|
2017-02-23 00:56:52 +01:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2023-11-17 07:01:51 +01:00
|
|
|
StringDataPtr StringData::Registry::intern(StringView str)
|
|
|
|
{
|
|
|
|
return intern(str, hash_value(str));
|
|
|
|
}
|
|
|
|
|
2017-02-23 00:56:52 +01:00
|
|
|
void StringData::Registry::remove(StringView str)
|
|
|
|
{
|
2017-03-07 01:30:54 +01:00
|
|
|
kak_assert(m_strings.contains(str));
|
|
|
|
m_strings.unordered_remove(str);
|
2015-01-15 14:54:38 +01:00
|
|
|
}
|
|
|
|
|
2017-02-23 00:56:52 +01:00
|
|
|
void StringData::Registry::debug_stats() const
|
2015-01-15 14:54:38 +01:00
|
|
|
{
|
2015-06-06 12:54:48 +02:00
|
|
|
write_to_debug_buffer("Shared Strings stats:");
|
2015-01-15 14:54:38 +01:00
|
|
|
size_t total_refcount = 0;
|
|
|
|
size_t total_size = 0;
|
|
|
|
size_t count = m_strings.size();
|
|
|
|
for (auto& st : m_strings)
|
|
|
|
{
|
2018-07-26 14:20:19 +02:00
|
|
|
total_refcount += (st.value->refcount & refcount_mask) - 1;
|
2017-03-07 01:30:54 +01:00
|
|
|
total_size += (int)st.value->length;
|
2015-01-15 14:54:38 +01:00
|
|
|
}
|
2015-06-06 12:54:48 +02:00
|
|
|
write_to_debug_buffer(format(" data size: {}, mean: {}", total_size, (float)total_size/count));
|
|
|
|
write_to_debug_buffer(format(" refcounts: {}, mean: {}", total_refcount, (float)total_refcount/count));
|
2015-01-15 14:54:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|