From 645fabd1013e94693f6da2d4263b0edd3b0b014d Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sun, 16 Jul 2023 11:36:59 -0600 Subject: [PATCH] [gsubgpos] Vastly speed up ChainRuleSet / RuleSet matching Match the first component in a fast loop. Idea replicated from LigatureSet. Speeds up Gulzar shaping by 22%! 37% in NotoNastaliqUrdu! --- src/OT/Layout/GSUB/LigatureSet.hh | 4 +- src/hb-ot-layout-gsubgpos.hh | 82 ++++++++++++++++++++++++++++--- 2 files changed, 77 insertions(+), 9 deletions(-) diff --git a/src/OT/Layout/GSUB/LigatureSet.hh b/src/OT/Layout/GSUB/LigatureSet.hh index 25fc02453..67a4e0219 100644 --- a/src/OT/Layout/GSUB/LigatureSet.hh +++ b/src/OT/Layout/GSUB/LigatureSet.hh @@ -92,7 +92,9 @@ struct LigatureSet } /* This version is optimized for speed by matching the first component - * of the ligature here, instead of calling into the ligation code. */ + * of the ligature here, instead of calling into the ligation code. + * + * This is replicated in ChainRuleSet and RuleSet. */ hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input; skippy_iter.reset (c->buffer->idx, 1); diff --git a/src/hb-ot-layout-gsubgpos.hh b/src/hb-ot-layout-gsubgpos.hh index f814c7e2e..703509277 100644 --- a/src/hb-ot-layout-gsubgpos.hh +++ b/src/hb-ot-layout-gsubgpos.hh @@ -1972,6 +1972,9 @@ static inline bool context_apply_lookup (hb_ot_apply_context_t *c, template struct Rule { + template + friend struct RuleSet; + bool intersects (const hb_set_t *glyphs, ContextClosureLookupContext &lookup_context) const { return context_intersects (glyphs, @@ -2159,13 +2162,74 @@ struct RuleSet const ContextApplyLookupContext &lookup_context) const { TRACE_APPLY (this); - return_trace ( - + hb_iter (rule) - | hb_map (hb_add (this)) - | hb_map ([&] (const Rule &_) { return _.apply (c, lookup_context); }) - | hb_any - ) - ; + + unsigned num_rules = rule.len; + + if (HB_OPTIMIZE_SIZE_VAL || num_rules <= 2) + { + slow: + return_trace ( + + hb_iter (rule) + | hb_map (hb_add (this)) + | hb_map ([&] (const Rule &_) { return _.apply (c, lookup_context); }) + | hb_any + ) + ; + } + + /* This version is optimized for speed by matching the first component + * of the rule here, instead of calling into the matching code. + * + * Replicated from LigatureSet::apply(). */ + + hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input; + skippy_iter.reset (c->buffer->idx, 1); + skippy_iter.set_match_func (match_always, nullptr); + skippy_iter.set_glyph_data ((HBUINT16 *) nullptr); + unsigned unsafe_to; + hb_glyph_info_t *first = nullptr; + bool matched = skippy_iter.next (&unsafe_to); + if (likely (matched)) + { + first = &c->buffer->info[skippy_iter.idx]; + unsafe_to = skippy_iter.idx + 1; + + if (skippy_iter.may_skip (c->buffer->info[skippy_iter.idx])) + { + /* Can't use the fast path if eg. the next char is a default-ignorable + * or other skippable. */ + goto slow; + } + } + else + goto slow; + + bool unsafe_to_concat = false; + + for (unsigned int i = 0; i < num_rules; i++) + { + const auto &r = this+rule.arrayZ[i]; + + const auto &input = r.inputZ; + + if (r.inputCount <= 1 || + (!lookup_context.funcs.match || + lookup_context.funcs.match (*first, input.arrayZ[0], lookup_context.match_data))) + { + if (r.apply (c, lookup_context)) + { + if (unsafe_to_concat) + c->buffer->unsafe_to_concat (c->buffer->idx, unsafe_to); + return_trace (true); + } + } + else + unsafe_to_concat = true; + } + if (likely (unsafe_to_concat)) + c->buffer->unsafe_to_concat (c->buffer->idx, unsafe_to); + + return_trace (false); } bool subset (hb_subset_context_t *c, @@ -3254,7 +3318,9 @@ struct ChainRuleSet } /* This version is optimized for speed by matching the first component - * of the rule here, instead of calling into the matching code. */ + * of the rule here, instead of calling into the matching code. + * + * Replicated from LigatureSet::apply(). */ hb_ot_apply_context_t::skipping_iterator_t &skippy_iter = c->iter_input; skippy_iter.reset (c->buffer->idx, 1);