From 37bfdf79c66ffbe4a5f02dbbc13756865ca11c1e Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 5 Feb 2025 02:08:19 +0000 Subject: [PATCH] [morx] Only collect glyphs that can initiate action in the machine And match them against the buffer glyph_set. 4% speedup in LucidaGrande. --- src/hb-aat-layout-common.hh | 121 ++++++++++++++++++++++++++++---- src/hb-aat-layout-morx-table.hh | 6 +- 2 files changed, 110 insertions(+), 17 deletions(-) diff --git a/src/hb-aat-layout-common.hh b/src/hb-aat-layout-common.hh index bb24b6437..f8076aa24 100644 --- a/src/hb-aat-layout-common.hh +++ b/src/hb-aat-layout-common.hh @@ -31,6 +31,7 @@ #include "hb-aat-map.hh" #include "hb-open-type.hh" #include "hb-cache.hh" +#include "hb-bit-page.hh" namespace OT { struct GDEF; @@ -111,6 +112,13 @@ struct LookupFormat0 { glyphs.add_range (0, num_glyphs - 1); } + template + void collect_glyphs_filtered (set_t &glyphs, unsigned num_glyphs, const filter_t &filter) const + { + for (unsigned i = 0; i < num_glyphs; i++) + if (filter (arrayZ[i])) + glyphs.add (i); + } bool sanitize (hb_sanitize_context_t *c) const { @@ -143,8 +151,14 @@ struct LookupSegmentSingle template void collect_glyphs (set_t &glyphs) const { - if (first == DELETED_GLYPH) - return; + if (first == DELETED_GLYPH) return; + glyphs.add_range (first, last); + } + template + void collect_glyphs_filtered (set_t &glyphs, const filter_t &filter) const + { + if (first == DELETED_GLYPH) return; + if (!filter (value)) return; glyphs.add_range (first, last); } @@ -185,6 +199,13 @@ struct LookupFormat2 for (unsigned int i = 0; i < count; i++) segments[i].collect_glyphs (glyphs); } + template + void collect_glyphs_filtered (set_t &glyphs, const filter_t &filter) const + { + unsigned count = segments.get_length (); + for (unsigned int i = 0; i < count; i++) + segments[i].collect_glyphs_filtered (glyphs, filter); + } bool sanitize (hb_sanitize_context_t *c) const { @@ -220,10 +241,18 @@ struct LookupSegmentArray template void collect_glyphs (set_t &glyphs) const { - if (first == DELETED_GLYPH) - return; + if (first == DELETED_GLYPH) return; glyphs.add_range (first, last); } + template + void collect_glyphs_filtered (set_t &glyphs, const void *base, const filter_t &filter) const + { + if (first == DELETED_GLYPH) return; + const auto &values = base+valuesZ; + for (hb_codepoint_t i = first; i <= last; i++) + if (filter (values[i - first])) + glyphs.add (i); + } int cmp (hb_codepoint_t g) const { return g < first ? -1 : g <= last ? 0 : +1; } @@ -274,6 +303,13 @@ struct LookupFormat4 for (unsigned i = 0; i < count; i++) segments[i].collect_glyphs (glyphs); } + 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); + } bool sanitize (hb_sanitize_context_t *c) const { @@ -306,8 +342,14 @@ struct LookupSingle template void collect_glyphs (set_t &glyphs) const { - if (glyph == DELETED_GLYPH) - return; + if (glyph == DELETED_GLYPH) return; + glyphs.add (glyph); + } + template + void collect_glyphs_filtered (set_t &glyphs, const filter_t &filter) const + { + if (glyph == DELETED_GLYPH) return; + if (!filter (value)) return; glyphs.add (glyph); } @@ -347,6 +389,13 @@ struct LookupFormat6 for (unsigned i = 0; i < count; i++) entries[i].collect_glyphs (glyphs); } + template + void collect_glyphs_filtered (set_t &glyphs, const filter_t &filter) const + { + unsigned count = entries.get_length (); + for (unsigned i = 0; i < count; i++) + entries[i].collect_glyphs_filtered (glyphs, filter); + } bool sanitize (hb_sanitize_context_t *c) const { @@ -382,12 +431,20 @@ struct LookupFormat8 template void collect_glyphs (set_t &glyphs) const { - if (unlikely (!glyphCount)) - return; - if (firstGlyph == DELETED_GLYPH) - return; + if (unlikely (!glyphCount)) return; + if (firstGlyph == DELETED_GLYPH) return; glyphs.add_range (firstGlyph, firstGlyph + glyphCount - 1); } + template + void collect_glyphs_filtered (set_t &glyphs, const filter_t &filter) const + { + if (unlikely (!glyphCount)) return; + if (firstGlyph == DELETED_GLYPH) return; + const T *p = valueArrayZ.arrayZ; + for (unsigned i = 0; i < glyphCount; i++) + if (filter (p[i])) + glyphs.add (firstGlyph + i); + } bool sanitize (hb_sanitize_context_t *c) const { @@ -436,10 +493,8 @@ struct LookupFormat10 template void collect_glyphs (set_t &glyphs) const { - if (unlikely (!glyphCount)) - return; - if (firstGlyph == DELETED_GLYPH) - return; + if (unlikely (!glyphCount)) return; + if (firstGlyph == DELETED_GLYPH) return; glyphs.add_range (firstGlyph, firstGlyph + glyphCount - 1); } @@ -504,6 +559,18 @@ struct Lookup default:return; } } + template + void collect_glyphs_filtered (set_t &glyphs, unsigned num_glyphs, const filter_t &filter) const + { + switch (u.format) { + case 0: hb_barrier (); u.format0.collect_glyphs_filtered (glyphs, num_glyphs, filter); return; + case 2: hb_barrier (); u.format2.collect_glyphs_filtered (glyphs, filter); return; + case 4: hb_barrier (); u.format4.collect_glyphs_filtered (glyphs, filter); return; + case 6: hb_barrier (); u.format6.collect_glyphs_filtered (glyphs, filter); return; + case 8: hb_barrier (); u.format8.collect_glyphs_filtered (glyphs, filter); return; + default:return; + } + } typename T::type get_class (hb_codepoint_t glyph_id, unsigned int num_glyphs, @@ -635,6 +702,25 @@ struct StateTable { (this+classTable).collect_glyphs (glyphs, num_glyphs); } + template + void collect_initial_glyphs (set_t &glyphs, unsigned num_glyphs, const table_t &table) const + { + // Collect all classes going out from the start state. + unsigned num_classes = nClasses; + hb_set_t filter; + for (unsigned i = 0; i < num_classes; i++) + { + const auto &entry = get_entry (STATE_START_OF_TEXT, i); + if (new_state (entry.newState) == STATE_START_OF_TEXT && + !table.is_actionable (entry)) + continue; + + filter.add (i); + } + + // And glyphs in those classes. + (this+classTable).collect_glyphs_filtered (glyphs, num_glyphs, filter); + } int new_state (unsigned int newState) const { return Types::extended ? newState : ((int) newState - (int) stateArrayTable) / (int) nClasses; } @@ -809,6 +895,13 @@ struct ClassTable if (classArray.arrayZ[i] != CLASS_OUT_OF_BOUNDS) glyphs.add (firstGlyph + i); } + template + void collect_glyphs_filtered (set_t &glyphs, unsigned num_glyphs, const filter_t &filter) const + { + for (unsigned i = 0; i < classArray.len; i++) + if (filter (classArray.arrayZ[i])) + glyphs.add (firstGlyph + i); + } bool sanitize (hb_sanitize_context_t *c) const { diff --git a/src/hb-aat-layout-morx-table.hh b/src/hb-aat-layout-morx-table.hh index 21ec7f305..8a2370c84 100644 --- a/src/hb-aat-layout-morx-table.hh +++ b/src/hb-aat-layout-morx-table.hh @@ -69,7 +69,7 @@ struct RearrangementSubtable bool is_actionable (const Entry &entry) const { - return (entry.flags & Verb); // && start < end; + return (entry.flags & (MarkFirst | MarkLast | Verb)); } struct driver_context_t @@ -222,7 +222,7 @@ struct ContextualSubtable bool is_actionable (const Entry &entry) const { - return entry.data.markIndex != 0xFFFF || entry.data.currentIndex != 0xFFFF; + return (entry.flags & SetMark) || entry.data.markIndex != 0xFFFF || entry.data.currentIndex != 0xFFFF; } struct driver_context_t @@ -967,7 +967,7 @@ struct hb_accelerate_subtables_context_t : template auto init_ (const T &obj_, unsigned num_glyphs, hb_priority<1>) HB_AUTO_RETURN ( - obj_.machine.collect_glyphs (glyph_set, num_glyphs) + obj_.machine.collect_initial_glyphs (glyph_set, num_glyphs, obj_) ) template