2015-01-15 14:54:38 +01:00
|
|
|
#include "shared_string.hh"
|
|
|
|
#include "debug.hh"
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
|
|
|
SharedString StringRegistry::intern(StringView str)
|
|
|
|
{
|
|
|
|
auto it = m_strings.find(str);
|
|
|
|
if (it == m_strings.end())
|
|
|
|
{
|
|
|
|
SharedString shared_str = str;
|
2015-01-18 19:23:58 +01:00
|
|
|
it = m_strings.emplace(shared_str, shared_str.m_storage).first;
|
2015-01-15 14:54:38 +01:00
|
|
|
}
|
2015-01-19 20:31:56 +01:00
|
|
|
return SharedString{it->second};
|
2015-01-15 14:54:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void StringRegistry::purge_unused()
|
|
|
|
{
|
|
|
|
for (auto it = m_strings.begin(); it != m_strings.end(); )
|
|
|
|
{
|
2015-01-18 19:23:58 +01:00
|
|
|
if (it->second->refcount == 1)
|
2015-01-15 14:54:38 +01: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-01-18 19:23:58 +01:00
|
|
|
total_refcount += st.second->refcount - 1;
|
2015-01-19 14:25:04 +01:00
|
|
|
total_size += (int)st.second->length;
|
2015-01-15 14:54:38 +01:00
|
|
|
}
|
|
|
|
write_debug(" data size: " + to_string(total_size) + ", mean: " + to_string((float)total_size/count));
|
|
|
|
write_debug(" refcounts: " + to_string(total_refcount) + ", mean: " + to_string((float)total_refcount/count));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|