From 904a85a27eda2b1f87d32c5f0a0598d8273d6569 Mon Sep 17 00:00:00 2001 From: jsteemann Date: Sat, 23 Jan 2016 17:26:11 +0100 Subject: [PATCH] less re-allocations --- arangod/VocBase/VocShaper.cpp | 24 ++++++++++++++++-------- lib/Basics/associative.cpp | 18 ++++++++++++------ lib/Basics/associative.h | 2 +- 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/arangod/VocBase/VocShaper.cpp b/arangod/VocBase/VocShaper.cpp index 923ae81c9e..9685a8f6a1 100644 --- a/arangod/VocBase/VocShaper.cpp +++ b/arangod/VocBase/VocShaper.cpp @@ -527,16 +527,16 @@ TRI_shape_aid_t VocShaper::findOrCreateAttributeByName(char const* name) { { // make room for one more element WRITE_LOCKER(_attributeIdsLock); - if (! TRI_ReserveAssociativePointer(&_attributeIds, _attributeIds._nrUsed + 1)) { - return 0; + if (! TRI_ReserveAssociativePointer(&_attributeIds, 1)) { + THROW_ARANGO_EXCEPTION(TRI_ERROR_OUT_OF_MEMORY); } } { // make room for one more element WRITE_LOCKER(_attributeNamesLock); - if (! TRI_ReserveAssociativePointer(&_attributeNames, _attributeNames._nrUsed + 1)) { - return 0; + if (! TRI_ReserveAssociativePointer(&_attributeNames, 1)) { + THROW_ARANGO_EXCEPTION(TRI_ERROR_OUT_OF_MEMORY); } } @@ -640,11 +640,19 @@ TRI_shape_t const* VocShaper::findShape(TRI_shape_t* shape, bool create) { THROW_ARANGO_EXCEPTION(TRI_ERROR_DEBUG); } + { + // make room for one more element + WRITE_LOCKER(_shapeIdsLock); + if (! TRI_ReserveAssociativePointer(&_shapeIds, 1)) { + THROW_ARANGO_EXCEPTION(TRI_ERROR_OUT_OF_MEMORY); + } + } + { // make room for one more element WRITE_LOCKER(_shapeDictionaryLock); - if (! TRI_ReserveAssociativePointer(&_shapeDictionary, _shapeDictionary._nrUsed + 1)) { - return 0; + if (! TRI_ReserveAssociativePointer(&_shapeDictionary, 1)) { + THROW_ARANGO_EXCEPTION(TRI_ERROR_OUT_OF_MEMORY); } } @@ -1216,7 +1224,7 @@ TRI_shape_path_t const* VocShaper::findShapePathByName(char const* name, { // make room for one more element WRITE_LOCKER(_attributePathsByNameLock); - if (! TRI_ReserveAssociativePointer(&_attributePathsByName, _attributePathsByName._nrUsed + 1)) { + if (! TRI_ReserveAssociativePointer(&_attributePathsByName, 1)) { TRI_Free(_memoryZone, result); return nullptr; } @@ -1225,7 +1233,7 @@ TRI_shape_path_t const* VocShaper::findShapePathByName(char const* name, { // make room for one more element WRITE_LOCKER(_attributePathsByPidLock); - if (! TRI_ReserveAssociativePointer(&_attributePathsByPid, _attributePathsByPid._nrUsed + 1)) { + if (! TRI_ReserveAssociativePointer(&_attributePathsByPid, 1)) { TRI_Free(_memoryZone, result); return nullptr; } diff --git a/lib/Basics/associative.cpp b/lib/Basics/associative.cpp index 2700c8937e..579763e67b 100644 --- a/lib/Basics/associative.cpp +++ b/lib/Basics/associative.cpp @@ -179,16 +179,22 @@ bool TRI_EqualStringKeyAssociativePointer(TRI_associative_pointer_t* array, //////////////////////////////////////////////////////////////////////////////// bool TRI_ReserveAssociativePointer(TRI_associative_pointer_t* array, - int32_t nrElements) { + uint32_t nrElements) { uint32_t targetSize = array->_nrUsed + nrElements; - if (array->_nrAlloc < 2 * targetSize) { - // we must resize - return ResizeAssociativePointer(array, (uint32_t)(2 * targetSize) + 1); + if (array->_nrAlloc >= 2 * targetSize) { + // no need to resize + return true; } - // no seed to resize - return true; + // we must resize + + // make sure we grow the array by a huge amount so we have only few resizes + if (targetSize < (2 * array->_nrAlloc) + 1) { + targetSize = (2 * array->_nrAlloc) + 1; + } + + return ResizeAssociativePointer(array, targetSize); } //////////////////////////////////////////////////////////////////////////////// diff --git a/lib/Basics/associative.h b/lib/Basics/associative.h index 4863fe9ffb..76bf728528 100644 --- a/lib/Basics/associative.h +++ b/lib/Basics/associative.h @@ -101,7 +101,7 @@ bool TRI_EqualStringKeyAssociativePointer(TRI_associative_pointer_t*, /// @brief reserves space in the array for extra elements //////////////////////////////////////////////////////////////////////////////// -bool TRI_ReserveAssociativePointer(TRI_associative_pointer_t*, int32_t); +bool TRI_ReserveAssociativePointer(TRI_associative_pointer_t*, uint32_t); //////////////////////////////////////////////////////////////////////////////// /// @brief lookups an element given a key