add extensive caching
| -rw-r--r-- | Cargo.lock | 94 | ||||
| -rw-r--r-- | Cargo.toml | 3 | ||||
| -rw-r--r-- | src/lib.rs | 54 |
3 files changed, 99 insertions, 52 deletions
@@ -21,6 +21,12 @@ dependencies = [ ] [[package]] +name = "array_chunks" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75892c1bdf5a3e474bbab36fa7f08d1bcc49ce35bd08fdd44cd761805b5865ac" + +[[package]] name = "atools" version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -40,9 +46,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "bitflags" -version = "2.9.1" +version = "2.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b8e56985ec62d17e9c1001dc89c88ecd7dc08e47eba5ec7c29c7b5eeecde967" +checksum = "6a65b545ab31d687cff52899d4890855fec459eb6afe0da6417b8a18da87aa29" [[package]] name = "bumpalo" @@ -52,18 +58,18 @@ checksum = "46c5e41b57b8bba42a04676d81cb89e9ee8e859a1a66f80a5a72e1cb76b34d43" [[package]] name = "bytemuck" -version = "1.23.1" +version = "1.23.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c76a5792e44e4abe34d3abf15636779261d45a7450612059293d1d2cfc63422" +checksum = "3995eaeebcdf32f91f980d360f78732ddc061097ab4e39991ae7a6ace9194677" dependencies = [ "bytemuck_derive", ] [[package]] name = "bytemuck_derive" -version = "1.10.0" +version = "1.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "441473f2b4b0459a68628c744bc61d23e730fb00128b841d30fa4bb3972257e4" +checksum = "4f154e572231cb6ba2bd1176980827e3d5dc04cc183a75dea38109fbdd672d29" dependencies = [ "proc-macro2", "quote", @@ -72,9 +78,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.2.30" +version = "1.2.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "deec109607ca693028562ed836a5f1c4b8bd77755c4e132fc5ce11b0b6211ae7" +checksum = "3ee0f8803222ba5a7e2777dd72ca451868909b1ac410621b676adf07280e9b5f" dependencies = [ "shlex", ] @@ -125,12 +131,15 @@ checksum = "75b325c5dbd37f80359721ad39aca5a29fb04c89279657cffdda8736d0c0b9d2" name = "dsb" version = "0.1.0" dependencies = [ + "array_chunks", "atools", "fimg", "implicit-fn", "lower", + "lru-cache", "swash", "swizzle", + "umath", ] [[package]] @@ -166,8 +175,9 @@ checksum = "6a02dba6a60cd31533cf16561ced53239686d18f1464bff49579dd320fcea081" [[package]] name = "fimg" -version = "0.4.45" +version = "0.4.46" dependencies = [ + "array_chunks", "atools", "clipline", "fer", @@ -370,9 +380,9 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" [[package]] name = "libc" -version = "0.2.174" +version = "0.2.175" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1171693293099992e19cddea4e8b849964e9846f4acee11b3948bcc337be8776" +checksum = "6a82ae493e598baaea5209805c49bbf2ea7de956d50d7da0da1164f9c6d28543" [[package]] name = "libloading" @@ -381,21 +391,27 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "07033963ba89ebaf1584d767badaa2e8fcec21aedea6b8c0346d487d49c28667" dependencies = [ "cfg-if", - "windows-targets 0.53.2", + "windows-targets 0.53.3", ] [[package]] name = "libredox" -version = "0.1.6" +version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4488594b9328dee448adb906d8b126d9b7deb7cf5c22161ee591610bb1be83c0" +checksum = "391290121bad3d37fbddad76d8f5d1c1c314cfc646d143d7e07a3086ddff0ce3" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.9.2", "libc", "redox_syscall", ] [[package]] +name = "linked-hash-map" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f" + +[[package]] name = "linux-raw-sys" version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -428,6 +444,15 @@ dependencies = [ ] [[package]] +name = "lru-cache" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31e24f1ad8321ca0e8a1e0ac13f23cb668e6f5466c2c57319f6a5cf1cc8e3b1c" +dependencies = [ + "linked-hash-map", +] + +[[package]] name = "mattr" version = "0.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -547,9 +572,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.95" +version = "1.0.101" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778" +checksum = "89ae43fd86e4158d6db51ad8e2b80f313af9cc74f5c0e03ccb87de09998732de" dependencies = [ "unicode-ident", ] @@ -599,11 +624,11 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.5.16" +version = "0.5.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7251471db004e509f4e75a62cca9435365b5ec7bcdff530d612ac7c87c44a792" +checksum = "5407465600fb0548f1442edf71dd20683c6ed326200ace4b1ef0763521bb3b77" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.9.2", ] [[package]] @@ -612,7 +637,7 @@ version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "11181fbabf243db407ef8df94a6ce0b2f9a733bd8be4ad02b4eda9602296cac8" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.9.2", "errno", "libc", "linux-raw-sys", @@ -621,9 +646,9 @@ dependencies = [ [[package]] name = "rustversion" -version = "1.0.21" +version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a0d197bd2c9dc6e53b84da9556a69ba4cdfab8619eb41a8bd1cc2027a0f6b1d" +checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" [[package]] name = "sdl2" @@ -692,9 +717,9 @@ dependencies = [ [[package]] name = "slab" -version = "0.4.10" +version = "0.4.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04dc19736151f35336d325007ac991178d504a119863a2fcb3758cdb5e52c50d" +checksum = "7a2ae44ef20feb57a68b23d846850f861394c2e02dc425a50098ae8c90267589" [[package]] name = "smallvec" @@ -721,9 +746,9 @@ checksum = "9cd32a3a5640a1e8ba59937591c61be77cae746e7c0fa081b24deebb4f7ece4c" [[package]] name = "syn" -version = "2.0.104" +version = "2.0.106" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "17b6f705963418cdb9927482fa304bc562ece2fdd4f616084c50b7023b435a40" +checksum = "ede7c438028d4436d71104916910f5bb611972c5cfd7f89b8300a8186e6fada6" dependencies = [ "proc-macro2", "quote", @@ -985,6 +1010,12 @@ dependencies = [ ] [[package]] +name = "windows-link" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e6ad25900d524eaabdbbb96d20b4311e1e7ae1699af4fb28c17ae66c80d798a" + +[[package]] name = "windows-result" version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1008,7 +1039,7 @@ version = "0.60.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f2f500e4d28234f72040990ec9d39e3a6b950f9f22d3dba18416c35882612bcb" dependencies = [ - "windows-targets 0.53.2", + "windows-targets 0.53.3", ] [[package]] @@ -1029,10 +1060,11 @@ dependencies = [ [[package]] name = "windows-targets" -version = "0.53.2" +version = "0.53.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c66f69fcc9ce11da9966ddb31a40968cad001c5bedeb5c2b82ede4253ab48aef" +checksum = "d5fe6031c4041849d7c496a8ded650796e7b6ecc19df1a431c1a363342e5dc91" dependencies = [ + "windows-link", "windows_aarch64_gnullvm 0.53.0", "windows_aarch64_msvc 0.53.0", "windows_i686_gnu 0.53.0", @@ -1145,7 +1177,7 @@ version = "0.39.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1" dependencies = [ - "bitflags 2.9.1", + "bitflags 2.9.2", ] [[package]] @@ -4,13 +4,16 @@ version = "0.1.0" edition = "2024" [dependencies] +array_chunks = "1.0.0" atools = "0.1.6" fimg = { path = "../fimg", features = ["real-show", "save"] } # fimg = { git = "https://github.com/bend-n/fimg" } implicit-fn = "0.1.0" lower = "0.2.0" +lru-cache = "0.1.2" swash = "0.2.5" swizzle = "0.1.0" +umath = "0.0.7" [[test]] name = "test" @@ -1,6 +1,5 @@ #![allow(incomplete_features)] #![feature( - array_chunks, super_let, debug_closure_helpers, const_trait_impl, @@ -15,8 +14,10 @@ #![allow(unsafe_op_in_unsafe_fn)] use std::iter::{successors, zip}; pub mod cell; +use array_chunks::*; use atools::{ArrayTools, Join}; use fimg::{Image, OverlayAt}; +use lru_cache::LruCache; use swash::scale::{Render, ScaleContext, Source}; use swash::shape::ShapeContext; use swash::shape::cluster::Glyph; @@ -24,15 +25,18 @@ use swash::text::cluster::{CharCluster, Parser, Token}; use swash::text::{Codepoint, Script}; use swash::zeno::Format; use swash::{FontRef, Instance}; +use umath::FF32; pub use crate::cell::Cell; use crate::cell::Style; - +#[derive(Clone)] pub struct Fonts<'a, 'b, 'c, 'd> { regular: F<'a>, bold: F<'b>, italic: F<'c>, bold_italic: F<'d>, + + cache: LruCache<(u8, FF32, u16), swash::scale::image::Image>, } #[derive(Clone, Copy)] pub enum F<'a> { @@ -83,6 +87,7 @@ impl<'a, 'b, 'c, 'd> Fonts<'a, 'b, 'c, 'd> { bold: bold.into(), italic: italic.into(), bold_italic: bold_italic.into(), + cache: LruCache::new(3000), } } } @@ -92,7 +97,7 @@ pub unsafe fn render( (c, r): (usize, usize), ppem: f32, bgcolor: [u8; 3], - fonts: Fonts, + fonts: &mut Fonts, line_spacing: f32, subpixel: bool, ) -> Image<Box<[u8]>, 3> { @@ -207,27 +212,34 @@ pub unsafe fn render( // }; // } if let Some(_) = cell.letter { - let f = match cell.style.flags { + let (key, f) = match cell.style.flags { f if (f & Style::BOLD != 0) & (f & Style::ITALIC != 0) => - fonts.bold_italic, - f if f & Style::BOLD != 0 => fonts.bold, - f if f & Style::ITALIC != 0 => fonts.italic, - _ => fonts.regular, + (3, fonts.bold_italic), + f if f & Style::BOLD != 0 => (2, fonts.bold), + f if f & Style::ITALIC != 0 => (1, fonts.italic), + _ => (0, fonts.regular), }; let id = glyph.id; - let mut scbd = ScaleContext::new(); - let scbd = - scbd.builder(*f).variations(f.variations()).size(ppem); + let key = (key, FF32::new(ppem), id); - let x = Render::new(&[Source::Outline]) - .format(if subpixel { - Format::Subpixel - } else { - Format::Alpha - }) - .render(&mut scbd.build(), id) - .unwrap(); + if !fonts.cache.contains_key(&key) { + let mut scbd = ScaleContext::new(); + let scbd = scbd + .builder(*f) + .variations(f.variations()) + .size(ppem); + let x = Render::new(&[Source::Outline]) + .format(if subpixel { + Format::Subpixel + } else { + Format::Alpha + }) + .render(&mut scbd.build(), id) + .unwrap(); + fonts.cache.insert(key, x); + }; + let x = fonts.cache.get_mut(&key).unwrap(); if x.placement.width == 0 { continue; @@ -249,8 +261,8 @@ pub unsafe fn render( ) .buf(&x.data), (x_, y_), - color.map(_ as u32), - cell.style.bg.map(_ as u32), + color.map(_ as _), + cell.style.bg.map(_ as _), ); } else { i.as_mut().blend_alpha_and_color_at( |