From 5b319d5bfc910d7957259135b83fd04bdceeaff0 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Oct 2026 16:10:55 -0600 Subject: [PATCH] [subset] Compute COLR constant_after_pinning once per plan PaintRadialGradient::constant_after_pinning() answers a question about the source ItemVariationStore and the subset plan only, yet it rescanned every variation region (building a hashmap per region, up to 32767) on every paint visit. Shared paints are visited once per reference by OffsetTo::serialize_subset(), so during partial instancing the cost grew as visits x regionCount: 40 s to 6 minutes for a 192 KB font with a PaintComposite chain and many regions. Cache the answer in a tri-state plan field (-1 until computed, like the lazily-set has_gdef_varstore flag) so the scan runs at most once per plan; the plan is valid for one source face and one set of axis locations, so this is sound. Move the cheap varIdxBase != NO_VARIATION test ahead of the scan so non-variable format 6 gradients never trigger it. The allocation-failure path of get_var_region() still sets the serializer error and returns uncached, as before. Output is byte-identical; the attack font drops from ~41 s to ~0.03 s. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-910I4C0Y. Co-Authored-By: Claude Opus 4.8 --- src/OT/Color/COLR/COLR.hh | 11 +++++++---- src/hb-subset-plan.cc | 1 + src/hb-subset-plan.hh | 3 +++ 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/OT/Color/COLR/COLR.hh b/src/OT/Color/COLR/COLR.hh index c0533edf7..a2b630840 100644 --- a/src/OT/Color/COLR/COLR.hh +++ b/src/OT/Color/COLR/COLR.hh @@ -890,6 +890,10 @@ struct PaintRadialGradient const ItemVarStoreInstancer &instancer) const { if (c->plan->has_avar2) return false; + /* The answer depends on the table and the plan only, but shared paints + * are visited once per reference, so compute it once per plan. */ + int &constant = c->plan->colrv1_constant_after_pinning; + if (constant >= 0) return constant; const auto ®ions = instancer.varStore->get_region_list (); for (unsigned i = 0; i < regions.regionCount; i++) { @@ -904,10 +908,10 @@ struct PaintRadialGradient Triple *limit; if (!c->plan->user_axes_location.has (axis.first, &limit) || limit->minimum != limit->maximum) - return false; + return (constant = false); } } - return true; + return (constant = true); } void dependv1 (hb_colrv1_depend_context_t* c) const {} @@ -1052,9 +1056,8 @@ struct PaintRadialGradient uint32_t varIdxBase) const { TRACE_SUBSET (this); - if (instancer && !c->plan->pinned_at_default && + if (instancer && !c->plan->pinned_at_default && varIdxBase != VarIdx::NO_VARIATION && (c->plan->all_axes_pinned || constant_after_pinning (c, instancer)) && - varIdxBase != VarIdx::NO_VARIATION && (radius0 + (double) roundf (instancer (varIdxBase, 2)) < 0 || radius1 + (double) roundf (instancer (varIdxBase, 5)) < 0)) return_trace (subset_nonnegative (c, instancer, varIdxBase)); diff --git a/src/hb-subset-plan.cc b/src/hb-subset-plan.cc index 1f664d181..7dd6908ca 100644 --- a/src/hb-subset-plan.cc +++ b/src/hb-subset-plan.cc @@ -695,6 +695,7 @@ hb_subset_plan_t::hb_subset_plan_t (hb_face_t *face, all_axes_pinned = false; pinned_at_default = true; has_gdef_varstore = false; + colrv1_constant_after_pinning = -1; has_avar2 = false; #ifdef HB_EXPERIMENTAL_API diff --git a/src/hb-subset-plan.hh b/src/hb-subset-plan.hh index 9b20b3385..3d122731a 100644 --- a/src/hb-subset-plan.hh +++ b/src/hb-subset-plan.hh @@ -169,6 +169,9 @@ struct hb_subset_plan_t // whether GDEF ItemVariationStore is retained mutable bool has_gdef_varstore; + // whether every COLR variation region uses pinned axes only; -1 until computed + int colrv1_constant_after_pinning; + // whether we're doing avar2 partial instancing bool has_avar2;