&'ctx str 解析为 Value<'ctx> 尝试引用原始字节,而不是将它们复制到字符串中来减少分配,从而实现更快的 JSON 反序列化。对于ndjson类型的 JSON, serde_json_borrow的解析速度提高了 2 倍。Running benches/crit_bench.rs (/home/pascal/cargo_target_dir/release/deps/crit_bench-fd2d661e0b4255c5)
flat-json-to-doc/serde-json-owned
time: [352.60 µs 353.40 µs 354.26 µs]
thrpt: [236.01 MiB/s 236.59 MiB/s 237.12 MiB/s]
flat-json-to-doc/serde-json-borrowed
time: [175.53 µs 175.72 µs 175.93 µs]
thrpt: [475.23 MiB/s 475.81 MiB/s 476.34 MiB/s]
fn counter(count: &mut i32) -> impl View<i32> {
container((
text(count.to_string()),
container((
button(text("More"), |count| *count += 1),
button(text("Less"), |count| *count -= 1),
))
))
}
pub trait View<T> {
type Widget: Widget;
fn is_changed(&self, last: &Self) -> bool;
fn handle(&mut self, state: &mut T, cx: EventContext<Self::Widget>);
fn build(&mut self) -> Self::Widget;
fn rebuild(&mut self, last: Self, widget: Self::Widget) -> Self::Widget
where
Self: Sized,
{
if self.is_changed(&last) {
widget
} else {
self.build()
}
}
}
pub trait Widget {
type Layout: Layout;
fn paint(&self, context: PaintContext<Self>)
where
Self: Sized;
fn layout(&mut self, taffy: &mut Taffy, scale_factor: f32) -> Self::Layout;
}
原文作者:Rust语言中文社区;如有侵权,请联系管理员删除。