From 5f67ced9da066b449ae47f741a86a9ba1499d7b2 Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Mon, 1 Aug 2022 15:03:14 +0300 Subject: [PATCH] Optimize the use of buffers in health reporting This is done by leveraging Vert.x's Buffer directly instead of relying on a ByteArrayOutputStream which results in a lot of unnecessary copying of data --- .../runtime/SmallRyeHealthHandlerBase.java | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/extensions/smallrye-health/runtime/src/main/java/io/quarkus/smallrye/health/runtime/SmallRyeHealthHandlerBase.java b/extensions/smallrye-health/runtime/src/main/java/io/quarkus/smallrye/health/runtime/SmallRyeHealthHandlerBase.java index f1b41271c2ac7..0724d7026625c 100644 --- a/extensions/smallrye-health/runtime/src/main/java/io/quarkus/smallrye/health/runtime/SmallRyeHealthHandlerBase.java +++ b/extensions/smallrye-health/runtime/src/main/java/io/quarkus/smallrye/health/runtime/SmallRyeHealthHandlerBase.java @@ -1,7 +1,7 @@ package io.quarkus.smallrye.health.runtime; -import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.OutputStream; import java.io.UncheckedIOException; import io.quarkus.arc.Arc; @@ -47,11 +47,31 @@ private void doHandle(RoutingContext ctx) { resp.setStatusCode(503); } resp.headers().set(HttpHeaders.CONTENT_TYPE, "application/json; charset=UTF-8"); - try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { + Buffer buffer = Buffer.buffer(256); // this size seems to cover the basic health checks + try (BufferOutputStream outputStream = new BufferOutputStream(buffer);) { reporter.reportHealth(outputStream, health); - resp.end(Buffer.buffer(outputStream.toByteArray())); + resp.end(buffer); } catch (IOException e) { throw new UncheckedIOException(e); } } + + private static class BufferOutputStream extends OutputStream { + + private final Buffer buffer; + + private BufferOutputStream(Buffer buffer) { + this.buffer = buffer; + } + + @Override + public void write(byte[] b, int off, int len) throws IOException { + buffer.appendBytes(b, off, len); + } + + @Override + public void write(int b) throws IOException { + buffer.appendInt(b); + } + } }