From 9359cfd32f1f2c9ec1fd7e3cabd75ada4fc91204 Mon Sep 17 00:00:00 2001 From: Adrien Grand Date: Tue, 29 Oct 2024 18:16:52 +0100 Subject: [PATCH] Speed up Lucene912PostingsReader nextDoc() impls. (#13963) 127 times out of 128, nextDoc() returns the next doc ID in the buffer. Currently, we check if the current doc is equal to the last doc ID in the block to know if we need to refill. We can do better by comparing the current index in the block with the block size, which is a bit more efficient since the latter is a constant. --- lucene/CHANGES.txt | 3 +++ .../lucene912/Lucene912PostingsReader.java | 21 +++++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index 7146a97195eb..66fe8b4ebd57 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -77,6 +77,9 @@ Optimizations * GITHUB#13961: Replace Map with IntObjectHashMap for DV producer. (Pan Guixin) +* GITHUB#13963: Speed up nextDoc() implementations in Lucene912PostingsReader. + (Adrien Grand) + Bug Fixes --------------------- * GITHUB#13832: Fixed an issue where the DefaultPassageFormatter.format method did not format passages as intended diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene912/Lucene912PostingsReader.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene912/Lucene912PostingsReader.java index cea329c93ca3..6da66e4ddf2a 100644 --- a/lucene/core/src/java/org/apache/lucene/codecs/lucene912/Lucene912PostingsReader.java +++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene912/Lucene912PostingsReader.java @@ -580,7 +580,7 @@ private void moveToNextLevel0Block() throws IOException { @Override public int nextDoc() throws IOException { - if (doc == level0LastDocID) { // advance skip data on level 0 + if (docBufferUpto == BLOCK_SIZE) { // advance skip data on level 0 moveToNextLevel0Block(); } @@ -875,7 +875,7 @@ private void moveToNextLevel0Block() throws IOException { @Override public int nextDoc() throws IOException { - if (doc == level0LastDocID) { // advance level 0 skip data + if (docBufferUpto == BLOCK_SIZE) { // advance level 0 skip data moveToNextLevel0Block(); } @@ -1417,11 +1417,13 @@ private void moveToNextLevel0Block() throws IOException { @Override public int nextDoc() throws IOException { - if (doc == level0LastDocID) { - moveToNextLevel0Block(); - } else if (needsRefilling) { - refillDocs(); - needsRefilling = false; + if (docBufferUpto == BLOCK_SIZE) { + if (needsRefilling) { + refillDocs(); + needsRefilling = false; + } else { + moveToNextLevel0Block(); + } } return this.doc = (int) docBuffer[docBufferUpto++]; @@ -1644,8 +1646,9 @@ public void advanceShallow(int target) throws IOException { @Override public int nextDoc() throws IOException { - advanceShallow(doc + 1); - if (needsRefilling) { + if (docBufferUpto == BLOCK_SIZE) { + advanceShallow(doc + 1); + assert needsRefilling; refillDocs(); needsRefilling = false; }