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
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
|
|
|
SharedString StringRegistry::intern(StringView str)
|
|
|
|
{
|
2015-04-16 13:38:59 +02:00
|
|
|
auto it = m_strings.find({str, SharedString::NoCopy{}});
|
2015-01-15 14:54:38 +01:00
|
|
|
if (it == m_strings.end())
|
|
|
|
{
|
|
|
|
SharedString shared_str = str;
|
2015-04-16 13:38:59 +02:00
|
|
|
it = m_strings.emplace(shared_str).first;
|
2015-01-15 14:54:38 +01:00
|
|
|
}
|
2015-04-16 13:38:59 +02:00
|
|
|
return *it;
|
2015-01-15 14:54:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void StringRegistry::purge_unused()
|
|
|
|
{
|
|
|
|
for (auto it = m_strings.begin(); it != m_strings.end(); )
|
|
|
|
{
|
2015-04-16 13:38:59 +02:00
|
|
|
if (it->m_storage->refcount == 1)
|
2015-01-15 14:54:38 +01:00
|
|
|
it = m_strings.erase(it);
|
|
|
|
else
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StringRegistry::debug_stats() const
|
|
|
|
{
|
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)
|
|
|
|
{
|
2015-04-16 13:38:59 +02:00
|
|
|
total_refcount += st.m_storage->refcount - 1;
|
|
|
|
total_size += (int)st.m_storage->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
|
|
|
}
|
|
|
|
|
|
|
|
}
|