[harfrust] Cache the shaper view across shape calls

Shaper construction rebuilds the OtTables/AatTables view -- ~800
instructions of dependent-load-heavy table parsing -- on every shape
call, while the heavy ShaperData underneath is already cached on the
font instance. Keep the constructed Shaper in the per-font data,
keyed on hb_font_t's variation-coords serial passed down from
hb-harfrust.cc, rebuilding both the coords snapshot and the view only
when the serial changes. This also fixes the instance's coords
snapshot going stale if variations change after font-data creation.

Lock-free by hb_font_t's contract: font parameters don't change while
shaping calls are in flight, so replacing the cached entry on a
serial change never races a reader; the only true race is two threads
building the first entry, resolved with a compare-exchange.

Measured (clang release rig, harfrust shaper): Roboto/en-words -6.3%
instructions / -8..-10% cycles, Roboto/thelittleprince -1.0%/-3%,
Devanagari Sangam/hi-words -2.4%/-4.4%, Menlo -1.4%/-2.8%, Lucida
Grande -1.1%/-2.1%, GeezaPro -0.4%/-0.6% -- short-buffer workloads
gain most. Outputs byte-identical throughout, including Roboto
cross-checked against the ot shaper. Full test suite passes.

Needs a harfrust release carrying Shaper::from_font_instance
(harfbuzz/harfrust#443).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Behdad EsfahbodandClaude Fable 5 committed 2026-08-23 00:45:35 -06:00
1 parent b04cd416f3
commit e76021ea8c
2 files changed
+78 -4

No files matched your search

+2
View File
@@ -114,6 +114,7 @@ _hb_harfrust_shape_rs (const void *font_data,
const void *rs_shape_plan,
const void *rs_buffer,
hb_font_t *font,
uint32_t coords_serial,
hb_buffer_t *buffer,
const hb_codepoint_t *pre_context,
uint32_t pre_context_len,
@@ -179,6 +180,7 @@ retry_shape_plan:
hr_shape_plan,
hr_buffer,
font,
font->serial_coords.get_relaxed (),
buffer,
buffer->context[0],
buffer->context_len[0],
+76 -4
View File
@@ -15,7 +15,7 @@ use harfrust::{
FontTableFunction, NominalGlyphBatch,
},
GlyphExtents, GlyphFlags as HRGlyphFlags, GlyphId, GlyphInfo as HRGlyphInfo,
GlyphPosition as HRGlyphPosition, NormalizedCoord, ShapeOptions, Tag,
GlyphPosition as HRGlyphPosition, NormalizedCoord, ShapeOptions, Shaper, Tag,
};
use smallvec::SmallVec;
@@ -133,8 +133,29 @@ pub unsafe extern "C" fn _hb_harfrust_shaper_face_data_destroy_rs(data: *mut c_v
let _hr_face_data = Box::from_raw(data);
}
/// The per-shape-call `OtTables`/`AatTables` view, cached between
/// calls. The heavy `ShaperData` is cached inside the font instance
/// already; this keeps the constructed `Shaper` alive too, keyed on
/// hb_font_t's variation-coords serial so a variations change rebuilds
/// both the coords snapshot and the view.
struct CachedShaper {
coords_serial: u32,
/// Borrows `instance` below with an erased lifetime. Declared
/// first so it drops first; `instance` is never touched while the
/// shaper is alive.
shaper: Option<Shaper<'static>>,
instance: Box<FontInstance>,
}
pub struct HBHarfRustFontData {
/// Create-time snapshot; used for shape-plan creation.
instance: FontInstance,
/// The shaping view. Lock-free by hb_font_t's contract: font
/// parameters don't change while shaping calls are in flight, so
/// replacement on a serial change never races a reader. The only
/// true race is two threads building the first entry, resolved
/// with a compare-exchange (the loser drops its copy).
cached: std::sync::atomic::AtomicPtr<CachedShaper>,
}
struct HBHarfBuzzFontFuncs {
@@ -251,7 +272,10 @@ pub unsafe extern "C" fn _hb_harfrust_shaper_font_data_create_rs(
let face_data = face_data as *const HBHarfRustFaceData;
let instance = font_to_instance(font, &(*face_data).font);
let hr_font_data = HBHarfRustFontData { instance };
let hr_font_data = HBHarfRustFontData {
instance,
cached: std::sync::atomic::AtomicPtr::new(std::ptr::null_mut()),
};
let hr_font_data = Box::new(hr_font_data);
let hr_font_data_ptr = Box::into_raw(hr_font_data);
@@ -262,7 +286,13 @@ pub unsafe extern "C" fn _hb_harfrust_shaper_font_data_create_rs(
#[unsafe(no_mangle)]
pub unsafe extern "C" fn _hb_harfrust_shaper_font_data_destroy_rs(data: *mut c_void) {
let data = data as *mut HBHarfRustFontData;
let _hr_font_data = Box::from_raw(data);
let hr_font_data = Box::from_raw(data);
let cached = hr_font_data
.cached
.load(std::sync::atomic::Ordering::Acquire);
if !cached.is_null() {
drop(Box::from_raw(cached));
}
}
fn hb_language_to_hr_language(language: hb_language_t) -> Option<harfrust::Language> {
@@ -354,6 +384,7 @@ pub unsafe extern "C" fn _hb_harfrust_shape_rs(
shape_plan: *const c_void,
hr_buffer_box: *const c_void,
font: *mut hb_font_t,
coords_serial: u32,
buffer: *mut hb_buffer_t,
pre_context: *const u32,
pre_context_length: u32,
@@ -443,7 +474,48 @@ pub unsafe extern "C" fn _hb_harfrust_shape_rs(
.point_size(ptem)
.features(&features)
.font_funcs(Some(&mut font_funcs));
let glyphs = harfrust::shape(&(*font_data).instance, hr_buffer, options);
use std::sync::atomic::Ordering;
let slot = &(*font_data).cached;
let mut cached = slot.load(Ordering::Acquire);
if cached.is_null() || (*cached).coords_serial != coords_serial {
let mut new = Box::new(CachedShaper {
coords_serial,
shaper: None,
instance: Box::new(font_to_instance(font, (*font_data).instance.font())),
});
let shaper = Shaper::from_font_instance(&new.instance);
// Erase the lifetime; see CachedShaper.
new.shaper = shaper.map(|s| std::mem::transmute::<Shaper<'_>, Shaper<'static>>(s));
new.coords_serial = coords_serial;
let new = Box::into_raw(new);
if cached.is_null() {
match slot.compare_exchange(
std::ptr::null_mut(),
new,
Ordering::AcqRel,
Ordering::Acquire,
) {
Ok(_) => cached = new,
Err(winner) => {
// Another thread built it first; ours goes.
drop(Box::from_raw(new));
cached = winner;
}
}
} else {
// Serial changed: by hb_font_t's contract no shaping call
// is concurrently reading the old entry.
let old = slot.swap(new, Ordering::AcqRel);
drop(Box::from_raw(old));
cached = new;
}
}
let Some(shaper) = (*cached).shaper.as_ref() else {
*hr_buffer_box = hr_buffer; // Move the buffer back into the box
let _ = Box::into_raw(hr_buffer_box); // Prevent double free
return false as hb_bool_t;
};
let glyphs = shaper.shape(hr_buffer, options);
hb_buffer_set_content_type(
buffer,