From 573b6522f9eb101cee93989ac3788a73e1e62c5d Mon Sep 17 00:00:00 2001 From: klensy Date: Sun, 14 Jan 2024 14:50:19 +0300 Subject: [PATCH 1/3] remove useless alloc on rust reference book this reduces total allocs from 490mb to 474mb: ==23272== Total: 490,538,699 bytes in 1,760,117 blocks ==23272== At t-gmax: 13,872,954 bytes in 4,655 blocks ==23272== At t-end: 488,516 bytes in 884 blocks ==23272== Reads: 830,509,060 bytes ==23272== Writes: 522,290,614 bytes to ==40876== Total: 474,156,323 bytes in 1,521,025 blocks ==40876== At t-gmax: 13,872,954 bytes in 4,655 blocks ==40876== At t-end: 488,516 bytes in 884 blocks ==40876== Reads: 820,933,434 bytes ==40876== Writes: 514,838,350 bytes --- src/renderer/html_handlebars/helpers/navigation.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/renderer/html_handlebars/helpers/navigation.rs b/src/renderer/html_handlebars/helpers/navigation.rs index 86b240ba..12c69027 100644 --- a/src/renderer/html_handlebars/helpers/navigation.rs +++ b/src/renderer/html_handlebars/helpers/navigation.rs @@ -103,7 +103,7 @@ fn find_chapter( } } - previous = Some(item.clone()); + previous = Some(item); } _ => continue, } From e3ad9d097ea2f7333e5d84f4690528c74149ed94 Mon Sep 17 00:00:00 2001 From: klensy Date: Sun, 14 Jan 2024 15:16:23 +0300 Subject: [PATCH 2/3] reduce useless regex allocs from 474mb to 215mb ==40876== Total: 474,156,323 bytes in 1,521,025 blocks ==40876== At t-gmax: 13,872,954 bytes in 4,655 blocks ==40876== At t-end: 488,516 bytes in 884 blocks ==40876== Reads: 820,933,434 bytes ==40876== Writes: 514,838,350 bytes to ==57763== Total: 215,292,393 bytes in 1,161,048 blocks ==57763== At t-gmax: 13,872,954 bytes in 4,655 blocks ==57763== At t-end: 1,210,783 bytes in 1,274 blocks ==57763== Reads: 598,542,892 bytes ==57763== Writes: 229,841,910 bytes --- src/renderer/html_handlebars/hbs_renderer.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index c701729f..b706108e 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -935,8 +935,9 @@ fn add_playground_pre( /// Modifies all `` blocks to convert "hidden" lines and to wrap them in /// a ``. fn hide_lines(html: &str, code_config: &Code) -> String { - let language_regex = Regex::new(r"\blanguage-(\w+)\b").unwrap(); - let hidelines_regex = Regex::new(r"\bhidelines=(\S+)").unwrap(); + static LANGUAGE_REGEX: Lazy = Lazy::new(|| Regex::new(r"\blanguage-(\w+)\b").unwrap()); + static HIDELINES_REGEX: Lazy = Lazy::new(|| Regex::new(r"\bhidelines=(\S+)").unwrap()); + CODE_BLOCK_RE .replace_all(html, |caps: &Captures<'_>| { let text = &caps[1]; @@ -951,12 +952,12 @@ fn hide_lines(html: &str, code_config: &Code) -> String { ) } else { // First try to get the prefix from the code block - let hidelines_capture = hidelines_regex.captures(classes); + let hidelines_capture = HIDELINES_REGEX.captures(classes); let hidelines_prefix = match &hidelines_capture { Some(capture) => Some(&capture[1]), None => { // Then look up the prefix by language - language_regex.captures(classes).and_then(|capture| { + LANGUAGE_REGEX.captures(classes).and_then(|capture| { code_config.hidelines.get(&capture[1]).map(|p| p.as_str()) }) } From 745f7c73136ef7c0319183d7592fe5ff8582df26 Mon Sep 17 00:00:00 2001 From: klensy Date: Mon, 15 Jan 2024 12:49:29 +0300 Subject: [PATCH 3/3] pathdiff only used with watch feature, so make it optional --- Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 44fc88e0..61749a89 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,7 +28,6 @@ log = "0.4.17" memchr = "2.5.0" opener = "0.6.1" pulldown-cmark = { version = "0.9.3", default-features = false } -pathdiff = "0.2.1" regex = "1.8.1" serde = { version = "1.0.163", features = ["derive"] } serde_json = "1.0.96" @@ -41,6 +40,7 @@ topological-sort = "0.2.2" notify = { version = "6.1.1", optional = true } notify-debouncer-mini = { version = "0.4.1", optional = true } ignore = { version = "0.4.20", optional = true } +pathdiff = { version = "0.2.1", optional = true } # Serve feature futures-util = { version = "0.3.28", optional = true } @@ -61,7 +61,7 @@ walkdir = "2.3.3" [features] default = ["watch", "serve", "search"] -watch = ["dep:notify", "dep:notify-debouncer-mini", "dep:ignore"] +watch = ["dep:notify", "dep:notify-debouncer-mini", "dep:ignore", "dep:pathdiff"] serve = ["dep:futures-util", "dep:tokio", "dep:warp"] search = ["dep:elasticlunr-rs", "dep:ammonia"]