From 9e65a95d62e60be13aef221cbca493fdb6160e75 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 9 Nov 2026 18:03:08 -0600 Subject: [PATCH] [aat] Share the segment-collection guard between Lookup formats 2 and 4 (#6612) #6513 made LookupFormat2::collect_glyphs_filtered() stop adding segments one by one once they are out of order, folding the rest into a single range. It did so with an inline copy of the segment logic instead of the segment's own methods, and LookupFormat4 kept the old loop, so repeated full-range format-4 segments each still cost a full pass (bounded only by sanitize's per-byte max_ops on the value arrays). Move the loop into collect_segments_glyphs_filtered(), shared by both formats. It asks each segment type whether it may contribute (may_match()) and collects through the segment's own collect_glyphs_filtered(). Format-2 segments check their value against the filter. Format-4 segments only check their range, so out-of-order format-4 segments are folded into a superset without reading their values. Format 4's collect_glyphs() now uses the filtered path with an always-true filter, as format 2's already does. Sorted tables produce the same sets as before. Testing: meson test -C build (all passing). Follow-up to #6513 Co-authored-by: Claude Opus 5.5 --- src/hb-aat-layout-common.hh | 100 +++++++++++++++++++----------------- 1 file changed, 54 insertions(+), 46 deletions(-) diff --git a/src/hb-aat-layout-common.hh b/src/hb-aat-layout-common.hh index 85f1f8293..119a5cb9e 100644 --- a/src/hb-aat-layout-common.hh +++ b/src/hb-aat-layout-common.hh @@ -271,6 +271,48 @@ struct hb_aat_apply_context_t : template struct Lookup; +/* Collects the glyphs of LookupFormat2 / LookupFormat4 segments. + * + * Segments are supposed to be sorted and disjoint, in which case adding + * them one by one touches each glyph and each page of the set at most once. + * The sanitizer does not check that, and a malformed table can repeat a + * segment spanning the whole glyph range thousands of times, each costing a + * full pass. So once a segment is out of order, add the rest as a single + * range; that is a superset of the glyphs, which is fine for a set that is + * only used as a pre-filter. */ +template +static inline void +collect_segments_glyphs_filtered (const segments_t &segments, + set_t &glyphs, + const void *base, + const filter_t &filter) +{ + unsigned count = segments.get_length (); + hb_codepoint_t next = 0; /* One past the last glyph added. */ + unsigned i; + for (i = 0; i < count; i++) + { + const auto &segment = segments[i]; + if (segment.first < next) break; /* Not sorted and disjoint. */ + if (!segment.may_match (base, filter)) + continue; + segment.collect_glyphs_filtered (glyphs, base, filter); + next = segment.last + 1; + } + + hb_codepoint_t first = HB_CODEPOINT_INVALID, last = 0; + for (; i < count; i++) + { + const auto &segment = segments[i]; + if (!segment.may_match (base, filter)) + continue; + first = hb_min (first, (hb_codepoint_t) segment.first); + last = hb_max (last, (hb_codepoint_t) segment.last); + } + if (first <= last) + glyphs.add_range (first, last); +} + template struct LookupFormat0 { @@ -330,11 +372,13 @@ struct LookupSegmentSingle if (first == DELETED_GLYPH) return; glyphs.add_range (first, last); } + template + bool may_match (const void *base HB_UNUSED, const filter_t &filter) const + { return first != DELETED_GLYPH && first <= last && filter (value); } template - void collect_glyphs_filtered (set_t &glyphs, const filter_t &filter) const + void collect_glyphs_filtered (set_t &glyphs, const void *base, const filter_t &filter) const { - if (first == DELETED_GLYPH) return; - if (!filter (value)) return; + if (!may_match (base, filter)) return; glyphs.add_range (first, last); } @@ -375,41 +419,7 @@ struct LookupFormat2 } template void collect_glyphs_filtered (set_t &glyphs, const filter_t &filter) const - { - /* Segments are supposed to be sorted and disjoint, in which case adding - * them one by one touches each page of the set at most once. The - * sanitizer does not check that, and a malformed table can repeat a - * segment spanning the whole glyph range thousands of times, each - * costing a full pass over the set. So once a segment is out of order, - * add the rest as a single range; that is a superset of the glyphs, - * which is fine for a set that is only used as a pre-filter. */ - unsigned count = segments.get_length (); - hb_codepoint_t next = 0; /* One past the last glyph added. */ - unsigned i; - for (i = 0; i < count; i++) - { - const auto &segment = segments[i]; - if (segment.first < next) break; /* Not sorted and disjoint. */ - if (segment.first == DELETED_GLYPH || segment.first > segment.last || - !filter (segment.value)) - continue; - glyphs.add_range (segment.first, segment.last); - next = segment.last + 1; - } - - hb_codepoint_t first = HB_CODEPOINT_INVALID, last = 0; - for (; i < count; i++) - { - const auto &segment = segments[i]; - if (segment.first == DELETED_GLYPH || segment.first > segment.last || - !filter (segment.value)) - continue; - first = hb_min (first, (hb_codepoint_t) segment.first); - last = hb_max (last, (hb_codepoint_t) segment.last); - } - if (first <= last) - glyphs.add_range (first, last); - } + { collect_segments_glyphs_filtered (segments, glyphs, this, filter); } bool sanitize (hb_sanitize_context_t *c) const { @@ -448,6 +458,10 @@ struct LookupSegmentArray if (first == DELETED_GLYPH) return; glyphs.add_range (first, last); } + /* Without looking at the values; for a superset. */ + template + bool may_match (const void *base HB_UNUSED, const filter_t &filter HB_UNUSED) const + { return first != DELETED_GLYPH && first <= last; } template void collect_glyphs_filtered (set_t &glyphs, const void *base, const filter_t &filter) const { @@ -503,17 +517,11 @@ struct LookupFormat4 template void collect_glyphs (set_t &glyphs) const { - unsigned count = segments.get_length (); - for (unsigned i = 0; i < count; i++) - segments[i].collect_glyphs (glyphs); + collect_glyphs_filtered (glyphs, [] (const T &) { return true; }); } template void collect_glyphs_filtered (set_t &glyphs, const filter_t &filter) const - { - unsigned count = segments.get_length (); - for (unsigned i = 0; i < count; i++) - segments[i].collect_glyphs_filtered (glyphs, this, filter); - } + { collect_segments_glyphs_filtered (segments, glyphs, this, filter); } bool sanitize (hb_sanitize_context_t *c) const {