Skip to content

Commit

Permalink
Merge pull request #1934 from iced-rs/reuse-text-cache-entries
Browse files Browse the repository at this point in the history
Reuse entries in `text::Cache`
  • Loading branch information
hecrj authored Jun 27, 2023
2 parents af62ec1 + 00859c2 commit 8d65e40
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 83 deletions.
4 changes: 2 additions & 2 deletions core/src/renderer/null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ impl text::Renderer for Null {
_font: Font,
_bounds: Size,
_shaping: text::Shaping,
) -> (f32, f32) {
(0.0, 20.0)
) -> Size {
Size::new(0.0, 20.0)
}

fn hit_test(
Expand Down
6 changes: 3 additions & 3 deletions core/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ pub trait Renderer: crate::Renderer {
font: Self::Font,
bounds: Size,
shaping: Shaping,
) -> (f32, f32);
) -> Size;

/// Measures the width of the text as if it were laid out in a single line.
fn measure_width(
Expand All @@ -173,7 +173,7 @@ pub trait Renderer: crate::Renderer {
font: Self::Font,
shaping: Shaping,
) -> f32 {
let (width, _) = self.measure(
let bounds = self.measure(
content,
size,
LineHeight::Absolute(Pixels(size)),
Expand All @@ -182,7 +182,7 @@ pub trait Renderer: crate::Renderer {
shaping,
);

width
bounds.width
}

/// Tests whether the provided point is within the boundaries of text
Expand Down
10 changes: 4 additions & 6 deletions core/src/widget/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::mouse;
use crate::renderer;
use crate::text;
use crate::widget::Tree;
use crate::{Color, Element, Layout, Length, Pixels, Rectangle, Size, Widget};
use crate::{Color, Element, Layout, Length, Pixels, Rectangle, Widget};

use std::borrow::Cow;

Expand Down Expand Up @@ -139,18 +139,16 @@ where

let size = self.size.unwrap_or_else(|| renderer.default_size());

let bounds = limits.max();

let (width, height) = renderer.measure(
let bounds = renderer.measure(
&self.content,
size,
self.line_height,
self.font.unwrap_or_else(|| renderer.default_font()),
bounds,
limits.max(),
self.shaping,
);

let size = limits.resolve(Size::new(width, height));
let size = limits.resolve(bounds);

layout::Node::new(size)
}
Expand Down
2 changes: 1 addition & 1 deletion graphics/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub trait Text {
font: Font,
bounds: Size,
shaping: text::Shaping,
) -> (f32, f32);
) -> Size;

/// Tests whether the provided point is within the boundaries of [`Text`]
/// laid out with the given parameters, returning information about
Expand Down
2 changes: 1 addition & 1 deletion graphics/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ where
font: Font,
bounds: Size,
shaping: text::Shaping,
) -> (f32, f32) {
) -> Size {
self.backend().measure(
content,
size,
Expand Down
2 changes: 1 addition & 1 deletion renderer/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl backend::Text for Backend {
font: Font,
bounds: Size,
shaping: text::Shaping,
) -> (f32, f32) {
) -> Size {
delegate!(
self,
backend,
Expand Down
2 changes: 1 addition & 1 deletion tiny_skia/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@ impl backend::Text for Backend {
font: Font,
bounds: Size,
shaping: text::Shaping,
) -> (f32, f32) {
) -> Size {
self.text_pipeline.measure(
contents,
size,
Expand Down
108 changes: 69 additions & 39 deletions tiny_skia/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,10 @@ impl Pipeline {
shaping,
};

let (_, buffer) = self.cache.get_mut().allocate(font_system, key);
let (_, entry) = self.cache.get_mut().allocate(font_system, key);

let (total_lines, max_width) = buffer
.layout_runs()
.enumerate()
.fold((0, 0.0), |(_, max), (i, buffer)| {
(i + 1, buffer.line_w.max(max))
});

let total_height = total_lines as f32 * line_height * scale_factor;
let max_width = max_width * scale_factor;
let max_width = entry.bounds.width * scale_factor;
let total_height = entry.bounds.height * scale_factor;

let bounds = bounds * scale_factor;

Expand All @@ -94,7 +87,7 @@ impl Pipeline {

let mut swash = cosmic_text::SwashCache::new();

for run in buffer.layout_runs() {
for run in entry.buffer.layout_runs() {
for glyph in run.glyphs {
let physical_glyph = glyph.physical((x, y), scale_factor);

Expand Down Expand Up @@ -138,12 +131,12 @@ impl Pipeline {
font: Font,
bounds: Size,
shaping: Shaping,
) -> (f32, f32) {
) -> Size {
let mut measurement_cache = self.cache.borrow_mut();

let line_height = f32::from(line_height.to_absolute(Pixels(size)));

let (_, paragraph) = measurement_cache.allocate(
let (_, entry) = measurement_cache.allocate(
&mut self.font_system.borrow_mut(),
Key {
content,
Expand All @@ -155,14 +148,7 @@ impl Pipeline {
},
);

let (total_lines, max_width) = paragraph
.layout_runs()
.enumerate()
.fold((0, 0.0), |(_, max), (i, buffer)| {
(i + 1, buffer.line_w.max(max))
});

(max_width, line_height * total_lines as f32)
entry.bounds
}

pub fn hit_test(
Expand All @@ -180,7 +166,7 @@ impl Pipeline {

let line_height = f32::from(line_height.to_absolute(Pixels(size)));

let (_, paragraph) = measurement_cache.allocate(
let (_, entry) = measurement_cache.allocate(
&mut self.font_system.borrow_mut(),
Key {
content,
Expand All @@ -192,12 +178,22 @@ impl Pipeline {
},
);

let cursor = paragraph.hit(point.x, point.y)?;
let cursor = entry.buffer.hit(point.x, point.y)?;

Some(Hit::CharOffset(cursor.index))
}
}

fn measure(buffer: &cosmic_text::Buffer) -> Size {
let (width, total_lines) = buffer
.layout_runs()
.fold((0.0, 0usize), |(width, total_lines), run| {
(run.line_w.max(width), total_lines + 1)
});

Size::new(width, total_lines as f32 * buffer.metrics().line_height)
}

fn to_family(family: font::Family) -> cosmic_text::Family<'static> {
match family {
font::Family::Name(name) => cosmic_text::Family::Name(name),
Expand Down Expand Up @@ -354,12 +350,18 @@ impl GlyphCache {
}

struct Cache {
entries: FxHashMap<KeyHash, cosmic_text::Buffer>,
entries: FxHashMap<KeyHash, Entry>,
measurements: FxHashMap<KeyHash, KeyHash>,
recently_used: FxHashSet<KeyHash>,
hasher: HashBuilder,
trim_count: usize,
}

struct Entry {
buffer: cosmic_text::Buffer,
bounds: Size,
}

#[cfg(not(target_arch = "wasm32"))]
type HashBuilder = twox_hash::RandomXxHashBuilder64;

Expand All @@ -372,6 +374,7 @@ impl Cache {
fn new() -> Self {
Self {
entries: FxHashMap::default(),
measurements: FxHashMap::default(),
recently_used: FxHashSet::default(),
hasher: HashBuilder::default(),
trim_count: 0,
Expand All @@ -382,20 +385,14 @@ impl Cache {
&mut self,
font_system: &mut cosmic_text::FontSystem,
key: Key<'_>,
) -> (KeyHash, &mut cosmic_text::Buffer) {
let hash = {
let mut hasher = self.hasher.build_hasher();

key.content.hash(&mut hasher);
key.size.to_bits().hash(&mut hasher);
key.line_height.to_bits().hash(&mut hasher);
key.font.hash(&mut hasher);
key.bounds.width.to_bits().hash(&mut hasher);
key.bounds.height.to_bits().hash(&mut hasher);
key.shaping.hash(&mut hasher);

hasher.finish()
};
) -> (KeyHash, &mut Entry) {
let hash = key.hash(self.hasher.build_hasher());

if let Some(hash) = self.measurements.get(&hash) {
let _ = self.recently_used.insert(*hash);

return (*hash, self.entries.get_mut(hash).unwrap());
}

if let hash_map::Entry::Vacant(entry) = self.entries.entry(hash) {
let metrics = cosmic_text::Metrics::new(key.size, key.size * 1.2);
Expand All @@ -416,7 +413,24 @@ impl Cache {
to_shaping(key.shaping),
);

let _ = entry.insert(buffer);
let bounds = measure(&buffer);

let _ = entry.insert(Entry { buffer, bounds });

for bounds in [
bounds,
Size {
width: key.bounds.width,
..bounds
},
] {
if key.bounds != bounds {
let _ = self.measurements.insert(
Key { bounds, ..key }.hash(self.hasher.build_hasher()),
hash,
);
}
}
}

let _ = self.recently_used.insert(hash);
Expand All @@ -428,6 +442,8 @@ impl Cache {
if self.trim_count > Self::TRIM_INTERVAL {
self.entries
.retain(|key, _| self.recently_used.contains(key));
self.measurements
.retain(|_, value| self.recently_used.contains(value));

self.recently_used.clear();

Expand All @@ -448,4 +464,18 @@ struct Key<'a> {
shaping: Shaping,
}

impl Key<'_> {
fn hash<H: Hasher>(self, mut hasher: H) -> KeyHash {
self.content.hash(&mut hasher);
self.size.to_bits().hash(&mut hasher);
self.line_height.to_bits().hash(&mut hasher);
self.font.hash(&mut hasher);
self.bounds.width.to_bits().hash(&mut hasher);
self.bounds.height.to_bits().hash(&mut hasher);
self.shaping.hash(&mut hasher);

hasher.finish()
}
}

type KeyHash = u64;
2 changes: 1 addition & 1 deletion wgpu/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ impl backend::Text for Backend {
font: Font,
bounds: Size,
shaping: core::text::Shaping,
) -> (f32, f32) {
) -> Size {
self.text_pipeline.measure(
contents,
size,
Expand Down
Loading

0 comments on commit 8d65e40

Please sign in to comment.