Skip to content

Commit

Permalink
Use branching for bounds checking
Browse files Browse the repository at this point in the history
It turns out that branching code is faster than branch free code in this case.
CPUs are really good at branch prediction, I guess.
Measurements show the difference is very slight, but it is none the less there.
  • Loading branch information
chrisvest committed May 26, 2017
1 parent 4b49ff0 commit c1954e6
Showing 1 changed file with 20 additions and 16 deletions.
Expand Up @@ -392,19 +392,19 @@ long assertPagedFileStillMappedAndGetIdOfLastPage()
*/
private long getBoundedPointer( int offset, int size )
{
long p = pointer;
long can = p + offset;
if ( boundsCheck )
{
long can = pointer + offset;
long lim = pointer + pageSize - size;
long ref = Math.min( can, lim );
ref = Math.max( ref, pointer );
outOfBounds |= ref != can | lim < pointer;
return ref;
}
else
{
return pointer + offset;
if ( can + size > p + pageSize || can < p )
{
outOfBounds = true;
// Return the victim page when we are out of bounds, since at this point we can't tell if the pointer
// will be used for reading or writing.
return victimPage;
}
}
return can;
}

/**
Expand All @@ -415,15 +415,19 @@ private long getBoundedPointer( int offset, int size )
*/
private long nextBoundedPointer( int size )
{
int offset = this.offset;
long can = pointer + offset;
if ( boundsCheck )
{
outOfBounds |= offset + size > pageSize;
return outOfBounds? pointer : pointer + offset;
}
else
{
return pointer + offset;
if ( offset + size > pageSize )
{
outOfBounds = true;
// Return the victim page when we are out of bounds, since at this point we can't tell if the pointer
// will be used for reading or writing.
return victimPage;
}
}
return can;
}

@Override
Expand Down

0 comments on commit c1954e6

Please sign in to comment.