From 33b2efe3181267a047af6087899cc27a2df805ff Mon Sep 17 00:00:00 2001 From: Arseny Kapoulkine Date: Tue, 21 Apr 2015 23:02:44 -0700 Subject: 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. --- src/pugixml.cpp | 51 +++++++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 24 deletions(-) (limited to 'src/pugixml.cpp') diff --git a/src/pugixml.cpp b/src/pugixml.cpp index b2c13f5..1d0f5e8 100644 --- a/src/pugixml.cpp +++ b/src/pugixml.cpp @@ -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(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(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 -- cgit v1.2.3