From 9e02a87c1d9bd4a72d479f2d5e7918bd6b05c584 Mon Sep 17 00:00:00 2001 From: Min Hsu Date: Tue, 19 May 2026 14:17:09 -0700 Subject: [PATCH] misc: Prevent Vec_IntRemove from loading nSize in every iteration This seemly benign loop ``` for ( i++; i < p->nSize; i++ ) p->pArray[i-1] = p->pArray[i]; ``` will actually load `p->nSize` in every loop iteration (rather than memorizing the value) due to some unfortunate pointer aliasing properties in C/C++. As Vec_IntRemove is quite ubiquitous, this extra memory load actually causes visible performance impact and prevents further optimizations on the loop. This patch fixes this by factoring `p->nSize` out of the loop. --- src/misc/vec/vecInt.h | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/misc/vec/vecInt.h b/src/misc/vec/vecInt.h index e32a2dd18a..9a73cde837 100644 --- a/src/misc/vec/vecInt.h +++ b/src/misc/vec/vecInt.h @@ -1069,28 +1069,28 @@ static inline int Vec_IntFind( Vec_Int_t * p, int Entry ) ***********************************************************************/ static inline int Vec_IntRemove( Vec_Int_t * p, int Entry ) { - int i; - for ( i = 0; i < p->nSize; i++ ) + int i, Size = p->nSize; + for ( i = 0; i < Size; i++ ) if ( p->pArray[i] == Entry ) break; - if ( i == p->nSize ) + if ( i == Size ) return 0; - assert( i < p->nSize ); - for ( i++; i < p->nSize; i++ ) + assert( i < Size ); + for ( i++; i < Size; i++ ) p->pArray[i-1] = p->pArray[i]; p->nSize--; return 1; } static inline int Vec_IntRemove1( Vec_Int_t * p, int Entry ) { - int i; - for ( i = 1; i < p->nSize; i++ ) + int i, Size = p->nSize; + for ( i = 1; i < Size; i++ ) if ( p->pArray[i] == Entry ) break; - if ( i >= p->nSize ) + if ( i >= Size ) return 0; - assert( i < p->nSize ); - for ( i++; i < p->nSize; i++ ) + assert( i < Size ); + for ( i++; i < Size; i++ ) p->pArray[i-1] = p->pArray[i]; p->nSize--; return 1;