[Perf] cache the function that used to calculate width and height of (#90)

dynamic text
pull/91/head
The Mist 2024-04-25 21:38:03 +08:00 committed by GitHub
parent 66206496f9
commit ef08881e1a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 161 additions and 14 deletions

157
generator/Cargo.lock generated
View File

@ -8,6 +8,18 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "ahash"
version = "0.8.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011"
dependencies = [
"cfg-if",
"once_cell",
"version_check",
"zerocopy",
]
[[package]]
name = "aho-corasick"
version = "1.1.2"
@ -17,6 +29,12 @@ dependencies = [
"memchr",
]
[[package]]
name = "allocator-api2"
version = "0.2.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c6cb57a04249c6480766f7f7cef5467412af1490f8d1e243141daddada3264f"
[[package]]
name = "arboard"
version = "3.3.2"
@ -87,7 +105,7 @@ dependencies = [
"regex",
"rustc-hash",
"shlex",
"syn",
"syn 2.0.52",
]
[[package]]
@ -120,6 +138,39 @@ version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]]
name = "cached"
version = "0.49.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e8e463fceca5674287f32d252fb1d94083758b8709c160efae66d263e5f4eba"
dependencies = [
"ahash",
"cached_proc_macro",
"cached_proc_macro_types",
"hashbrown",
"instant",
"once_cell",
"thiserror",
]
[[package]]
name = "cached_proc_macro"
version = "0.20.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad9f16c0d84de31a2ab7fdf5f7783c14631f7075cf464eb3bb43119f61c9cb2a"
dependencies = [
"darling",
"proc-macro2",
"quote",
"syn 1.0.109",
]
[[package]]
name = "cached_proc_macro_types"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ade8366b8bd5ba243f0a58f036cc0ca8a2f069cff1a2351ef1cac6b083e16fc0"
[[package]]
name = "cc"
version = "1.0.90"
@ -245,6 +296,41 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "darling"
version = "0.14.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b750cb3417fd1b327431a470f388520309479ab0bf5e323505daf0290cd3850"
dependencies = [
"darling_core",
"darling_macro",
]
[[package]]
name = "darling_core"
version = "0.14.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "109c1ca6e6b7f82cc233a97004ea8ed7ca123a9af07a8230878fcfda9b158bf0"
dependencies = [
"fnv",
"ident_case",
"proc-macro2",
"quote",
"strsim",
"syn 1.0.109",
]
[[package]]
name = "darling_macro"
version = "0.14.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4aab4dbc9f7611d8b55048a3a16d2d010c2c8334e46304b40ac1cc14bf3b48e"
dependencies = [
"darling_core",
"quote",
"syn 1.0.109",
]
[[package]]
name = "deranged"
version = "0.3.11"
@ -262,7 +348,7 @@ checksum = "d150dea618e920167e5973d70ae6ece4385b7164e0d799fe7c122dd0a5d912ad"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.52",
]
[[package]]
@ -386,7 +472,7 @@ checksum = "1a5c6c585bc94aaf2c7b51dd4c2ba22680844aba4c687be581871a6f518c5742"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.52",
]
[[package]]
@ -400,6 +486,7 @@ name = "generator"
version = "0.1.0"
dependencies = [
"arboard",
"cached",
"cosmic-text",
"nvim-oxi",
"regex",
@ -431,6 +518,10 @@ name = "hashbrown"
version = "0.14.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604"
dependencies = [
"ahash",
"allocator-api2",
]
[[package]]
name = "home"
@ -441,6 +532,12 @@ dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "ident_case"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
[[package]]
name = "image"
version = "0.24.9"
@ -465,6 +562,15 @@ dependencies = [
"hashbrown",
]
[[package]]
name = "instant"
version = "0.1.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c"
dependencies = [
"cfg-if",
]
[[package]]
name = "itoa"
version = "1.0.10"
@ -760,7 +866,7 @@ checksum = "7fa77de5791207f41bab307dadb91e66a2bbbd19433e4243ef98c9d45e03a007"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.52",
]
[[package]]
@ -1031,7 +1137,7 @@ checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.52",
]
[[package]]
@ -1053,7 +1159,7 @@ checksum = "0b2e6b945e9d3df726b65d6ee24060aff8e3533d431f677a9695db04eff9dfdb"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.52",
]
[[package]]
@ -1089,6 +1195,12 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6637bab7722d379c8b41ba849228d680cc12d0a45ba1fa2b48f2a30577a06731"
[[package]]
name = "strsim"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "swash"
version = "0.1.12"
@ -1100,6 +1212,17 @@ dependencies = [
"zeno",
]
[[package]]
name = "syn"
version = "1.0.109"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "syn"
version = "2.0.52"
@ -1171,7 +1294,7 @@ checksum = "c61f3ba182994efc43764a46c018c347bc492c79f024e705f46567b418f6d4f7"
dependencies = [
"proc-macro2",
"quote",
"syn",
"syn 2.0.52",
]
[[package]]
@ -1651,3 +1774,23 @@ name = "zeno"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd15f8e0dbb966fd9245e7498c7e9e5055d9e5c8b676b95bd67091cd11a1e697"
[[package]]
name = "zerocopy"
version = "0.7.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "74d4d3961e53fa4c9a25a8637fc2bfaf2595b3d3ae34875568a5cf64787716be"
dependencies = [
"zerocopy-derive",
]
[[package]]
name = "zerocopy-derive"
version = "0.7.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.52",
]

