Selaa lähdekoodia

Override BufferedInputStream to not sychronize single byte reads in Compressor (#109647)

With biased locking gone, we see some slowness in profiling
when we use this stream for single byte reads.
This is a recent regression that is a result of
https://openjdk.org/jeps/374.
-> the sychronization overhead for bulk reads hardly matters, but since
we do quite a few single byte reads lets fix this.
Armin Braun 1 vuosi sitten
vanhempi
commit
3b5395e31a

+ 10 - 1
server/src/main/java/org/elasticsearch/common/compress/Compressor.java

@@ -26,7 +26,16 @@ public interface Compressor {
      */
     default StreamInput threadLocalStreamInput(InputStream in) throws IOException {
         // wrap stream in buffer since InputStreamStreamInput doesn't do any buffering itself but does a lot of small reads
-        return new InputStreamStreamInput(new BufferedInputStream(threadLocalInputStream(in), DeflateCompressor.BUFFER_SIZE));
+        return new InputStreamStreamInput(new BufferedInputStream(threadLocalInputStream(in), DeflateCompressor.BUFFER_SIZE) {
+            @Override
+            public int read() throws IOException {
+                // override read to avoid synchronized single byte reads now that JEP374 removed biased locking
+                if (pos >= count) {
+                    return super.read();
+                }
+                return buf[pos++] & 0xFF;
+            }
+        });
     }
 
     /**