From 9e04cb9c41bcfb43bb1e8d1bc977ef7ec527aff9 Mon Sep 17 00:00:00 2001 From: Armin Braun Date: Mon, 8 Jul 2024 10:59:50 +0200 Subject: [PATCH] Override single byte writes to OutputStreamIndexOutput to remove locking (#13543) Single byte writes to BufferedOutputStream show up pretty hot in indexing benchmarks. We can save the locking overhead introduced by JEP374 by overriding and providing a no-lock fastpath. --- .../lucene/store/OutputStreamIndexOutput.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lucene/core/src/java/org/apache/lucene/store/OutputStreamIndexOutput.java b/lucene/core/src/java/org/apache/lucene/store/OutputStreamIndexOutput.java index 5c3b4dbd88a..14b56a67e8a 100644 --- a/lucene/core/src/java/org/apache/lucene/store/OutputStreamIndexOutput.java +++ b/lucene/core/src/java/org/apache/lucene/store/OutputStreamIndexOutput.java @@ -135,5 +135,19 @@ public class OutputStreamIndexOutput extends IndexOutput { BitUtil.VH_LE_LONG.set(buf, count, i); count += Long.BYTES; } + + @Override + public void write(int b) throws IOException { + // override single byte write to avoid synchronization overhead now that JEP374 removed biased + // locking + byte[] buffer = buf; + int count = this.count; + if (count >= buffer.length) { + super.write(b); + } else { + buffer[count] = (byte) b; + this.count = count + 1; + } + } } }