From da41b70b18d8e05bc4f5aba19df9aad511fa3d5a Mon Sep 17 00:00:00 2001 From: Zufu Liu Date: Wed, 8 Jan 2025 09:20:56 +1100 Subject: Feature [feature-requests:#1540]. Optimize InsertEmpty to memset equivalent. Simplify second argument by std::fill -> std::fill_n, std::copy -> std::copy_n. --- src/SplitVector.h | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/SplitVector.h b/src/SplitVector.h index 19b854f5a..081c7cbfb 100644 --- a/src/SplitVector.h +++ b/src/SplitVector.h @@ -191,7 +191,7 @@ public: } RoomFor(insertLength); GapTo(position); - std::fill(body.data() + part1Length, body.data() + part1Length + insertLength, v); + std::fill_n(body.data() + part1Length, insertLength, v); lengthBody += insertLength; part1Length += insertLength; gapLength -= insertLength; @@ -210,10 +210,8 @@ public: } RoomFor(insertLength); GapTo(position); - for (ptrdiff_t elem = part1Length; elem < part1Length + insertLength; elem++) { - T emptyOne = {}; - body[elem] = std::move(emptyOne); - } + T *ptr = body.data() + part1Length; + std::uninitialized_value_construct_n(ptr, insertLength); lengthBody += insertLength; part1Length += insertLength; gapLength -= insertLength; @@ -238,7 +236,7 @@ public: } RoomFor(insertLength); GapTo(positionToInsert); - std::copy(s + positionFrom, s + positionFrom + insertLength, body.data() + part1Length); + std::copy_n(s + positionFrom, insertLength, body.data() + part1Length); lengthBody += insertLength; part1Length += insertLength; gapLength -= insertLength; @@ -284,11 +282,11 @@ public: if (range1Length > part1AfterPosition) range1Length = part1AfterPosition; } - std::copy(body.data() + position, body.data() + position + range1Length, buffer); + std::copy_n(body.data() + position, range1Length, buffer); buffer += range1Length; position = position + range1Length + gapLength; const ptrdiff_t range2Length = retrieveLength - range1Length; - std::copy(body.data() + position, body.data() + position + range2Length, buffer); + std::copy_n(body.data() + position, range2Length, buffer); } /// Compact the buffer and return a pointer to the first element. -- cgit v1.2.3