[refactor](trx-frontend-http): pre-allocate spectrum encoding output

Replace format! with pre-allocated String::with_capacity for spectrum
frame encoding, reducing allocation overhead in the hot SSE path.

https://claude.ai/code/session_01XzurkeuUmamBuhQwxVy7T4
Signed-off-by: Claude <noreply@anthropic.com>
This commit is contained in:
Claude
2026-03-26 06:26:44 +00:00
committed by Stan Grams
parent adf65ae56d
commit 71c23f0895
@@ -71,13 +71,22 @@ fn base64_encode(data: &[u8]) -> String {
/// RDS is intentionally excluded — it changes rarely and is sent via the
/// `/events` state stream instead.
fn encode_spectrum_frame(frame: &trx_core::rig::state::SpectrumData) -> String {
let bytes: Vec<u8> = frame
// Encode directly from the iterator to avoid an intermediate Vec<u8>.
let clamped: Vec<u8> = frame
.bins
.iter()
.map(|&v| v.round().clamp(-128.0, 127.0) as i8 as u8)
.collect();
let b64 = base64_encode(&bytes);
format!("{},{},{b64}", frame.center_hz, frame.sample_rate)
let b64 = base64_encode(&clamped);
// Pre-allocate: header digits + 2 commas + base64 body.
let mut out = String::with_capacity(40 + b64.len());
out.push_str(&frame.center_hz.to_string());
out.push(',');
out.push_str(&frame.sample_rate.to_string());
out.push(',');
out.push_str(&b64);
out
}
#[derive(serde::Serialize)]