1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-12-28 06:08:05 +03:00

Use uint32_t for interned strings slots

This commit is contained in:
Maxime Coste 2015-01-13 13:58:11 +00:00
parent 61619a4d4d
commit 118a6e1a7c
2 changed files with 13 additions and 10 deletions

View File

@ -28,7 +28,7 @@ InternedString StringRegistry::acquire(StringView str)
auto it = m_slot_map.find(str); auto it = m_slot_map.find(str);
if (it == m_slot_map.end()) if (it == m_slot_map.end())
{ {
size_t slot; Slot slot;
if (not m_free_slots.empty()) if (not m_free_slots.empty())
{ {
slot = m_free_slots.back(); slot = m_free_slots.back();
@ -48,20 +48,20 @@ InternedString StringRegistry::acquire(StringView str)
return InternedString{storage_view, slot}; return InternedString{storage_view, slot};
} }
size_t slot = it->second; Slot slot = it->second;
auto& data = m_storage[slot]; auto& data = m_storage[slot];
++data.refcount; ++data.refcount;
return {{data.data.data(), (int)data.data.size()}, slot}; return {{data.data.data(), (int)data.data.size()}, slot};
} }
void StringRegistry::acquire(size_t slot) void StringRegistry::acquire(Slot slot)
{ {
kak_assert(slot < m_storage.size()); kak_assert(slot < m_storage.size());
kak_assert(m_storage[slot].refcount > 0); kak_assert(m_storage[slot].refcount > 0);
++m_storage[slot].refcount; ++m_storage[slot].refcount;
} }
void StringRegistry::release(size_t slot) noexcept void StringRegistry::release(Slot slot) noexcept
{ {
kak_assert(m_storage[slot].refcount > 0); kak_assert(m_storage[slot].refcount > 0);
if (--m_storage[slot].refcount == 0) if (--m_storage[slot].refcount == 0)

View File

@ -6,6 +6,8 @@
#include "unordered_map.hh" #include "unordered_map.hh"
#include "vector.hh" #include "vector.hh"
#include <cstddef>
namespace Kakoune namespace Kakoune
{ {
@ -17,13 +19,14 @@ public:
void debug_stats() const; void debug_stats() const;
private: private:
friend class InternedString; friend class InternedString;
using Slot = uint32_t;
InternedString acquire(StringView str); InternedString acquire(StringView str);
void acquire(size_t slot); void acquire(Slot slot);
void release(size_t slot) noexcept; void release(Slot slot) noexcept;
UnorderedMap<StringView, size_t, MemoryDomain::InternedString> m_slot_map; UnorderedMap<StringView, Slot, MemoryDomain::InternedString> m_slot_map;
Vector<size_t, MemoryDomain::InternedString> m_free_slots; Vector<Slot, MemoryDomain::InternedString> m_free_slots;
struct DataAndRefCount struct DataAndRefCount
{ {
Vector<char, MemoryDomain::InternedString> data; Vector<char, MemoryDomain::InternedString> data;
@ -101,7 +104,7 @@ public:
private: private:
friend class StringRegistry; friend class StringRegistry;
InternedString(StringView str, size_t slot) InternedString(StringView str, StringRegistry::Slot slot)
: StringView(str), m_slot(slot) {} : StringView(str), m_slot(slot) {}
void acquire_ifn(StringView str) void acquire_ifn(StringView str)
@ -121,7 +124,7 @@ private:
StringRegistry::instance().release(m_slot); StringRegistry::instance().release(m_slot);
} }
size_t m_slot = -1; StringRegistry::Slot m_slot = -1;
}; };
inline size_t hash_value(const Kakoune::InternedString& str) inline size_t hash_value(const Kakoune::InternedString& str)