View File

@ -13,6 +13,7 @@ arboard = {features = ["wayland-data-control"], version = "3.3.2"}
thiserror = "1.0.58"
regex = "1.10.3"
two-face = "0.3.0"
cached = "0.49.3"
[lib]
crate-type = ["cdylib"]

View File

@ -1,3 +1,4 @@
use cached::proc_macro::cached;
use regex::Regex;
const MIN_WIDTH: f32 = 100.;
@ -12,6 +13,7 @@ fn min_width(width: f32) -> f32 {
// Because the code block is input by users, we need to calculate the width and height
// to make sure render the width and height of the "editor" shape correctly
#[cached(key = "String", convert = r#"{ format!("{}", text) }"#)]
pub fn calc_wh(text: &str, char_wdith: f32, line_height: f32) -> (f32, f32) {
let trimmed_text = prepare_code(text);
let lines = trimmed_text.lines();
@ -36,7 +38,7 @@ fn replace_tab_to_space(text: &str) -> String {
str::replace(text, "\t", &spaces)
}
// Find min indention of the line, and remove the same indention from subsequent lines
// Find min indention of code lines, and remove the same indention from subsequent lines
fn trim_space(text: &str) -> String {
let lines = text.split("\n").collect::<Vec<&str>>();
let regex = Regex::new(r"(?:^|\n)(\s*)").unwrap();

View File

@ -1,13 +1,10 @@
use std::sync::Arc;
use tiny_skia::Pixmap;
use crate::{config::TakeSnapshotParams, edges::edge::Edge};
use super::{
render_error,
style::{ComponentAlign, ComponentStyle, RawComponentStyle, Size, Style},
};
use crate::{config::TakeSnapshotParams, edges::edge::Edge};
use std::sync::Arc;
use tiny_skia::Pixmap;
pub struct ComponentContext {
pub scale_factor: f32,
@ -152,6 +149,8 @@ pub trait Component {
Ok(render_params.clone())
}
// Dynamic calculate width and height of children, if the children is empty, get_dynamic_wh
// will return (0., 0.)
fn get_dynamic_wh(&self) -> (f32, f32) {
let children = self.children();
let calc_children_wh = |cb: fn((f32, f32), &Box<dyn Component>) -> (f32, f32)| {
@ -160,11 +159,13 @@ pub trait Component {
let style = self.style();
match style.align {
// If align is row, width is sum of children width, height is max of children height
ComponentAlign::Row => calc_children_wh(|(w, h), child| {
let style = child.parsed_style();
(w + style.width, h.max(style.height))
}),
// If align is column, width is max of children width, height is sum of children height
ComponentAlign::Column => calc_children_wh(|(w, h), child| {
let style = child.parsed_style();