Skip to content

Commit

Permalink
Drop ShapePlanCache
Browse files Browse the repository at this point in the history
The ShapePlanCache was added to improve performance when shaping the same
strings over and over. However, it never had the ability to be trimmed
and when it was moved to FontSystem, this created a permanently growing
allocation. It is recommended to instead use the shape-run-cache feature
which supports trimming if it is desired to have higher performance for
repeated shaping, at the cost of manually specifying when to trim.
  • Loading branch information
jackpot51 committed Nov 7, 2024
1 parent 4fe90bb commit 1f4065c
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 113 deletions.
9 changes: 3 additions & 6 deletions src/font/fallback/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use alloc::vec::Vec;
use fontdb::Family;
use unicode_script::Script;

use crate::{Font, FontMatchKey, FontSystem, ShapeBuffer, ShapePlanCache};
use crate::{Font, FontMatchKey, FontSystem, ShapeBuffer};

use self::platform::*;

Expand Down Expand Up @@ -117,11 +117,8 @@ impl<'a> FontFallbackIter<'a> {
}
}

pub fn shape_caches(&mut self) -> (&mut ShapeBuffer, &mut ShapePlanCache) {
(
&mut self.font_system.shape_buffer,
&mut self.font_system.shape_plan_cache,
)
pub fn shape_caches(&mut self) -> &mut ShapeBuffer {
&mut self.font_system.shape_buffer
}

fn face_contains_family(&self, id: fontdb::ID, family_name: &str) -> bool {
Expand Down
6 changes: 1 addition & 5 deletions src/font/system.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Attrs, Font, FontMatchAttrs, HashMap, ShapeBuffer, ShapePlanCache};
use crate::{Attrs, Font, FontMatchAttrs, HashMap, ShapeBuffer};
use alloc::collections::BTreeSet;
use alloc::string::String;
use alloc::sync::Arc;
Expand Down Expand Up @@ -103,9 +103,6 @@ pub struct FontSystem {
/// Cache for font matches.
font_matches_cache: HashMap<FontMatchAttrs, Arc<Vec<FontMatchKey>>>,

/// Cache for rustybuzz shape plans.
pub(crate) shape_plan_cache: ShapePlanCache,

/// Scratch buffer for shaping and laying out.
pub(crate) shape_buffer: ShapeBuffer,

Expand Down Expand Up @@ -177,7 +174,6 @@ impl FontSystem {
font_cache: Default::default(),
font_matches_cache: Default::default(),
font_codepoint_support_info_cache: Default::default(),
shape_plan_cache: ShapePlanCache::default(),
monospace_fallbacks_buffer: BTreeSet::default(),
#[cfg(feature = "shape-run-cache")]
shape_run_cache: crate::ShapeRunCache::default(),
Expand Down
3 changes: 0 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,6 @@ mod line_ending;
pub use self::shape::*;
mod shape;

use self::shape_plan_cache::*;
mod shape_plan_cache;

pub use self::shape_run_cache::*;
mod shape_run_cache;

Expand Down
81 changes: 37 additions & 44 deletions src/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use unicode_segmentation::UnicodeSegmentation;
use crate::fallback::FontFallbackIter;
use crate::{
math, Align, AttrsList, CacheKeyFlags, Color, Font, FontSystem, LayoutGlyph, LayoutLine,
Metrics, ShapePlanCache, Wrap,
Metrics, Wrap,
};

/// The shaping strategy of some text.
Expand Down Expand Up @@ -106,7 +106,6 @@ impl fmt::Debug for ShapeBuffer {

fn shape_fallback(
scratch: &mut ShapeBuffer,
shape_plan_cache: &mut ShapePlanCache,
glyphs: &mut Vec<ShapeGlyph>,
font: &Font,
line: &str,
Expand Down Expand Up @@ -141,8 +140,14 @@ fn shape_fallback(
let rtl = matches!(buffer.direction(), rustybuzz::Direction::RightToLeft);
assert_eq!(rtl, span_rtl);

let shape_plan = shape_plan_cache.get(font, &buffer);
let glyph_buffer = rustybuzz::shape_with_plan(font.rustybuzz(), shape_plan, buffer);
let shape_plan = rustybuzz::ShapePlan::new(
font.rustybuzz(),
buffer.direction(),
Some(buffer.script()),
buffer.language().as_ref(),
&[],
);
let glyph_buffer = rustybuzz::shape_with_plan(font.rustybuzz(), &shape_plan, buffer);
let glyph_infos = glyph_buffer.glyph_infos();
let glyph_positions = glyph_buffer.glyph_positions();

Expand Down Expand Up @@ -258,17 +263,9 @@ fn shape_run(

let glyph_start = glyphs.len();
let mut missing = {
let (scratch, shape_plan_cache) = font_iter.shape_caches();
let scratch = font_iter.shape_caches();
shape_fallback(
scratch,
shape_plan_cache,
glyphs,
&font,
line,
attrs_list,
start_run,
end_run,
span_rtl,
scratch, glyphs, &font, line, attrs_list, start_run, end_run, span_rtl,
)
};

Expand All @@ -284,10 +281,9 @@ fn shape_run(
font_iter.face_name(font.id())
);
let mut fb_glyphs = Vec::new();
let (scratch, shape_plan_cache) = font_iter.shape_caches();
let scratch = font_iter.shape_caches();
let fb_missing = shape_fallback(
scratch,
shape_plan_cache,
&mut fb_glyphs,
&font,
line,
Expand Down Expand Up @@ -456,34 +452,31 @@ fn shape_skip(
let ascent = metrics.ascent / f32::from(metrics.units_per_em);
let descent = metrics.descent / f32::from(metrics.units_per_em);

glyphs.extend(
line[start_run..end_run]
.char_indices()
.enumerate()
.map(|(i, (chr_idx, codepoint))| {
let glyph_id = charmap.map(codepoint);
let x_advance = glyph_metrics.advance_width(glyph_id);
let attrs = attrs_list.get_span(start_run + chr_idx);

ShapeGlyph {
start: i,
end: i + 1,
x_advance,
y_advance: 0.0,
x_offset: 0.0,
y_offset: 0.0,
ascent,
descent,
font_monospace_em_width,
font_id,
glyph_id,
color_opt: attrs.color_opt,
metadata: attrs.metadata,
cache_key_flags: attrs.cache_key_flags,
metrics_opt: attrs.metrics_opt.map(|x| x.into()),
}
}),
);
glyphs.extend(line[start_run..end_run].char_indices().enumerate().map(
|(i, (chr_idx, codepoint))| {
let glyph_id = charmap.map(codepoint);
let x_advance = glyph_metrics.advance_width(glyph_id);
let attrs = attrs_list.get_span(start_run + chr_idx);

ShapeGlyph {
start: i,
end: i + 1,
x_advance,
y_advance: 0.0,
x_offset: 0.0,
y_offset: 0.0,
ascent,
descent,
font_monospace_em_width,
font_id,
glyph_id,
color_opt: attrs.color_opt,
metadata: attrs.metadata,
cache_key_flags: attrs.cache_key_flags,
metrics_opt: attrs.metrics_opt.map(|x| x.into()),
}
},
));
}

/// A shaped glyph
Expand Down
55 changes: 0 additions & 55 deletions src/shape_plan_cache.rs

This file was deleted.

0 comments on commit 1f4065c

Please sign in to comment.