2015-01-15 16:54:38 +03:00
|
|
|
#include "shared_string.hh"
|
|
|
|
#include "debug.hh"
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
|
|
|
SharedString StringRegistry::intern(StringView str)
|
|
|
|
{
|
2015-04-16 14:38:59 +03:00
|
|
|
auto it = m_strings.find({str, SharedString::NoCopy{}});
|
2015-01-15 16:54:38 +03:00
|
|
|
if (it == m_strings.end())
|
|
|
|
{
|
|
|
|
SharedString shared_str = str;
|
2015-04-16 14:38:59 +03:00
|
|
|
it = m_strings.emplace(shared_str).first;
|
2015-01-15 16:54:38 +03:00
|
|
|
}
|
2015-04-16 14:38:59 +03:00
|
|
|
return *it;
|
2015-01-15 16:54:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void StringRegistry::purge_unused()
|
|
|
|
{
|
|
|
|
for (auto it = m_strings.begin(); it != m_strings.end(); )
|
|
|
|
{
|
2015-04-16 14:38:59 +03:00
|
|
|
if (it->m_storage->refcount == 1)
|
2015-01-15 16:54:38 +03:00
|
|
|
it = m_strings.erase(it);
|
|
|
|
else
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void StringRegistry::debug_stats() const
|
|
|
|
{
|
|
|
|
write_debug("Shared Strings stats:");
|
|
|
|
size_t total_refcount = 0;
|
|
|
|
size_t total_size = 0;
|
|
|
|
size_t count = m_strings.size();
|
|
|
|
for (auto& st : m_strings)
|
|
|
|
{
|
2015-04-16 14:38:59 +03:00
|
|
|
total_refcount += st.m_storage->refcount - 1;
|
|
|
|
total_size += (int)st.m_storage->length;
|
2015-01-15 16:54:38 +03:00
|
|
|
}
|
2015-03-31 01:06:02 +03:00
|
|
|
write_debug(format(" data size: {}, mean: {}", total_size, (float)total_size/count));
|
|
|
|
write_debug(format(" refcounts: {}, mean: {}", total_refcount, (float)total_refcount/count));
|
2015-01-15 16:54:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|