perf(docker): decode completed log frames with JDK fast paths (#6525)

Use bulk fragment appends and direct UTF-8 decoding while preserving bounded line reassembly and callback lifecycle behavior.

Signed-off-by: Andre Kurait <AndreKurait@gmail.com>
This commit is contained in:
Andre Kurait authored and GitHub committed 2026-10-25 13:21:02 -04:00
1 parent 4a6e59fb3e
commit 57889bbb45
2 files changed
+133 -9

No files matched your search

@@ -20,6 +20,7 @@ import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Deque;
import java.util.EnumMap;
@@ -517,10 +518,29 @@ public class ContainerLogStreamer {
/** Appends a frame payload and returns the complete lines it produced (possibly none). */
List<String> append(byte[] payload) {
int size = buffer.size();
boolean fitsWithinLimit = payload.length <= MAX_LINE_BYTES - 3 - size;
// A complete frame safely below the cap needs no byte-level reassembly or UTF-8 tracking.
// Keep empty lines, including the one before a final newline, for the emitter to handle.
if (size == 0 && payload.length > 0 && fitsWithinLimit && payload[payload.length - 1] == '\n') {
return Arrays.asList(new String(payload, 0, payload.length - 1, StandardCharsets.UTF_8)
.split("\n", -1));
}
List<String> lines = new ArrayList<>();
for (byte b : payload) {
int start = 0;
for (int i = 0; i < payload.length; i++) {
byte b = payload[i];
if (b == '\n') {
lines.add(takeLine());
lines.add(takeLine(payload, start, i));
start = i + 1;
size = 0;
continue;
}
// Away from the cap, only the final four bytes can affect the next frame's UTF-8 state.
if (fitsWithinLimit) {
if (i >= payload.length - 4) {
trackUtf8(b);
}
continue;
}
// Force-split BEFORE a character that would push the buffer past the cap, so the emitted
@@ -528,20 +548,27 @@ public class ContainerLogStreamer {
// the next event whole instead of inflating this one. Only at a character boundary, and
// only for a real character start (not a continuation byte).
if (pendingContinuation == 0 && (b & 0xC0) != 0x80
&& buffer.size() > 0
&& buffer.size() + utf8CharLength(b) > MAX_LINE_BYTES) {
lines.add(takeLine());
&& size > 0
&& size + utf8CharLength(b) > MAX_LINE_BYTES) {
lines.add(takeLine(payload, start, i));
start = i;
size = 0;
}
buffer.write(b);
size++;
trackUtf8(b);
if ((buffer.size() >= MAX_LINE_BYTES && pendingContinuation == 0)
|| buffer.size() >= MAX_LINE_BYTES + 3) {
if ((size >= MAX_LINE_BYTES && pendingContinuation == 0)
|| size >= MAX_LINE_BYTES + 3) {
// Do not split a valid character whose continuation bytes arrive in a later frame.
// Malformed input may never reach a boundary, so allow at most the three continuation
// bytes a valid UTF-8 character could need before forcing the buffer to drain.
lines.add(takeLine());
lines.add(takeLine(payload, start, i + 1));
start = i + 1;
size = 0;
}
}
if (start < payload.length) {
buffer.write(payload, start, payload.length - start);
}
return lines;
}
@@ -585,6 +612,15 @@ public class ContainerLogStreamer {
}
}
private String takeLine(byte[] payload, int start, int end) {
if (!hasPendingBytes()) {
pendingContinuation = 0;
return new String(payload, start, end - start, StandardCharsets.UTF_8);
}
buffer.write(payload, start, end - start);
return takeLine();
}
private String takeLine() {
String line = buffer.toString(StandardCharsets.UTF_8);
buffer.reset();