[repacker] Bound clone buffers by each ligature split range

Many short ranges allocated the original LigatureSet offset array for
every clone, retaining quadratic buffer space even after shrink_buffer
moved the tails. Bound capacity by the number of offset slots in the
range, since each emitted set consumes at least one slot.

Advance through earlier ligature counts once across ordered ranges and
stop scanning at each range's end. Add a compact graph requiring one
subtable per coverage entry and verify counts, glyphs and connectivity.

An 8000-range run reduces peak RSS from 163 MB to 32 MB. Source-link
movement and coverage filtering still take quadratic time across many
ranges; document that remaining work rather than claiming a full fix.

Testing: full suite (303 passed, 1 skipped), ASan/UBSan repacker tests,
HB_NO_BEYOND_64K repacker tests, and 520 injected allocation failures
in a temporary harness. The combined fix series also passed 228650
ASan fuzzer runs with failing malloc.

Assisted-by: OpenAI Codex
This commit is contained in:
Behdad Esfahbod committed 2026-11-12 18:24:28 -06:00
1 parent 95b711ccb8
commit 5b1aa7c9db
2 files changed
+76 -8

No files matched your search

+22 -8
View File
@@ -99,7 +99,11 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1
total_ligas,
std::move (counts),
std::move (liga_set_ids),
0,
0,
};
// TODO: move_children () and coverage filtering still scan the source
// for each range; many short ranges can take quadratic time.
return actuate_subtable_split<split_context_t> (split_context, split_points);
}
@@ -226,6 +230,8 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1
unsigned original_count_;
hb_vector_t<unsigned> liga_counts;
hb_vector_t<unsigned> liga_set_ids;
unsigned first_set;
unsigned first_liga;
unsigned original_count ()
{
@@ -234,7 +240,13 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1
graph_result_t<unsigned> clone_range (unsigned start, unsigned end)
{
return thiz->clone_range (c, this_index, liga_counts, liga_set_ids, start, end);
// Ranges arrive in order. Advance past earlier sets once across all
// ranges, rather than scanning the complete counts array for each one.
while (first_set < liga_counts.length &&
first_liga + liga_counts[first_set] <= start)
first_liga += liga_counts[first_set++];
return thiz->clone_range (c, this_index, liga_counts, liga_set_ids,
first_set, first_liga, start, end);
}
graph_result_t<void> shrink (unsigned count)
@@ -313,22 +325,24 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1
unsigned this_index,
const hb_vector_t<unsigned>& liga_counts,
hb_vector_t<unsigned>& liga_set_ids,
unsigned first_set, unsigned first_liga,
unsigned start, unsigned end) const
{
DEBUG_MSG (SUBSET_REPACK, nullptr,
" Cloning LigatureSubstFormat1 (%u) range [%u, %u).", this_index, start, end);
// Create an oversized new liga subst, we'll adjust the size down later. We don't know
// the final size until we process it but we also need it to exist while we're processing
// so that nodes can be moved to it as needed.
// Each emitted set contains at least one offset slot in [start, end).
// Bound the capacity by this range, so many short ranges do not each
// allocate the original subtable's entire offset array.
unsigned capacity = hb_min ((unsigned) ligatureSet.len, end - start);
unsigned prime_size = OT::Layout::GSUB_impl::LigatureSubstFormat1_2<SmallTypes>::min_size
+ ligatureSet.get_size() - ligatureSet.len.get_size();
+ capacity * SmallTypes::size;
TRY_ASSIGN (unsigned liga_subst_prime_id, c.create_node (prime_size));
LigatureSubstFormat1* liga_subst_prime = (LigatureSubstFormat1*) c.graph.object (liga_subst_prime_id).head;
liga_subst_prime->format = this->format;
liga_subst_prime->ligatureSet.len = this->ligatureSet.len;
liga_subst_prime->ligatureSet.len = capacity;
// Create a place holder coverage prime id since we need to add virtual links to it while
// generating liga and liga sets. Afterwards it will be updated to have the correct coverage.
@@ -341,12 +355,12 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1
// Locate all liga sets with ligas between start and end.
// Clone or move them as needed.
unsigned count = 0;
unsigned count = first_liga;
unsigned liga_set_count = 0;
unsigned liga_set_start = HB_GRAPH_INVALID;
unsigned liga_set_end = 0; // inclusive
unsigned moved_end = 0; // liga sets before this one have already been moved to the new liga subst.
for (unsigned i = 0; i < liga_counts.length; i++)
for (unsigned i = first_set; i < liga_counts.length && count < end; i++)
{
unsigned num_ligas = liga_counts[i];
+54
View File
@@ -3136,6 +3136,59 @@ static void test_resolve_with_liga_split_shared_run (bool large_first)
}
}
static void test_liga_split_many_ranges (unsigned num_sets)
{
// A compact graph with one shared large ligature requires one output
// subtable per coverage entry. Allocations must follow the output ranges,
// rather than reserving the original offset array for every clone.
hb_vector_t<char> buffer;
hb_always_assert (buffer.resize (100000 + 2 * num_sets));
hb_serialize_context_t c (buffer.arrayZ, buffer.length);
c.start_serialize<char> ();
unsigned coverage = add_coverage (0, num_sets - 1, &c);
std::string large (65520, 'a');
start_object (large.data (), large.size (), &c);
add_virtual_offset (coverage, &c);
unsigned liga = c.pop_pack (false);
add_liga_set_header (1, &c);
add_offset (liga, &c);
add_virtual_offset (coverage, &c);
unsigned set = c.pop_pack (false);
add_liga_header (coverage, num_sets, &c);
for (unsigned i = 0; i < num_sets; i++) add_offset (set, &c);
unsigned subtable = c.pop_pack (false);
start_lookup (4, 1, &c);
add_offset (subtable, &c);
unsigned lookup = finish_lookup (&c);
unsigned lookup_list = add_lookup_list (&lookup, 1, &c);
add_gsubgpos_header (lookup_list, &c);
c.end_serialize ();
graph_t graph = graph_t::create (c.object_graph ()).value ();
auto context = graph::gsubgpos_graph_context_t::create (HB_OT_TAG_GSUB, graph).value ();
unsigned lookup_id = *context.lookups.keys ();
hb_always_assert (context.lookups.get (lookup_id)->split_subtables_if_needed (context, lookup_id).is_ok ());
auto subtables = graph.vertices_[lookup_id].position_to_index_map ().value ();
hb_always_assert (subtables.get_population () == num_sets);
for (unsigned i = 0; i < num_sets; i++)
{
unsigned id = subtables.get (6 + 2 * i);
const auto& obj = graph.object (id);
hb_always_assert (obj.tail - obj.head == 8);
hb_always_assert (*(const OT::HBUINT16*) (obj.head + 4) == 1);
auto coverage = graph.as_table<graph::Coverage> (id, obj.head + 2).value ();
auto glyphs = coverage.table->iter ();
hb_always_assert (glyphs && *glyphs == i);
glyphs++;
hb_always_assert (!glyphs);
auto set = graph.as_table<graph::LigatureSet> (id, obj.head + 6).value ();
hb_always_assert (set.table->ligature.len == 1);
unsigned liga_id = graph.index_for_offset (set.index, set.vertex->obj ().head + 2).value ();
hb_always_assert (graph.vertices_[liga_id].table_size () == large.size ());
}
hb_always_assert (graph.is_fully_connected ().is_ok ());
}
static void test_resolve_with_liga_split_shared_table ()
{
size_t buffer_size = 400000;
@@ -3800,6 +3853,7 @@ main (int argc, char **argv)
test_resolve_with_liga_split_shared_run (true);
test_resolve_with_liga_split_shared_run (false);
test_resolve_with_liga_split_null_offsets ();
test_liga_split_many_ranges (512);
test_resolve_with_liga_split_shared_table ();
test_resolve_with_liga_split_shared_coverage ();
test_deep_graph_traversal ();