From 7aa171150efe9b1bd69a68b8ce17a78115ec0285 Mon Sep 17 00:00:00 2001 From: Hitalo Souza Date: Fri, 3 Jul 2026 13:47:28 -0300 Subject: [PATCH] refactor(vanilla-epoll): extract shared emit_xcache helper for crud X-Cache framing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fold the two inlined X-Cache response framings (crud GET hit/miss) into one emit_xcache helper — byte-identical output, and identical to the vanilla-io_uring twin's emit_xcache, so both entries share one audited framing and diff cleanly. No behavior or performance change (validated: crud GET MISS/HIT bytes unchanged). Co-Authored-By: Claude Opus 4.8 (1M context) --- frameworks/vanilla-epoll/main.v | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/frameworks/vanilla-epoll/main.v b/frameworks/vanilla-epoll/main.v index 03855f12d..0e6ca07d0 100644 --- a/frameworks/vanilla-epoll/main.v +++ b/frameworks/vanilla-epoll/main.v @@ -236,6 +236,21 @@ fn write_resp(mut out []u8, ctype string, body string) { ws(mut out, body) } +// emit_xcache writes a complete 200 JSON response carrying an X-Cache: HIT|MISS header +// straight into `out` — the single framing shared by the crud GET hit/miss paths (and +// byte-identical to the vanilla-io_uring twin's emit_xcache, so the two entries diff +// cleanly). `body` is the render buffer (snapshotted under the read-lock on a HIT). +fn emit_xcache(mut out []u8, ctype string, body []u8, cache string) { + ws(mut out, 'HTTP/1.1 200 OK\r\nServer: vanilla\r\nX-Cache: ') + ws(mut out, cache) + ws(mut out, '\r\nContent-Type: ') + ws(mut out, ctype) + ws(mut out, '\r\nContent-Length: ') + wi(mut out, i64(body.len)) + ws(mut out, '\r\nConnection: keep-alive\r\n\r\n') + wb(mut out, body) +} + // emit_int writes a 200 whose body is a single integer, formatting it into the // reused per-worker scratch. The obvious `write_resp(.., n.str())` heap-allocates // an int->string on every request — a permanent leak under `-gc none` (e.g. the @@ -716,11 +731,7 @@ fn (mut w WorkerCtx) start_crud_get(mut out []u8, mut ac core.AsyncCtx, id int) } if hit { // Cache hit: answer synchronously, no DB round-trip. - ws(mut out, - 'HTTP/1.1 200 OK\r\nServer: vanilla\r\nX-Cache: HIT\r\nContent-Type: application/json\r\nContent-Length: ') - wi(mut out, i64(w.scratch.len)) - ws(mut out, '\r\nConnection: keep-alive\r\n\r\n') - wb(mut out, w.scratch) + emit_xcache(mut out, 'application/json', w.scratch, 'HIT') return .done } w.reset_params() @@ -758,11 +769,7 @@ fn (mut w WorkerCtx) render_crud_get(mut out []u8, res pg_async.Result, id int) slot.valid = true w.ro.crud_mu.unlock() } - ws(mut out, - 'HTTP/1.1 200 OK\r\nServer: vanilla\r\nX-Cache: MISS\r\nContent-Type: application/json\r\nContent-Length: ') - wi(mut out, i64(w.scratch.len)) - ws(mut out, '\r\nConnection: keep-alive\r\n\r\n') - wb(mut out, w.scratch) + emit_xcache(mut out, 'application/json', w.scratch, 'MISS') } fn (mut w WorkerCtx) start_crud_create(mut out []u8, mut ac core.AsyncCtx, req request_parser.HttpRequest) core.AsyncStep {