From 600c8c92ce56b0862212386d6e4fe99c7a4ccfd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Fri, 18 Jul 2025 13:47:45 +0800 Subject: [PATCH] =?UTF-8?q?perf(serialization):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E9=A2=9C=E8=89=B2=E6=A8=A1=E5=9D=97=E7=9A=84=E5=BA=8F=E5=88=97?= =?UTF-8?q?=E5=8C=96=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 使用直接序列化结构代替中间JSON对象,减少内存分配和转换开销 --- .../src/schemes/q_style_2/color_set.rs | 22 ++++++++-------- color-module/src/schemes/q_style_2/swatch.rs | 25 +++++++++++-------- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/color-module/src/schemes/q_style_2/color_set.rs b/color-module/src/schemes/q_style_2/color_set.rs index f67ed92..ac23825 100644 --- a/color-module/src/schemes/q_style_2/color_set.rs +++ b/color-module/src/schemes/q_style_2/color_set.rs @@ -3,8 +3,7 @@ use std::sync::Arc; use linked_hash_map::LinkedHashMap; use palette::{Oklch, color_difference::Wcag21RelativeContrast, luma::Luma}; -use serde::Serialize; -use serde_json::json; +use serde::{Serialize, ser::SerializeStruct}; use crate::{ convert::{map_oklch_to_luma, map_oklch_to_srgb_hex}, @@ -281,15 +280,14 @@ impl Serialize for ColorSet { let on_root = map_oklch_to_srgb_hex(&self.on_root); let on_disabled = map_oklch_to_srgb_hex(&self.on_disabled); - let color_set_object = json!({ - "root": root, - "hover": hover, - "active": active, - "focus": focus, - "disabled": disabled, - "onRoot": on_root, - "onDisabled": on_disabled, - }); - color_set_object.serialize(serializer) + let mut state = serializer.serialize_struct("ColorSet", 6)?; + state.serialize_field("root", &root)?; + state.serialize_field("hover", &hover)?; + state.serialize_field("active", &active)?; + state.serialize_field("focus", &focus)?; + state.serialize_field("disabled", &disabled)?; + state.serialize_field("onRoot", &on_root)?; + state.serialize_field("onDisabled", &on_disabled)?; + state.end() } } diff --git a/color-module/src/schemes/q_style_2/swatch.rs b/color-module/src/schemes/q_style_2/swatch.rs index 453cf34..ce906af 100644 --- a/color-module/src/schemes/q_style_2/swatch.rs +++ b/color-module/src/schemes/q_style_2/swatch.rs @@ -1,7 +1,7 @@ +use internment::Intern; use linked_hash_map::LinkedHashMap; use palette::Oklch; -use serde::Serialize; -use serde_json::Value; +use serde::{Serialize, ser::SerializeStruct}; use crate::convert::map_oklch_to_srgb_hex; @@ -77,20 +77,23 @@ impl Swatch { } } +fn get_static_str(s: &str) -> &'static str { + Intern::new(s.to_string()).as_ref() +} + impl Serialize for Swatch { fn serialize(&self, serializer: S) -> Result where S: serde::Serializer, { - let swatch_object = SWATCH_LIGHTINGS - .iter() - .map(|l| { - let color = self.get_hex(*l); - let key = format!("{l:02}"); - (key, color) - }) - .collect::(); + let mut state = serializer.serialize_struct("Swatch", SWATCH_LIGHTINGS.len())?; - swatch_object.serialize(serializer) + for l in SWATCH_LIGHTINGS { + let color = self.get_hex(l); + let key: &'static str = get_static_str(&format!("{l:02}")); + state.serialize_field(key, &color)?; + } + + state.end() } }