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

View File

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