From ace3f8efcfbf132bf0c143de8a5182dafc69fdcd Mon Sep 17 00:00:00 2001 From: He-Pin Date: Sat, 23 May 2026 23:39:05 +0800 Subject: [PATCH] perf: buffer native stdout writes Motivation: Scala Native kube-prometheus rendering still showed write overhead after the renderer/import stack. NativeOutputStream already writes through fwrite, but stdout used the platform default stdio buffer. Modification: Configure NativeOutputStream with full C stdio buffering via setvbuf before rendering, using a 256 KiB buffer size. Result: Native kube-prometheus A/B on the 0.5.12 stack improved in both orders: forward 218.848 ms clean vs 188.528 ms candidate; reverse 183.701 ms candidate vs 224.045 ms clean. Output matched by cmp; clean PR branch reformat, full tests, and bench regressions passed. --- sjsonnet/src-native/sjsonnet/NativeOutputStream.scala | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/sjsonnet/src-native/sjsonnet/NativeOutputStream.scala b/sjsonnet/src-native/sjsonnet/NativeOutputStream.scala index 45d28ebb..d8a85ea4 100644 --- a/sjsonnet/src-native/sjsonnet/NativeOutputStream.scala +++ b/sjsonnet/src-native/sjsonnet/NativeOutputStream.scala @@ -13,6 +13,8 @@ import scala.scalanative.unsigned._ * Uses stdio.fwrite which has internal C library buffering, avoiding per-call syscall overhead. */ class NativeOutputStream(file: Ptr[FILE]) extends OutputStream { + stdio.setvbuf(file, null, stdio._IOFBF, NativeOutputStream.BufferSize.toUSize) + override def write(b: Int): Unit = stdio.fputc(b, file) @@ -27,3 +29,7 @@ class NativeOutputStream(file: Ptr[FILE]) extends OutputStream { override def close(): Unit = flush() } + +object NativeOutputStream { + private final val BufferSize = 256 * 1024 +}