Skip to content

Commit 830c26f

Browse files
original-brownbeardavidkyle
authored andcommitted
Make two SubReaderWrapper implementations singletons (#112596)
random find: No state in either of these, no need to recreate them for every directory. Saves a few cycles and makes it more obvious that there's no state in these.
1 parent 73f0f6b commit 830c26f

File tree

2 files changed

+34
-23
lines changed

2 files changed

+34
-23
lines changed

server/src/main/java/org/elasticsearch/common/lucene/Lucene.java

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -884,24 +884,26 @@ protected StoredFieldsReader doGetSequentialStoredFieldsReader(StoredFieldsReade
884884
}
885885
}
886886

887-
DirectoryReaderWithAllLiveDocs(DirectoryReader in) throws IOException {
888-
super(in, new SubReaderWrapper() {
889-
@Override
890-
public LeafReader wrap(LeafReader leaf) {
891-
final SegmentReader segmentReader = segmentReader(leaf);
892-
final Bits hardLiveDocs = segmentReader.getHardLiveDocs();
893-
if (hardLiveDocs == null) {
894-
return new LeafReaderWithLiveDocs(leaf, null, leaf.maxDoc());
895-
}
896-
// Once soft-deletes is enabled, we no longer hard-update or hard-delete documents directly.
897-
// Two scenarios that we have hard-deletes: (1) from old segments where soft-deletes was disabled,
898-
// (2) when IndexWriter hits non-aborted exceptions. These two cases, IW flushes SegmentInfos
899-
// before exposing the hard-deletes, thus we can use the hard-delete count of SegmentInfos.
900-
final int numDocs = segmentReader.maxDoc() - segmentReader.getSegmentInfo().getDelCount();
901-
assert numDocs == popCount(hardLiveDocs) : numDocs + " != " + popCount(hardLiveDocs);
902-
return new LeafReaderWithLiveDocs(segmentReader, hardLiveDocs, numDocs);
887+
private static final SubReaderWrapper ALL_LIVE_DOCS_SUB_READER_WRAPPER = new SubReaderWrapper() {
888+
@Override
889+
public LeafReader wrap(LeafReader leaf) {
890+
final SegmentReader segmentReader = segmentReader(leaf);
891+
final Bits hardLiveDocs = segmentReader.getHardLiveDocs();
892+
if (hardLiveDocs == null) {
893+
return new LeafReaderWithLiveDocs(leaf, null, leaf.maxDoc());
903894
}
904-
});
895+
// Once soft-deletes is enabled, we no longer hard-update or hard-delete documents directly.
896+
// Two scenarios that we have hard-deletes: (1) from old segments where soft-deletes was disabled,
897+
// (2) when IndexWriter hits non-aborted exceptions. These two cases, IW flushes SegmentInfos
898+
// before exposing the hard-deletes, thus we can use the hard-delete count of SegmentInfos.
899+
final int numDocs = segmentReader.maxDoc() - segmentReader.getSegmentInfo().getDelCount();
900+
assert numDocs == popCount(hardLiveDocs) : numDocs + " != " + popCount(hardLiveDocs);
901+
return new LeafReaderWithLiveDocs(segmentReader, hardLiveDocs, numDocs);
902+
}
903+
};
904+
905+
DirectoryReaderWithAllLiveDocs(DirectoryReader in) throws IOException {
906+
super(in, ALL_LIVE_DOCS_SUB_READER_WRAPPER);
905907
}
906908

907909
@Override

server/src/main/java/org/elasticsearch/index/shard/IndexShard.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,13 +1700,22 @@ public void setGlobalCheckpointIfUnpromotable(long globalCheckpoint) {
17001700

17011701
private static final class NonClosingReaderWrapper extends FilterDirectoryReader {
17021702

1703+
private static final LeafReader[] EMPTY_LEAF_READERS = new LeafReader[0];
1704+
1705+
private static final FilterDirectoryReader.SubReaderWrapper SUB_READER_WRAPPER = new SubReaderWrapper() {
1706+
@Override
1707+
public LeafReader wrap(LeafReader reader) {
1708+
return reader;
1709+
}
1710+
1711+
@Override
1712+
protected LeafReader[] wrap(List<? extends LeafReader> readers) {
1713+
return readers.toArray(EMPTY_LEAF_READERS);
1714+
}
1715+
};
1716+
17031717
private NonClosingReaderWrapper(DirectoryReader in) throws IOException {
1704-
super(in, new SubReaderWrapper() {
1705-
@Override
1706-
public LeafReader wrap(LeafReader reader) {
1707-
return reader;
1708-
}
1709-
});
1718+
super(in, SUB_READER_WRAPPER);
17101719
}
17111720

17121721
@Override

0 commit comments

Comments
 (0)