From fe49713d1580ac1fdb4e3fdd4b021935e9ebab41 Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Mon, 5 May 2025 22:54:19 -0500 Subject: [PATCH 1/2] Don't double-read the input stream resource.getInputStream always returns a LoadServiceResourceInputStream so constructing another is just a waste of time. --- core/src/main/java/org/jruby/runtime/load/ExternalScript.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/main/java/org/jruby/runtime/load/ExternalScript.java b/core/src/main/java/org/jruby/runtime/load/ExternalScript.java index 6ec95b7895a..73f627001b2 100644 --- a/core/src/main/java/org/jruby/runtime/load/ExternalScript.java +++ b/core/src/main/java/org/jruby/runtime/load/ExternalScript.java @@ -56,7 +56,7 @@ public void load(Ruby runtime, boolean wrap) { } else { name = CompiledScriptLoader.getFilenameFromPathAndName(resource.getPath(), name, resource.isAbsolute()); - runtime.loadFile(name, new LoadServiceResourceInputStream(in), wrap); + runtime.loadFile(name, in, wrap); } } catch (IOException e) { throw runtime.newIOErrorFromException(e); From 90849cea143560d87de9b38546cdae84feb136ff Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Mon, 5 May 2025 22:58:18 -0500 Subject: [PATCH 2/2] Use Java 9 readAllBytes InputStream.readAllBytes was added in Java 9, and when possible it detects the exact size of the incoming file or stream and allocates only a single array to read all. This avoids the old logic that allocated n + 1 arrays based on the read chunk size. --- .../load/LoadServiceResourceInputStream.java | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/core/src/main/java/org/jruby/runtime/load/LoadServiceResourceInputStream.java b/core/src/main/java/org/jruby/runtime/load/LoadServiceResourceInputStream.java index 62c28ab2f70..5471a6ebe41 100644 --- a/core/src/main/java/org/jruby/runtime/load/LoadServiceResourceInputStream.java +++ b/core/src/main/java/org/jruby/runtime/load/LoadServiceResourceInputStream.java @@ -41,17 +41,9 @@ public byte[] getBytes() { return buf; } - private static final int READ_CHUNK_SIZE = 16384; - private void bufferEntireStream(InputStream stream) throws IOException { - byte[] chunk = new byte[READ_CHUNK_SIZE]; - int bytesRead; - while ((bytesRead = stream.read(chunk)) != -1) { - byte[] newbuf = new byte[buf.length + bytesRead]; - System.arraycopy(buf, 0, newbuf, 0, buf.length); - System.arraycopy(chunk, 0, newbuf, buf.length, bytesRead); - buf = newbuf; - count = buf.length; - } + byte[] all = stream.readAllBytes(); + buf = all; + count = all.length; } }