Use an UnorderedSet for string registry

This commit is contained in:
Maxime Coste 2015-04-16 12:38:59 +01:00
parent 491ffc4ffd
commit 2902fce437
3 changed files with 12 additions and 7 deletions

View File

@ -6,20 +6,20 @@ namespace Kakoune
SharedString StringRegistry::intern(StringView str) SharedString StringRegistry::intern(StringView str)
{ {
auto it = m_strings.find(str); auto it = m_strings.find({str, SharedString::NoCopy{}});
if (it == m_strings.end()) if (it == m_strings.end())
{ {
SharedString shared_str = str; SharedString shared_str = str;
it = m_strings.emplace(shared_str, shared_str.m_storage).first; it = m_strings.emplace(shared_str).first;
} }
return SharedString{it->second}; return *it;
} }
void StringRegistry::purge_unused() void StringRegistry::purge_unused()
{ {
for (auto it = m_strings.begin(); it != m_strings.end(); ) for (auto it = m_strings.begin(); it != m_strings.end(); )
{ {
if (it->second->refcount == 1) if (it->m_storage->refcount == 1)
it = m_strings.erase(it); it = m_strings.erase(it);
else else
++it; ++it;
@ -34,8 +34,8 @@ void StringRegistry::debug_stats() const
size_t count = m_strings.size(); size_t count = m_strings.size();
for (auto& st : m_strings) for (auto& st : m_strings)
{ {
total_refcount += st.second->refcount - 1; total_refcount += st.m_storage->refcount - 1;
total_size += (int)st.second->length; total_size += (int)st.m_storage->length;
} }
write_debug(format(" data size: {}, mean: {}", total_size, (float)total_size/count)); write_debug(format(" data size: {}, mean: {}", total_size, (float)total_size/count));
write_debug(format(" refcounts: {}, mean: {}", total_refcount, (float)total_refcount/count)); write_debug(format(" refcounts: {}, mean: {}", total_refcount, (float)total_refcount/count));

View File

@ -109,7 +109,7 @@ public:
void purge_unused(); void purge_unused();
private: private:
UnorderedMap<StringView, StringDataPtr, MemoryDomain::SharedString> m_strings; UnorderedSet<SharedString, MemoryDomain::SharedString> m_strings;
}; };
inline SharedString intern(StringView str) inline SharedString intern(StringView str)

View File

@ -5,6 +5,7 @@
#include "memory.hh" #include "memory.hh"
#include <unordered_map> #include <unordered_map>
#include <unordered_set>
namespace Kakoune namespace Kakoune
{ {
@ -13,6 +14,10 @@ template<typename Key, typename Value, MemoryDomain domain = TypeDomain<Key>::do
using UnorderedMap = std::unordered_map<Key, Value, Hash<Key>, std::equal_to<Key>, using UnorderedMap = std::unordered_map<Key, Value, Hash<Key>, std::equal_to<Key>,
Allocator<std::pair<const Key, Value>, domain>>; Allocator<std::pair<const Key, Value>, domain>>;
template<typename Key, MemoryDomain domain = TypeDomain<Key>::domain()>
using UnorderedSet = std::unordered_set<Key, Hash<Key>, std::equal_to<Key>,
Allocator<const Key, domain>>;
} }
#endif // unordered_map_hh_INCLUDED #endif // unordered_map_hh_INCLUDED