From 5e42275b1a807c817846f7306fa82f92379dcd62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Eric=20Janouch?= Date: Sat, 27 Mar 2021 14:48:31 +0100 Subject: [PATCH] Fix pathologically slow translate groups on Linux Major performance improvement on GCC with libstdc++. --- src/dsc.h | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/dsc.h b/src/dsc.h index 93403bca..34190e62 100644 --- a/src/dsc.h +++ b/src/dsc.h @@ -467,11 +467,17 @@ public: // Look to see if we already have something with the same handle value. ssassert(FindByIdNoOops(t->h) == nullptr, "Handle isn't unique"); - // Copy-construct at the end of the list. - new(&elem[n]) T(*t); + // Find out where the added element should be. + int pos = LowerBoundIndex(*t); + + // Shift everything from there to the end of the array. + new(&elem[n]) T(); + for (int i = n; i > pos; i--) + elem[i] = std::move(elem[i - 1]); + + // Copy-construct at the right place. + elem[pos] = T(*t); ++n; - // The item we just added is trivially sorted, so "merge" - std::inplace_merge(begin(), end() - 1, end(), Compare()); } T *FindById(H h) {