Skip to content

perf(runtime): eliminate hot-path string allocations#710

Open
arsenz wants to merge 1 commit into
supabase:mainfrom
arsenz:perf/eliminate-string-allocs
Open

perf(runtime): eliminate hot-path string allocations#710
arsenz wants to merge 1 commit into
supabase:mainfrom
arsenz:perf/eliminate-string-allocs

Conversation

@arsenz

@arsenz arsenz commented Jun 22, 2026

Copy link
Copy Markdown

What kind of change does this PR introduce?

Performance optimization (perf)

What is the current behavior?

Currently, the Edge Runtime performs unnecessary heap allocations and UTF-8 validations on every incoming request in the hot-path:

  1. UserWorkerResponse allocates a new String for status_text on every response, even though the underlying hyper status reasons are static.
  2. Header parsing unconditionally performs .to_str() UTF-8 validation before mapping to Deno ByteString.
  3. The server abort logging loop redundantly calls req_uri.to_string().

These allocations cause memory allocator lock contention and CPU overhead under high concurrency.

Please link any relevant issues here.
Resolves #712

What is the new behavior?

This PR refactors these hot-paths to use zero-copy mechanisms to bypass the allocator:

  • Changed status_text in UserWorkerResponse from String to &'static str to map canonical hyper reasons directly to memory without allocation.
  • Refactored header mapping to map raw bytes directly to Deno ByteString, bypassing .to_str() UTF-8 validation overhead.
  • Replaced .to_string() allocation on request URIs during abort logging with dynamic Display formatting.

Additional context

Baseline load testing using k6/specs/simple.ts shows significant improvements after these changes:

  • Throughput: Increased from ~11,134 RPS to ~12,872 RPS (+15.6%)
  • Latency: Average request duration dropped from 1.06ms to 915µs (breaking the sub-millisecond barrier).
  • Consistency: p95 latency improved from 1.30ms to 1.15ms.

These changes are purely internal optimizations and do not alter any external APIs or behavior.

- Replaced `String` with `&'static str` for `status_text` in `UserWorkerResponse`.

- Bypassed UTF-8 validation overhead by mapping raw bytes to Deno `ByteString` for headers.

- Removed `req_uri.to_string()` allocation during abort logging in the server loop.

This optimization yielded a ~15.6% increase in throughput (RPS) and achieved sub-millisecond average latency during baseline k6 benchmarking.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Performance]: Eliminate redundant string allocations in request hot-path

1 participant