shared_string.cc (1351B)
1 #include "shared_string.hh" 2 #include "debug.hh" 3 #include "format.hh" 4 5 namespace Kakoune 6 { 7 8 StringDataPtr StringData::Registry::intern(StringView str, size_t hash) 9 { 10 kak_assert(hash_value(str) == hash); 11 auto index = m_strings.find_index(str, hash); 12 if (index >= 0) 13 return StringDataPtr{m_strings.item(index).value}; 14 15 auto data = StringData::create(str); 16 data->refcount |= interned_flag; 17 m_strings.insert({data->strview(), data.get()}, hash); 18 return data; 19 } 20 21 StringDataPtr StringData::Registry::intern(StringView str) 22 { 23 return intern(str, hash_value(str)); 24 } 25 26 void StringData::Registry::remove(StringView str) 27 { 28 kak_assert(m_strings.contains(str)); 29 m_strings.unordered_remove(str); 30 } 31 32 void StringData::Registry::debug_stats() const 33 { 34 write_to_debug_buffer("Interned Strings stats:"); 35 size_t count = m_strings.size(); 36 size_t total_refcount = 0; 37 size_t total_size = 0; 38 for (auto& st : m_strings) 39 { 40 total_refcount += st.value->refcount & refcount_mask; 41 total_size += (int)st.value->length; 42 } 43 write_to_debug_buffer(format(" count: {}", count)); 44 write_to_debug_buffer(format(" data size: {}, mean: {}", total_size, (float)total_size/count)); 45 write_to_debug_buffer(format(" refcounts: {}, mean: {}", total_refcount, (float)total_refcount/count)); 46 } 47 48 }