From 1e8a554eaa7cbe47e405403cb1074698d778e6c8 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 11 Aug 2025 19:27:32 -0600 Subject: [PATCH] Hr buffer cache (#5452) * [harfrust] Rename a variable * [harfrust] Cache HR buffer on HB buffer --- src/hb-harfrust.cc | 43 +++++++++++++++++++++++++++++++++++++------ src/rust/shape.rs | 21 ++++++++++++++++++++- 2 files changed, 57 insertions(+), 7 deletions(-) diff --git a/src/hb-harfrust.cc b/src/hb-harfrust.cc index 2f3eb2596..38c9c89d8 100644 --- a/src/hb-harfrust.cc +++ b/src/hb-harfrust.cc @@ -29,6 +29,16 @@ #include "hb-shaper-impl.hh" +/* + * buffer + */ +extern "C" void * +_hb_harfrust_buffer_create_rs (void); + +extern "C" void +_hb_harfrust_buffer_destroy_rs (void *data); + + /* * shaper face data */ @@ -75,6 +85,7 @@ _hb_harfrust_shaper_font_data_destroy (hb_harfrust_font_data_t *data) _hb_harfrust_shaper_font_data_destroy_rs (data); } + /* * shape plan */ @@ -98,12 +109,13 @@ extern "C" hb_bool_t _hb_harfrust_shape_rs (const void *font_data, const void *face_data, const void *rs_shape_plan, + const void *rs_buffer, hb_font_t *font, hb_buffer_t *buffer, const hb_feature_t *features, unsigned int num_features); -static hb_user_data_key_t hr_shape_plan_key = {0}; +static hb_user_data_key_t hb_object_key = {0}; hb_bool_t _hb_harfrust_shape (hb_shape_plan_t *shape_plan, @@ -115,13 +127,31 @@ _hb_harfrust_shape (hb_shape_plan_t *shape_plan, const hb_harfrust_font_data_t *font_data = font->data.harfrust; const hb_harfrust_face_data_t *face_data = font->face->data.harfrust; +retry_buffer: + void *hr_buffer = hb_buffer_get_user_data (buffer, &hb_object_key); + if (unlikely (!hr_buffer)) + { + hr_buffer = _hb_harfrust_buffer_create_rs (); + if (unlikely (!hr_buffer)) + return false; + + if (!hb_buffer_set_user_data (buffer, + &hb_object_key, + hr_buffer, + _hb_harfrust_buffer_destroy_rs, + false)) + { + _hb_harfrust_buffer_destroy_rs (hr_buffer); + goto retry_buffer; + } + } + void *hr_shape_plan = nullptr; if (!num_features) { - retry: - hr_shape_plan = hb_shape_plan_get_user_data (shape_plan, - &hr_shape_plan_key); + retry_shape_plan: + hr_shape_plan = hb_shape_plan_get_user_data (shape_plan, &hb_object_key); if (unlikely (!hr_shape_plan)) { hr_shape_plan = _hb_harfrust_shape_plan_create_rs (font_data, face_data, @@ -130,13 +160,13 @@ _hb_harfrust_shape (hb_shape_plan_t *shape_plan, shape_plan->key.props.direction); if (hr_shape_plan && !hb_shape_plan_set_user_data (shape_plan, - &hr_shape_plan_key, + &hb_object_key, hr_shape_plan, _hb_harfrust_shape_plan_destroy_rs, false)) { _hb_harfrust_shape_plan_destroy_rs (hr_shape_plan); - goto retry; + goto retry_shape_plan; } } } @@ -144,6 +174,7 @@ _hb_harfrust_shape (hb_shape_plan_t *shape_plan, return _hb_harfrust_shape_rs (font_data, face_data, hr_shape_plan, + hr_buffer, font, buffer, features, diff --git a/src/rust/shape.rs b/src/rust/shape.rs index a2fbf0f6c..820c295e4 100644 --- a/src/rust/shape.rs +++ b/src/rust/shape.rs @@ -98,6 +98,18 @@ fn hb_language_to_hr_language(language: hb_language_t) -> Option *mut c_void { + let hr_buffer = Box::new(harfrust::UnicodeBuffer::new()); + Box::into_raw(hr_buffer) as *mut c_void +} + +#[no_mangle] +pub unsafe extern "C" fn _hb_harfrust_buffer_destroy_rs(data: *mut c_void) { + let data = data as *mut harfrust::UnicodeBuffer; + let _hr_buffer = Box::from_raw(data); +} + #[no_mangle] pub unsafe extern "C" fn _hb_harfrust_shape_plan_create_rs( font_data: *const c_void, @@ -144,6 +156,7 @@ pub unsafe extern "C" fn _hb_harfrust_shape_rs( font_data: *const c_void, face_data: *const c_void, shape_plan: *const c_void, + hr_buffer_box: *const c_void, font: *mut hb_font_t, buffer: *mut hb_buffer_t, features: *const hb_feature_t, @@ -154,7 +167,9 @@ pub unsafe extern "C" fn _hb_harfrust_shape_rs( let font_ref = &(*face_data).font_ref; - let mut hr_buffer = harfrust::UnicodeBuffer::new(); + let hr_buffer_box = hr_buffer_box as *mut harfrust::UnicodeBuffer; + let mut hr_buffer_box = Box::from_raw(hr_buffer_box); + let mut hr_buffer = *hr_buffer_box; // Set buffer properties let cluster_level = hb_buffer_get_cluster_level(buffer); @@ -299,5 +314,9 @@ pub unsafe extern "C" fn _hb_harfrust_shape_rs( pos.y_offset = (hr_pos.y_offset as f32 * y_scale + 0.5).floor() as hb_position_t; } + let hr_buffer = glyphs.clear(); + *hr_buffer_box = hr_buffer; // Move the buffer back into the box + let _ = Box::into_raw(hr_buffer_box); // Prevent double free + true as hb_bool_t }