diff --git a/src/graph/graph.hh b/src/graph/graph.hh index 099a93e41..ef49531a0 100644 --- a/src/graph/graph.hh +++ b/src/graph/graph.hh @@ -1016,12 +1016,7 @@ public: // incoming edges to root_idx should be all 32 bit in length so we don't need to de-dup these // set the subgraph incoming edge count to match all of root_idx's incoming edges hb_set_t parents; - for (unsigned root_idx : roots) - { - if (unlikely (root_idx >= vertices_.length)) - return Err(OUT_OF_BOUNDS); - subgraph.set (root_idx, wide_parents (root_idx, parents)); - } + TRY (wide_parents (roots, subgraph, parents)); for (unsigned root_idx : roots) { TRY (find_subgraph (root_idx, subgraph)); @@ -1771,26 +1766,45 @@ public: } /* - * Returns the numbers of incoming edges that are 24 or 32 bits wide. + * For each node in nodes, sets counts[node] to the number of incoming edges that + * are 24 or 32 bits wide. The sources of those edges are added to parents. + * counts is expected to be empty. */ - unsigned wide_parents (unsigned node_idx, hb_set_t& parents) const + graph_result_t wide_parents (const hb_set_t& nodes, + hb_map_t& counts, + hb_set_t& parents) const { - unsigned count = 0; - for (unsigned p : vertices_[node_idx].parents_iter ()) + // A single parent may link to many of the nodes (eg. a ConditionSet with + // thousands of 32 bit offsets), so collect the parents first and walk the + // links of each one only once. + hb_set_t all_parents; + for (unsigned node_idx : nodes) + { + if (unlikely (node_idx >= vertices_.length)) + return Err(OUT_OF_BOUNDS); + counts.set (node_idx, 0); + for (unsigned p : vertices_[node_idx].parents_iter ()) + all_parents.add (p); + } + TRY (graph_result_t::from (all_parents, ALLOCATION_FAILURE)); + TRY (graph_result_t::from (counts, ALLOCATION_FAILURE)); + + for (unsigned p : all_parents) { // Only real links can be wide for (const auto& l : vertices_[p].obj().real_links) { - if (l.objidx == node_idx - && (l.width == 3 || l.width == 4) - && !l.is_signed) + hb_codepoint_t *count; + if ((l.width == 3 || l.width == 4) + && !l.is_signed + && counts.has (l.objidx, &count)) { - count++; + (*count)++; parents.add (p); } } } - return count; + return graph_result_t::from (parents, ALLOCATION_FAILURE); } public: diff --git a/src/test-repacker.cc b/src/test-repacker.cc index 682713aec..47425d109 100644 --- a/src/test-repacker.cc +++ b/src/test-repacker.cc @@ -3220,6 +3220,74 @@ test_32bit_roots_traversal () free (buffer); } +static void +test_isolate_subgraph_shared_wide_parents () +{ + // Several parents each have 32 bit links to the same set of leaves, as + // FeatureVariations ConditionSets do. One leaf also has a 16 bit link from + // outside the subgraph, so isolating the leaves must duplicate that leaf only. + // + // root -16-> p1, p2, p3 + // root -16-> l1 + // p1, p2, p3 -32-> l1, l2, l3 + constexpr unsigned num_parents = 3; + constexpr unsigned num_leaves = 3; + + size_t buffer_size = 1000; + void* buffer = malloc (buffer_size); + hb_serialize_context_t c (buffer, buffer_size); + + c.start_serialize (); + unsigned leaves[num_leaves]; + for (unsigned i = 0; i < num_leaves; i++) + leaves[i] = add_object ("leaf", 4, &c); + + unsigned parents[num_parents]; + for (unsigned i = 0; i < num_parents; i++) + { + start_object ("parent", 6, &c); + for (unsigned j = 0; j < num_leaves; j++) + add_wide_offset (leaves[j], &c); + parents[i] = c.pop_pack (false); + } + + start_object ("root", 4, &c); + for (unsigned i = 0; i < num_parents; i++) + add_offset (parents[i], &c); + add_offset (leaves[0], &c); + c.pop_pack (false); + c.end_serialize (); + + graph_t graph = graph_t::create (c.object_graph ()).value (); + unsigned num_vertices = graph.vertices_.length; + hb_always_assert (num_vertices == 1 + num_parents + num_leaves); + + hb_set_t roots; + for (unsigned i = 0; i < num_leaves; i++) + roots.add (leaves[i] - 1); + + // leaves[0] has an incoming 16 bit edge from root, so it's duplicated. + hb_always_assert (graph.isolate_subgraph (roots).value ()); + hb_always_assert (graph.vertices_.length == num_vertices + 1); + hb_always_assert (roots.get_population () == num_leaves); + hb_always_assert (!roots.has (leaves[0] - 1)); + hb_always_assert (roots.has (num_vertices)); + for (unsigned i = 1; i < num_leaves; i++) + hb_always_assert (roots.has (leaves[i] - 1)); + + hb_always_assert (graph.update_parents ().is_ok ()); + hb_always_assert (graph.vertices_[leaves[0] - 1].incoming_edges () == 1); + hb_always_assert (graph.vertices_[num_vertices].incoming_edges () == num_parents); + for (unsigned i = 1; i < num_leaves; i++) + hb_always_assert (graph.vertices_[leaves[i] - 1].incoming_edges () == num_parents); + + // Now that all incoming edges to the roots are 32 bit, nothing is left to isolate. + hb_always_assert (!graph.isolate_subgraph (roots).value ()); + hb_always_assert (graph.vertices_.length == num_vertices + 1); + + free (buffer); +} + static void test_invalid_link_objidx () { size_t buffer_size = 100; @@ -3398,6 +3466,7 @@ main (int argc, char **argv) test_duplicate_subgraph_vertex_limit (); test_resolve_shared_overflow_vertex_limit (); test_32bit_roots_traversal (); + test_isolate_subgraph_shared_wide_parents (); // TODO(grieger): have run overflow tests compare graph equality not final packed binary. // TODO(grieger): split test where multiple subtables in one lookup are split to test link ordering.