summaryrefslogtreecommitdiff
path: root/src/shared_string.cc
blob: 8b4f6e6e3ea05913f13d2248295bc2d8f1db86c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include "shared_string.hh"
#include "debug.hh"
#include "format.hh"

namespace Kakoune
{

StringDataPtr StringData::Registry::intern(StringView str, size_t hash)
{
    kak_assert(hash_value(str) == hash);
    auto index = m_strings.find_index(str, hash);
    if (index >= 0)
        return StringDataPtr{m_strings.item(index).value};

    auto data = StringData::create(str);
    data->refcount |= interned_flag;
    m_strings.insert({data->strview(), data.get()}, hash);
    return data;
}

StringDataPtr StringData::Registry::intern(StringView str)
{
    return intern(str, hash_value(str));
}

void StringData::Registry::remove(StringView str)
{
    kak_assert(m_strings.contains(str));
    m_strings.unordered_remove(str);
}

void StringData::Registry::debug_stats() const
{
    write_to_debug_buffer("Interned Strings stats:");
    size_t count = m_strings.size();
    size_t total_refcount = 0;
    size_t total_size = 0;
    for (auto& st : m_strings)
    {
        total_refcount += st.value->refcount & refcount_mask;
        total_size += (int)st.value->length;
    }
    write_to_debug_buffer(format("  count: {}", count));
    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));
}

}