From 850b713fa8d53b0fe04bd8c5463ed02ee832c28d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B4=AE=E7=94=9F=EF=BC=88=E5=AD=90=E8=99=9A=EF=BC=89?= <2234839456@qq.com> Date: Thu, 5 Nov 2026 09:09:12 +0800 Subject: [PATCH] =?UTF-8?q?perf(rust):=20/api/svg=20=E8=BD=AE=E5=BB=93?= =?UTF-8?q?=E8=BD=AC=20path=20=E5=8D=95=E7=BC=93=E5=86=B2=E5=86=99?= =?UTF-8?q?=E5=85=A5=EF=BC=8C=E6=B6=88=E9=99=A4=E6=AF=8F=E7=82=B9=20String?= =?UTF-8?q?=20=E5=88=86=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - contour_to_path 直接写入预分配 String(write_num 内联),替代每点一次 format! 临时 String;2000 字极端场景冷路径 29.7ms → 16.7ms(~1.8×),典型 4~8 字场景本就 ~1.4ms(瓶颈为 HTTP 回环)不变 - 修复 color 参数缺省时 fill 为空的 bug(sanitize 默认色 333333) - 输出与 TS 版保持逐字节一致(cmp 验证) - 不做请求内并行:典型请求数字级计算量远小于线程调度开销,且服务已是连接级并行(每连接一线程),大文本罕见且首访后命中缓存 --- rust-spike/font/.stats.json | 2 +- rust-spike/src/svg.rs | 62 ++++++++++++++++++++++++------------- 2 files changed, 42 insertions(+), 22 deletions(-) diff --git a/rust-spike/font/.stats.json b/rust-spike/font/.stats.json index 6b52722..ca61553 100644 --- a/rust-spike/font/.stats.json +++ b/rust-spike/font/.stats.json @@ -1 +1 @@ -{"totalRequests":488,"subsetRequests":485,"subsetCacheHits":265,"totalChars":11097,"tempUploads":0,"offlineSubsets":0,"offlineDownloads":0} \ No newline at end of file +{"totalRequests":499,"subsetRequests":496,"subsetCacheHits":269,"totalChars":13515,"tempUploads":0,"offlineSubsets":0,"offlineDownloads":0} \ No newline at end of file diff --git a/rust-spike/src/svg.rs b/rust-spike/src/svg.rs index 92f6f9a..3728f57 100644 --- a/rust-spike/src/svg.rs +++ b/rust-spike/src/svg.rs @@ -9,19 +9,27 @@ use crate::cache::FontCacheError; use crate::{cache, http, misc, ttf, App}; /// 二次贝塞尔轮廓 → SVG path 片段(M/L/Q/Z;TrueType 隐含中点 on-curve 语义) -fn contour_to_path(contour: &[ttf::GlyfPt], parts: &mut Vec) { +/// 直接写入调用方缓冲:大文本(千字级)时避免每点一次 String 分配 +fn contour_to_path(contour: &[ttf::GlyfPt], out: &mut String) { if contour.len() < 2 { return; } let n = contour.len(); let mid = |a: &ttf::GlyfPt, b: &ttf::GlyfPt| (f64::from(a.x + b.x) / 2.0, f64::from(a.y + b.y) / 2.0); - // 数值格式化:整数不带小数点,节省体积 - let num = |v: f64| -> String { - if v.fract() == 0.0 { - format!("{}", v as i64) - } else { - format!("{v}") + // 数值内联写入:整数不带小数点,节省体积;c 为 NaN 表示仅两点指令(M/L) + let put = |out: &mut String, tag: char, a: f64, b: f64, c: f64, d: f64| { + out.push(tag); + out.push(' '); + write_num(out, a); + out.push(' '); + write_num(out, b); + if c.is_finite() { + out.push(' '); + write_num(out, c); + out.push(' '); + write_num(out, d); } + out.push(' '); }; // 找 on-curve 起点;全 off-curve(极罕见)用首尾中点作隐含起点 @@ -35,9 +43,8 @@ fn contour_to_path(contour: &[ttf::GlyfPt], parts: &mut Vec) { let (mx, my) = mid(&contour[n - 1], &contour[0]); (mx, my) }; - parts.push(format!("M {} {}", num(sx), num(sy))); + put(out, 'M', sx, sy, f64::NAN, f64::NAN); - let mut prev = (sx, sy); // 悬挂中的 off-curve 控制点 let mut ctrl: Option<(f64, f64)> = None; for k in 1..=n { @@ -45,26 +52,33 @@ fn contour_to_path(contour: &[ttf::GlyfPt], parts: &mut Vec) { let cur = (f64::from(p.x), f64::from(p.y)); if p.on_curve { match ctrl.take() { - Some(c) => parts.push(format!("Q {} {} {} {}", num(c.0), num(c.1), num(cur.0), num(cur.1))), - None => parts.push(format!("L {} {}", num(cur.0), num(cur.1))), + Some(c) => put(out, 'Q', c.0, c.1, cur.0, cur.1), + None => put(out, 'L', cur.0, cur.1, f64::NAN, f64::NAN), } - prev = cur; } else { if let Some(c) = ctrl { // 连续 off-curve:前控制点与本控制点的中点是隐含 on-curve 终点 let implied = ((c.0 + cur.0) / 2.0, (c.1 + cur.1) / 2.0); - parts.push(format!("Q {} {} {} {}", num(c.0), num(c.1), num(implied.0), num(implied.1))); - prev = implied; + put(out, 'Q', c.0, c.1, implied.0, implied.1); } ctrl = Some(cur); } } // 收尾:悬挂控制点指回起点 if let Some(c) = ctrl { - parts.push(format!("Q {} {} {} {}", num(c.0), num(c.1), num(sx), num(sy))); + put(out, 'Q', c.0, c.1, sx, sy); + } + out.push_str("Z "); +} + +/// 数值写入缓冲(整数不带小数点;字形坐标是整数或 .5 中点,无 NaN/Inf 路径) +fn write_num(out: &mut String, v: f64) { + use std::fmt::Write as _; + if v.fract() == 0.0 { + let _ = write!(out, "{}", v as i64); + } else { + let _ = write!(out, "{v}"); } - let _ = prev; - parts.push("Z".to_string()); } /// 参数安全化:颜色仅允许 3/6 位十六进制,防 SVG 注入 @@ -122,6 +136,8 @@ pub fn handle_svg( }; let size = size.clamp(12, 400); + // color 缺省/非法时回退默认色(对齐 TS 版 sanitizeColor) + let color = sanitize_color(&color); misc::mark_font_used(&font_path); misc::stats_record_subset(misc::utf16_len_utf8(text)); @@ -190,12 +206,16 @@ pub fn handle_svg( return Ok(close); } }; - let mut parts: Vec = Vec::new(); + let mut d = String::with_capacity(1024); for c in &contours { - contour_to_path(c, &mut parts); + contour_to_path(c, &mut d); } - if !parts.is_empty() { - paths.push((pen_x, parts.join(" "))); + /** 去掉末尾指令分隔空格,保持与 TS 版逐字节一致 */ + if d.ends_with(' ') { + d.pop(); + } + if !d.is_empty() { + paths.push((pen_x, d)); } pen_x += f64::from(font.advance_width(gid).unwrap_or_else(|| font.units_per_em.max(1) as u16)); }