From 7a52e42ee0676f47e801dd348b478302de5d2e50 Mon Sep 17 00:00:00 2001 From: Eric Date: Fri, 14 Aug 2020 03:11:22 +0200 Subject: transceiver: optimize code if optimizations are enabled There is no point in checking basic stuff ten thousand times per second since the sizes never change, so it's enough to enable the checks/assertions for unoptimized (debug) builds. This significantly decreases branch mispredictions. Change-Id: Iebd9e91b3c7f37f2dc646d3017c45139977e4d15 --- CommonLibs/Vector.h | 33 +++++++++++++++++++-------------- Transceiver52M/Resampler.cpp | 6 ++++-- Transceiver52M/arch/x86/convolve.c | 6 ++++-- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/CommonLibs/Vector.h b/CommonLibs/Vector.h index d55c5b3..6d1045d 100644 --- a/CommonLibs/Vector.h +++ b/CommonLibs/Vector.h @@ -36,6 +36,11 @@ #include #include +#ifndef __OPTIMIZE__ +#define assert_no_opt(x) assert(x) +#else +#define assert_no_opt(x) +#endif // We can't use Logger.h in this file... extern int gVectorDebug; #define BVDEBUG(msg) if (gVectorDebug) {std::cout << msg;} @@ -81,8 +86,8 @@ template class Vector { /** Return the size of the Vector. */ size_t size() const { - assert(mStart>=mData); - assert(mEnd>=mStart); + assert_no_opt(mStart>=mData); + assert_no_opt(mEnd>=mStart); return mEnd - mStart; } @@ -112,7 +117,7 @@ template class Vector { /** Reduce addressable size of the Vector, keeping content. */ void shrink(size_t newSize) { - assert(newSize <= mEnd - mStart); + assert_no_opt(newSize <= mEnd - mStart); mEnd = mStart + newSize; } @@ -199,7 +204,7 @@ template class Vector { { T* wStart = mStart + start; T* wEnd = wStart + span; - assert(wEnd<=mEnd); + assert_no_opt(wEnd<=mEnd); return Vector(NULL,wStart,wEnd); } @@ -208,7 +213,7 @@ template class Vector { { T* wStart = mStart + start; T* wEnd = wStart + span; - assert(wEnd<=mEnd); + assert_no_opt(wEnd<=mEnd); return Vector(NULL,wStart,wEnd); } @@ -228,8 +233,8 @@ template class Vector { unsigned int i; T* dst = other.mStart + start; T* src = mStart; - assert(dst+span<=other.mEnd); - assert(mStart+span<=mEnd); + assert_no_opt(dst+span<=other.mEnd); + assert_no_opt(mStart+span<=mEnd); for (i = 0; i < span; i++, src++, dst++) *dst = *src; /*TODO if not non-trivially copiable type class, optimize: @@ -250,8 +255,8 @@ template class Vector { void segmentCopyTo(Vector& other, size_t start, size_t span) const { const T* base = mStart + start; - assert(base+span<=mEnd); - assert(other.mStart+span<=other.mEnd); + assert_no_opt(base+span<=mEnd); + assert_no_opt(other.mStart+span<=other.mEnd); memcpy(other.mStart,base,span*sizeof(T)); } @@ -265,8 +270,8 @@ template class Vector { { const T* baseFrom = mStart + from; T* baseTo = mStart + to; - assert(baseFrom+span<=mEnd); - assert(baseTo+span<=mEnd); + assert_no_opt(baseFrom+span<=mEnd); + assert_no_opt(baseTo+span<=mEnd); memmove(baseTo,baseFrom,span*sizeof(T)); } @@ -280,7 +285,7 @@ template class Vector { { T* dp=mStart+start; T* end=dp+length; - assert(end<=mEnd); + assert_no_opt(end<=mEnd); while (dp class Vector { T& operator[](size_t index) { - assert(mStart+index