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
|
|
|
|
{
|
|
|
|
|
2016-02-05 01:20:45 +01:00
|
|
|
StringDataPtr StringRegistry::intern(StringView str)
|
2015-01-15 14:54:38 +01:00
|
|
|
{
|
2016-02-05 01:20:45 +01:00
|
|
|
auto it = m_strings.find(str);
|
2015-01-15 14:54:38 +01:00
|
|
|
if (it == m_strings.end())
|
|
|
|
{
|
2016-02-05 01:20:45 +01:00
|
|
|
auto data = StringData::create(str);
|
|
|
|
it = m_strings.emplace(data->strview(), data).first;
|
2015-01-15 14:54:38 +01:00
|
|
|
}
|
2016-02-05 01:20:45 +01:00
|
|
|
return it->second;
|
2015-01-15 14:54:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void StringRegistry::purge_unused()
|
|
|
|
{
|
|
|
|
for (auto it = m_strings.begin(); it != m_strings.end(); )
|
|
|
|
{
|
2016-02-05 01:20:45 +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
|
|
|
|
{
|
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)
|
|
|
|
{
|
2016-02-05 01:20:45 +01:00
|
|
|
total_refcount += st.second->refcount - 1;
|
|
|
|
total_size += (int)st.second->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
|
|
|
}
|
|
|
|
|
|
|
|
}
|