Skip to content

armgit / solr

Public Imported from GitHub
0

Apache Solr open-source search software

LUCENE-9535: Make ByteBuffersDataOutput#ramBytesUsed run in constant-time. (#1917)

Adrien Grandd226abd4481a
diff --git a/lucene/core/src/java/org/apache/lucene/store/ByteBuffersDataOutput.java b/lucene/core/src/java/org/apache/lucene/store/ByteBuffersDataOutput.java
index 34549c963de..ac1dc7579e5 100644
--- a/lucene/core/src/java/org/apache/lucene/store/ByteBuffersDataOutput.java
+++ b/lucene/core/src/java/org/apache/lucene/store/ByteBuffersDataOutput.java
@@ -400,8 +400,13 @@ public final class ByteBuffersDataOutput extends DataOutput implements Accountab
public long ramBytesUsed() {
// Return a rough estimation for allocated blocks. Note that we do not make
// any special distinction for direct memory buffers.
- return RamUsageEstimator.NUM_BYTES_OBJECT_REF * blocks.size() +
- blocks.stream().mapToLong(buf -> buf.capacity()).sum();
+ ByteBuffer first = blocks.peek();
+ if (first == null) {
+ return 0L;
+ } else {
+ // All blocks have the same capacity.
+ return (first.capacity() + RamUsageEstimator.NUM_BYTES_OBJECT_REF) * blocks.size();
+ }
}
/**