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.
This commit is contained in:
Min Hsu 2026-05-19 14:17:09 -07:00
parent f4d870e109
commit 9e02a87c1d
1 changed files with 10 additions and 10 deletions

View File

@ -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 ) static inline int Vec_IntRemove( Vec_Int_t * p, int Entry )
{ {
int i; int i, Size = p->nSize;
for ( i = 0; i < p->nSize; i++ ) for ( i = 0; i < Size; i++ )
if ( p->pArray[i] == Entry ) if ( p->pArray[i] == Entry )
break; break;
if ( i == p->nSize ) if ( i == Size )
return 0; return 0;
assert( i < p->nSize ); assert( i < Size );
for ( i++; i < p->nSize; i++ ) for ( i++; i < Size; i++ )
p->pArray[i-1] = p->pArray[i]; p->pArray[i-1] = p->pArray[i];
p->nSize--; p->nSize--;
return 1; return 1;
} }
static inline int Vec_IntRemove1( Vec_Int_t * p, int Entry ) static inline int Vec_IntRemove1( Vec_Int_t * p, int Entry )
{ {
int i; int i, Size = p->nSize;
for ( i = 1; i < p->nSize; i++ ) for ( i = 1; i < Size; i++ )
if ( p->pArray[i] == Entry ) if ( p->pArray[i] == Entry )
break; break;
if ( i >= p->nSize ) if ( i >= Size )
return 0; return 0;
assert( i < p->nSize ); assert( i < Size );
for ( i++; i < p->nSize; i++ ) for ( i++; i < Size; i++ )
p->pArray[i-1] = p->pArray[i]; p->pArray[i-1] = p->pArray[i];
p->nSize--; p->nSize--;
return 1; return 1;