mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-22 23:41:03 +01:00
[graph] Keep the Coverage range count of class_def_size_estimator_t incremental
add_class_def_size() rebuilt the Coverage size estimate by walking
every range of included_glyphs (num_glyph_ranges()) each time a class
added a glyph, so PairPosFormat2::split_subtables() did O(classes^2)
next_range() calls per subtable; a 131 KB font with 65535 single-glyph
classes numbered out of glyph order took minutes in hb_subset_or_fail().
The ClassDef half of the estimator was already made incremental in
19de83dad and #6212 skips the recount when a class adds no glyph, but
every class that does add a glyph still triggered a full recount.
Do the same for the Coverage half: a coverage_range_count member
(zeroed in reset()) is updated from the ranges of the newly added
class alone, adding one per range and subtracting one for each already
included neighbour (start-1, end+1) it merges with. This is exact
because every glyph belongs to exactly one class, so the new glyphs are
never already included; the population-unchanged shortcut therefore
becomes redundant and num_glyph_ranges() is removed.
A regression test is added to test-classdef-graph.cc whose classes
extend and bridge already-included ranges. Output is byte-identical;
the 8-subtable reproducer drops from >100 s to ~1 s; no public API/ABI
change.
This is the Coverage-estimation half left open by PR #6212.
Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-ZUWZMWMD.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
95086deff1
commit
c9a8620c00
2 files changed
+43
-15
No files matched your search
+17
-15
@@ -193,6 +193,7 @@ struct class_def_size_estimator_t
|
||||
included_glyphs.clear();
|
||||
included_class_def_glyphs.clear();
|
||||
included_classes.clear();
|
||||
coverage_range_count = 0;
|
||||
class_def_range_count = 0;
|
||||
class_max = 0;
|
||||
coverage_size_val = compute_coverage_size ();
|
||||
@@ -210,12 +211,23 @@ struct class_def_size_estimator_t
|
||||
if (!included_classes.has(klass)) {
|
||||
hb_set_t* glyphs = nullptr;
|
||||
if (glyphs_per_class.has(klass, &glyphs)) {
|
||||
unsigned num_glyphs = included_glyphs.get_population();
|
||||
// Each glyph belongs to exactly one class, so none of these glyphs are
|
||||
// included yet. Keep the coverage range count incremental (recounting
|
||||
// the ranges of included_glyphs for every class is quadratic): each new
|
||||
// range adds one, and merges with each already included neighbour.
|
||||
hb_codepoint_t start = HB_SET_VALUE_INVALID;
|
||||
hb_codepoint_t end = HB_SET_VALUE_INVALID;
|
||||
while (glyphs->next_range (&start, &end))
|
||||
{
|
||||
coverage_range_count++;
|
||||
if (start && included_glyphs.has (start - 1)) coverage_range_count--;
|
||||
if (included_glyphs.has (end + 1)) coverage_range_count--;
|
||||
}
|
||||
|
||||
included_glyphs.union_(*glyphs);
|
||||
if (klass)
|
||||
included_class_def_glyphs.union_(*glyphs);
|
||||
if (num_glyphs != included_glyphs.get_population())
|
||||
coverage_size_val = compute_coverage_size ();
|
||||
coverage_size_val = compute_coverage_size ();
|
||||
}
|
||||
|
||||
if (klass)
|
||||
@@ -230,17 +242,6 @@ struct class_def_size_estimator_t
|
||||
return class_def_size ();
|
||||
}
|
||||
|
||||
unsigned num_glyph_ranges (const hb_set_t &glyphs) const {
|
||||
hb_codepoint_t start = HB_SET_VALUE_INVALID;
|
||||
hb_codepoint_t end = HB_SET_VALUE_INVALID;
|
||||
|
||||
unsigned count = 0;
|
||||
while (glyphs.next_range (&start, &end)) {
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
private:
|
||||
graph_result_t<void> to_result() const
|
||||
{
|
||||
@@ -266,7 +267,7 @@ struct class_def_size_estimator_t
|
||||
unsigned coverage_base_size = OT::HBUINT16::static_size + glyph_size;
|
||||
unsigned range_size = 3 * glyph_size;
|
||||
unsigned format1_size = coverage_base_size + glyph_size * included_glyphs.get_population();
|
||||
unsigned format2_size = coverage_base_size + range_size * num_glyph_ranges (included_glyphs);
|
||||
unsigned format2_size = coverage_base_size + range_size * coverage_range_count;
|
||||
return hb_min(format1_size, format2_size);
|
||||
}
|
||||
|
||||
@@ -306,6 +307,7 @@ struct class_def_size_estimator_t
|
||||
hb_set_t included_classes;
|
||||
hb_set_t included_glyphs;
|
||||
hb_set_t included_class_def_glyphs;
|
||||
unsigned coverage_range_count;
|
||||
unsigned class_def_range_count;
|
||||
unsigned class_max;
|
||||
unsigned coverage_size_val;
|
||||
|
||||
@@ -327,6 +327,31 @@ static void test_running_class_size_estimates_with_locally_consecutive_glyphs ()
|
||||
hb_always_assert(check_add_class_def_size(estimator, map, 3, {2, 3}));
|
||||
}
|
||||
|
||||
static void test_running_coverage_size_estimates_with_interleaved_glyphs () {
|
||||
gid_and_class_list_t map = {
|
||||
{0, 2},
|
||||
{1, 1},
|
||||
{2, 1},
|
||||
{3, 1},
|
||||
{4, 2},
|
||||
{5, 1},
|
||||
{6, 1},
|
||||
{7, 1},
|
||||
{8, 2},
|
||||
{10, 3},
|
||||
};
|
||||
|
||||
// Class 2 extends and joins the coverage ranges of class 1.
|
||||
graph::class_def_size_estimator_t estimator = *graph::class_def_size_estimator_t::create (map.iter());
|
||||
hb_always_assert(check_add_class_def_size(estimator, map, 1, {1}));
|
||||
hb_always_assert(check_add_class_def_size(estimator, map, 2, {1, 2}));
|
||||
hb_always_assert(check_add_class_def_size(estimator, map, 3, {1, 2, 3}));
|
||||
|
||||
estimator.reset();
|
||||
hb_always_assert(check_add_class_def_size(estimator, map, 2, {2}));
|
||||
hb_always_assert(check_add_class_def_size(estimator, map, 1, {1, 2}));
|
||||
}
|
||||
|
||||
static void
|
||||
test_class_def_intersects_class ()
|
||||
{
|
||||
@@ -371,6 +396,7 @@ main (int argc, char **argv)
|
||||
test_class_and_coverage_size_estimates ();
|
||||
test_running_class_and_coverage_size_estimates ();
|
||||
test_running_class_size_estimates_with_locally_consecutive_glyphs ();
|
||||
test_running_coverage_size_estimates_with_interleaved_glyphs ();
|
||||
#ifndef HB_NO_BEYOND_64K
|
||||
test_class_def_format_3_values ();
|
||||
#endif
|
||||
|
||||
Reference in new issue
Block a user