From c1954e6af6ec75d079ff94f7008cbe2da667392e Mon Sep 17 00:00:00 2001 From: Chris Vest Date: Sun, 19 Mar 2017 09:11:08 +0100 Subject: [PATCH] Use branching for bounds checking 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. --- .../impl/muninn/MuninnPageCursor.java | 36 ++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/community/io/src/main/java/org/neo4j/io/pagecache/impl/muninn/MuninnPageCursor.java b/community/io/src/main/java/org/neo4j/io/pagecache/impl/muninn/MuninnPageCursor.java index d769380f1347..3e10541bfe14 100644 --- a/community/io/src/main/java/org/neo4j/io/pagecache/impl/muninn/MuninnPageCursor.java +++ b/community/io/src/main/java/org/neo4j/io/pagecache/impl/muninn/MuninnPageCursor.java @@ -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; } /** @@ -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