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.
This commit is contained in:
Armin Braun 2024-07-08 10:59:50 +02:00 committed by GitHub
parent 675772546c
commit 9e04cb9c41
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 14 additions and 0 deletions

View File

@ -135,5 +135,19 @@ public class OutputStreamIndexOutput extends IndexOutput {
BitUtil.VH_LE_LONG.set(buf, count, i); BitUtil.VH_LE_LONG.set(buf, count, i);
count += Long.BYTES; 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;
}
}
} }
} }