Optimize xml_allocator::reserve()

Make sure compact_hash_table::rehash() is not inlined - that way reserve() is
inlined so the fast path has no extra function calls.

Also use subtraction instead of multiplication when checking capacity.
This commit is contained in:
Arseny Kapoulkine 2015-04-21 23:02:44 -07:00
parent 52bcb4ecd6
commit 33b2efe318

View File

@ -338,7 +338,7 @@ PUGI__NS_BEGIN
bool reserve()
{
if (_count + 16 >= _capacity * 3 / 4)
if (_count + 16 >= _capacity - _capacity / 4)
return rehash();
return true;
@ -356,29 +356,7 @@ PUGI__NS_BEGIN
size_t _count;
bool rehash()
{
compact_hash_table rt;
rt._capacity = (_capacity == 0) ? 32 : _capacity * 2;
rt._items = static_cast<item_t*>(xml_memory::allocate(sizeof(item_t) * rt._capacity));
if (!rt._items)
return false;
memset(rt._items, 0, sizeof(item_t) * rt._capacity);
for (size_t i = 0; i < _capacity; ++i)
if (_items[i].key)
*rt.insert(_items[i].key) = _items[i].value;
if (_items)
xml_memory::deallocate(_items);
_capacity = rt._capacity;
_items = rt._items;
return true;
}
bool rehash();
static unsigned int hash(const void* key)
{
@ -394,6 +372,31 @@ PUGI__NS_BEGIN
return h;
}
};
PUGI__FN_NO_INLINE bool compact_hash_table::rehash()
{
compact_hash_table rt;
rt._capacity = (_capacity == 0) ? 32 : _capacity * 2;
rt._items = static_cast<item_t*>(xml_memory::allocate(sizeof(item_t) * rt._capacity));
if (!rt._items)
return false;
memset(rt._items, 0, sizeof(item_t) * rt._capacity);
for (size_t i = 0; i < _capacity; ++i)
if (_items[i].key)
*rt.insert(_items[i].key) = _items[i].value;
if (_items)
xml_memory::deallocate(_items);
_capacity = rt._capacity;
_items = rt._items;
return true;
}
PUGI__NS_END
#endif