diff options
author | Arseny Kapoulkine <arseny.kapoulkine@gmail.com> | 2015-04-21 23:02:44 -0700 |
---|---|---|
committer | Arseny Kapoulkine <arseny.kapoulkine@gmail.com> | 2015-04-21 23:02:44 -0700 |
commit | 33b2efe3181267a047af6087899cc27a2df805ff (patch) | |
tree | 91d3ff828fa5ba401e3abd3ca93b165e8996cbd0 /src | |
parent | 52bcb4ecd6a1f87c7a8f82315faac26f5066117a (diff) |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/pugixml.cpp | 51 |
1 files changed, 27 insertions, 24 deletions
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<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 |