Commit Graph
21844 Commits
Author SHA1 Message Date
Behdad Esfahbod 83eafa8ca6 [CFF] Bound charstring operators by the live work budget
The per-operator budget spend added a check to the charstring
interpreter's innermost loop.  The loop already counts operators through
max_ops, so cap max_ops by the caller's remaining budget and charge the
consumed operators back after the loop.  A single counter then enforces
both the structural HB_CFF_MAX_OPS limit and the live work budget, with
no budget check in the hot loop, and the operator count can never
overshoot the remaining budget.

This bounds the real quantity a COLR or VARC driver multiplies: executed
operator count, which a small charstring can drive up to HB_CFF_MAX_OPS
via subroutine reuse regardless of its byte length -- so charging the
charstring's length instead would badly undercount.  CFF2 blend still
charges its proportional work in place.

Tests: meson test -C build (275/275 passed); the CFF/COLR gpu-fuzzer
regression seed completes in 0.45s.

Assisted-by: Claude <noreply@anthropic.com>
2026-08-27 16:34:19 -06:00
Behdad Esfahbod 148fcb3c79 [NEWS] Restore the trailing blank line at end of file
Keep the rendering-work-budget NEWS edit to its intended additions and
drop the incidental end-of-file whitespace change.

Assisted-by: Claude <noreply@anthropic.com>
2026-08-27 15:42:37 -06:00
Behdad Esfahbod 565cbc3768 [limits] Compute the budget spend product in 64-bit
hb_budget_spend() evaluated COST * MULT in unsigned int, so the product
wrapped at 2^32 before widening to the int64_t counter.  No current
charge site can reach that (the largest, VARC's axisValues x coords, is
~2.7e8), but this primitive exists to harden against adversarial fonts,
and one cast removes the reasoning burden permanently.

Assisted-by: Claude <noreply@anthropic.com>
2026-08-27 15:42:37 -06:00
Behdad Esfahbod 115c3c0028 [CFF] Hoist the per-op budget charge out of the interpret loop
The charstring interpreter charged every operator by branching on the
opcode to pick HB_BUDGET_8 for callsubr/callgsubr and HB_BUDGET_1
otherwise -- two comparisons and a branch added to the innermost loop,
on the normal rendering path for every CFF font, not just adversarial
ones.

Charge one unit unconditionally in the loop and add the remaining call
weight inside the callsubr/callgsubr handlers, where a branch on the
opcode already exists.  The total per-call-operator charge is unchanged
(1 + 7 = 8), the subroutine is still not entered once the budget is
exhausted, and the hot loop is now branch-free.

Tests: meson test -C build (275/275 passed)

Assisted-by: Claude <noreply@anthropic.com>
2026-08-27 15:42:29 -06:00
Behdad Esfahbod 68641b2aed [limits] Consolidate rendering work budgets
Use HB_BUDGET_GLYPH as the common finite default for VARC and the
in-house draw and paint backends. Raster paint retains its surface-size
scaling so large targets still permit a small number of full passes.

Remove redundant exhaustion checks around hb_budget_spend(). A spend
that lands exactly on zero succeeds; only a negative live value latches
exhaustion. Apply the same boundary consistently to postcharged
renderer work and cover the next positive-cost attempt in tests.

The full Meson suite passes, as do the full TSan suite and the three
rendering regression seeds under ASan and UBSan.

Assisted-by: OpenAI Codex <codex@openai.com>
2026-08-26 18:34:31 -06:00
Behdad Esfahbod 7eab9be024 [cairo] Skip external lock inversion under TSan
Cairo's user-font rendering acquires two internal locks in both
orders. The new nested-rendering regression test exposes that existing
lock-order inversion under ThreadSanitizer, including when run against
an unmodified HarfBuzz.

Keep the end-to-end test enabled normally, but skip that workload when
compiled with TSan instead of suppressing deadlock detection globally.

Assisted-by: OpenAI Codex <codex@openai.com>
2026-08-26 18:34:02 -06:00
Behdad Esfahbod 08e080b130 [limits] Rely on the common integer definitions
hb-limits.hh already includes hb.hh, which reaches hb-common.h and its
portable fixed-width integer definitions.  Avoid a redundant direct
stdint.h include.

Assisted-by: OpenAI Codex <codex@openai.com>
2026-08-26 18:13:42 -06:00
Behdad Esfahbod 15a523bd87 [CFF] Test nested outline budget charges
Exercise CFF work accounting through the public draw-budget callbacks.
Use lower bounds above each parent charstring's flat operator cost so the
CFF1 assertion covers both seac children and the CFF2 assertion covers
proportional blend work.

Keep the interpreter's local cost type consistent with the unsigned spend
interface.

Tests: meson test -C build (275/275 passed)

Assisted-by: OpenAI Codex <codex@openai.com>
2026-08-26 17:54:45 -06:00
Behdad Esfahbod 8f3ab5ab87 [cairo] Share work across nested glyph rendering
Cairo's draw callbacks previously received a raw cairo_t, so paint-driven
outline traversals could not share a renderer budget.  Nested color glyphs
also construct fresh HarfBuzz paint contexts while populating the same
scaled-font glyph cache.

Attach a finite budget owner to the scaled font and let each draw/paint
context borrow it.  Recharge only for the outermost glyph callback, expose
the budget callbacks on both function sets, and charge emitted path and
nested color-glyph work.  Exercise the original vector timeout seed through
the Cairo user-font renderer.

Tests: meson test -C build (275/275 passed)

Assisted-by: OpenAI Codex <codex@openai.com>
2026-08-26 17:51:34 -06:00
Behdad Esfahbod a38e2831c0 [draw] Forward budgets through synthetic outlines
Synthetic slant and emboldening first render into an outline recorder.
That adapter previously hid the renderer's live budget, allowing the
underlying outline traversal to use a fresh per-glyph allowance.

Forward the destination draw data's remaining-budget pointer through
the recorder.  Add a regression test proving an exhausted synthetic
draw fails against the caller's budget.

Assisted-by: OpenAI Codex <codex@openai.com>
2026-08-26 17:42:23 -06:00
Behdad Esfahbod 5be2bffb7c [VARC] Thread the renderer work budget
Make VARC record traversal, variation processing, and all glyf/CFF
leaves consume the renderer-owned live counter. Forward that counter
through the transforming pen and make leaf traversal obtain it from
the active draw session.

Keep a finite standalone allowance for extents, charge records even
when conditions suppress their components, and add public-callback
coverage for cumulative depletion and exhaustion.

Tests: meson test -C build (274/274 passed)

Assisted-by: OpenAI Codex <codex@openai.com>
2026-08-26 17:39:46 -06:00
Behdad Esfahbod c06b7d4d5b [paint] Share the paint-extents work budget
Give the paint-extents context finite budget policy and live state, and
make its stack-local draw sink expose that live counter to outline
traversal. Charge emitted segments in place instead of debiting only
after an entire glyph draw has completed.

Handle COLR's zero-allocated scratch context explicitly without
confusing uninitialized storage with a client-selected zero budget.
Add focused coverage for scratch initialization, propagation, clear,
zero, and unlimited policies.

Tests: meson test -C build (274/274 passed)

Assisted-by: OpenAI Codex <codex@openai.com>
2026-08-26 17:35:23 -06:00
Behdad Esfahbod 9a424f1808 [gpu] Share rendering work budgets
Store finite budget policy and live state on GPU draw and paint
contexts. Charge generated curves against the draw context and make
paint-owned scratch draws borrow the paint session's counter.

Propagate the same live counter through both identity clip extraction
and the transforming forwarding pen. Preserve policy across clear and
restore DEFAULT on reset, with focused lifecycle and propagation tests.

The ClusterFuzz GPU timeout seed completes in 0.11 seconds in the
optimized build.

Tests: meson test -C build (273/273 passed)

Assisted-by: OpenAI Codex <codex@openai.com>
2026-08-26 17:28:03 -06:00
Behdad Esfahbod c7797701c0 [raster] Share rendering work budgets
Store budget policy and live state on raster draw and paint contexts.
Make outline traversal and curve flattening borrow the paint session's
live counter, while standalone draw keeps its own finite session counter.

Preserve raster's dynamic surface-sized default, resolving it only after
initialization and never replacing concrete or unlimited client policy.
Recharge on clear and restore DEFAULT on reset. Add lifecycle and lazy
default coverage.

Tests: meson test -C build (273/273 passed)

Assisted-by: OpenAI Codex <codex@openai.com>
2026-08-26 17:22:05 -06:00
Behdad Esfahbod b0606f9d8a [vector] Share rendering work budgets
Store budget policy and live state on vector draw and paint contexts.  Path
sinks borrow the owning context's live counter, making glyph traversal,
serialized path commands, and sweep-gradient patches consume one aggregate
budget throughout a clear-to-render session.

Recharge the configured policy on clear, restore the finite default on
reset, and expose the generic draw and paint budget callbacks for SVG and
PDF contexts.  Add lifecycle and traversal-depletion coverage for both
formats.

Tests: meson test -C build (273/273 passed)

Assisted-by: OpenAI Codex <codex@openai.com>
2026-08-26 17:17:20 -06:00
Behdad Esfahbod 472c3e0fd3 [draw] Add rendering work-budget API
Expose budget policy and remaining-work callbacks on draw and paint function
objects.  The public setters clamp invalid negative caps, distinguish finite
defaults from explicit unlimited operation, and report whether a backend
participates in accounting.

Let draw sessions borrow the live counter owned by draw_data.  OpenType glyf
and CFF traversal then debit the same counter in place, while unsupported
backends retain a finite per-glyph fallback.  Forward budget access through
the parent-font draw adaptor.

Document the new API and cover nil, default, unlimited, clamped, concrete,
and live-depletion behavior.

Tests: meson test -C build (272/272 passed)

Assisted-by: OpenAI Codex <codex@openai.com>
2026-08-26 17:11:21 -06:00
Behdad Esfahbod a1fa46cbde [limits] Streamline work-budget spending
Keep the hot spend primitive always-inline and branch-free.  Budget costs
and multipliers are unsigned and structurally bounded by their callers, so
the primitive only needs to debit the live counter and report exhaustion.

Move hb-limits.hh alongside the shared internal headers, after the paired
inline/noinline attribute definitions it now depends on.

Tests: meson test -C build (272/272 passed)

Assisted-by: OpenAI Codex <codex@openai.com>
2026-08-26 17:09:22 -06:00
Behdad Esfahbod 037aa79451 [fuzzing] Add rendering budget timeout seeds
Add the minimized vector, raster, and GPU testcases that exercise the
aggregate outline-expansion timeout.  Keeping the OSS-Fuzz testcase IDs
in their filenames makes each regression directly traceable.

The current leaf-level limits allow all three inputs to complete in the
optimized test build; later aggregate accounting will reduce their work
further.

Tests: meson test -C build

Assisted-by: OpenAI Codex <codex@openai.com>
2026-08-26 16:41:49 -06:00
Behdad Esfahbod 074b486d21 [limits] Add weighted outline work accounting
COLR can repeatedly request expensive glyf or CFF outlines, multiplying
their per-glyph structural limits.  Make both outline implementations
consume a caller-provided weighted budget so a later paint-session
counter can aggregate that work.

Add checked precharge arithmetic with stable exhaustion, charge glyf
loads, terminal points, and gvar tuples, and charge CFF operators,
subroutine calls, CFF2 blends, and both CFF1 seac outlines.  Keep a
generous local fallback for callers that do not yet provide a session
budget.

Tests: meson test -C build

Assisted-by: Codex <codex@openai.com>
2026-08-26 16:34:38 -06:00
Behdad Esfahbod 3f84c51ede [rust] Update HarfRust to 0.13.3
Assisted-by: Codex <codex@openai.com>
2026-08-25 14:38:53 -06:00
Behdad Esfahbod 03c3e45dfe [arabic] Enable Win1256 fallback on all platforms (#6209)
The Win1256 fallback is a static GSUB recipe selected by a narrow glyph-ID
signature; it does not call a Windows API. Enable it outside Windows too,
while retaining HB_NO_WIN1256 as an explicit opt-out.

Tested: meson test -C build; build with -DHB_NO_WIN1256

Assisted-by: Codex <codex@openai.com>
2026-08-25 10:00:11 -06:00
Behdad Esfahbod 299b37fe42 Merge pull request #6208 from harfbuzz/arabic-fallback-no-std-data
[arabic] Generate fallback data for no-std
2026-08-25 09:18:14 -06:00
Behdad Esfahbod af1ae4c9c6 [arabic] Generate fallback data for no-std
The HarfRust Arabic fallback cache now supports no-std builds. Emit the
presentation-form and ligature data without a std feature gate so the
generated Rust table is available there as well. The C++ output is unchanged.

Tested: meson test -C build

Assisted-by: Codex <codex@openai.com>
2026-08-25 09:17:04 -06:00
Behdad Esfahbod 4a3ef3a947 Merge pull request #6204 from Nussu06/depend-inputcount-page-overflow
[depend] Bound context inputCount to HB_MAX_CONTEXT_LENGTH
2026-08-25 07:58:24 -06:00
nussaiba shaikh 017621472d [bitset] Guard add_range/del_range against reversed element pointers
hb_bit_page_t::add_range and del_range compute la = &elt(a) and
lb = &elt(b) and memset the span between them. When a and b map to
element indices out of order within the page (la > lb), the pointer
difference (char *) lb - (char *) la goes negative and wraps to a
huge size_t, so the memset writes past the page.

Only run the multi-element branch when la < lb, and otherwise leave
the page untouched. The single-element (la == lb) branch is already
memory-safe. This defends the page code itself in addition to the
inputCount cap on the depend path.
2026-08-25 16:40:28 +05:30
Behdad Esfahbod 7356d8c3d1 [arabic] Generate fallback data for Rust (#6207)
Extend the Arabic table and PUA generators to emit the data needed by
HarfRust's fallback shaper. Require explicit PUA input paths so regeneration
does not depend on the current working directory, and pass them from the
HarfBuzz update makefile.

The generated C table payload remains byte-for-byte unchanged; its invocation
comment now records the required inputs. The Rust output passes HarfRust's full
workspace tests, no-std check, formatting, and strict clippy; the full
HarfBuzz Meson suite also passes.

Assisted-by: Codex <codex@openai.com>
2026-08-24 19:00:33 -06:00
Behdad Esfahbod ebdb43f4d9 [test] Add mort and morx text-rendering tests (#6206) 2026-08-24 14:54:55 -06:00
nussaiba shaikh 66ef3397ea [depend] Bound context inputCount to HB_MAX_CONTEXT_LENGTH
context_depend_recurse_lookups tracks covered sequence positions in
a fixed hb_bit_page_t, but the dependency-graph path never bounded
inputCount the way the shaping apply path does. A crafted GSUB
contextual rule with glyphCount past the page (e.g. 512) and a
SubstLookupRecord whose sequenceIndex lands in a higher page element
than endIndex makes hb_bit_page_t::add_range compute its memset
length as (char*)lb - (char*)la, which is negative and wraps to a
huge size_t, writing past the stack-allocated page. Reachable from
hb_subset_depend_from_face_or_fail on a crafted face.

Cap inputCount at HB_MAX_CONTEXT_LENGTH before the page is used,
matching the apply path; rules longer than that never apply during
shaping, so no real dependency edges are lost. Adds a regression
seed exercised by the depend fuzzer.
2026-08-24 14:57:59 +05:30
Behdad Esfahbod 485df9d575 [harfrust] Update to 0.13.2 (#6203) 2026-08-24 00:02:12 -06:00
Behdad Esfahbod 4a7dcd350a [harfrust] Store shape-plan data directly (#6201) 2026-08-23 22:42:03 -06:00
Behdad Esfahbod 5bfff718f1 [harfrust] Reuse cached per-shape metadata (#6200) 2026-08-23 21:58:04 -06:00
Behdad EsfahbodandClaude Fable 5 b04cd416f3 [aat] Force-inline the state-machine transition functions (#6198)
The subtables' transition functions are called once per transition
from the drive loop, but are big enough that the compiler declines to
inline them, leaving a call plus prologue in the hottest loop of AAT
shaping. Force them inline via HB_AAT_TRANSITION_INLINE, defined away
under HB_OPTIMIZE_SIZE.

Measured on the AAT corpus (clang -O3, instructions/cycles vs main):
Lucida Grande -10.3%/-5.2%, Menlo -6.6%, GeezaPro -5.6%/-4.4%,
Devanagari Sangam -5.6%/-2.2%. The ligature machine's transition alone
carries most of the Menlo/Lucida win. Code size: +16KB text (+0.65%).
Outputs byte-identical on the AAT corpus plus PingFang over a CJK
sample.

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-22 22:24:44 -06:00
Behdad EsfahbodandClaude Fable 5 29c1b91df1 [ft] Fix -Wdouble-promotion warnings in get_glyph_extents (#6196)
Make the float-to-double widening of the roundf() results explicit,
matching the treatment in glyf.hh from the previous round.

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-22 13:09:35 -06:00
ff217d3039 [graphite2] Fix cluster values when the buffer was reversed (#6156)
When the requested direction differs from the script's horizontal
direction, the Graphite2 shaper reverses the buffer before segmenting.
Input cluster values then decrease along the buffer, so assigning each
Graphite group the cluster of its base character can leave earlier
characters outside the resulting cluster map.

Preserve the source span of every Graphite glyph while constructing the
output. At monotone cluster levels, seed each glyph from the minimum
endpoint of that span and use merge_clusters() over each Graphite output
range. Keep the historical base-character behavior at non-monotone
levels.

HarfBuzz cluster values are opaque client labels and can be repeated or
gapped. Merging them can therefore coalesce multiple Graphite layout
groups. Preserve the Graphite group index separately for positioning so
every group's advance is emitted even when its HarfBuzz cluster was
merged. Validate Graphite slot source bounds before indexing the input.

Add shape regressions using a subset of Awami Nastaliq and an API test
with repeated input clusters. The latter used to merge the output
clusters correctly but drop an entire layout-group advance, reducing
the total from 3200 to 1269.

Tested with:
meson test -C build test-graphite2
meson test -C build --suite shape
meson test -C build



Assisted-by: Codex <codex@openai.com>

Co-authored-by: Michael Brown <michael.brown@rubicotech.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-22 12:48:13 -06:00
Behdad EsfahbodandClaude Fable 5 bc9e8db59b [algs] Add a float overload of hb_clamp_to (#6195)
Call sites feeding roundf/floorf/ceilf results into hb_clamp_to
promoted float to double implicitly, tripping -Wdouble-promotion under
clang. The overload widens explicitly — float cannot represent all
integer bounds exactly, so the clamp itself stays in double — and the
two glyf extents assignments that promote outside hb_clamp_to get
explicit casts. No behavior change.

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-21 15:07:28 -06:00
Behdad EsfahbodandClaude Fable 5 40949cc70b [ot-layout] Skip the per-subtable digest retest for single-subtable lookups (#6194)
The lookup accelerator's digest is the union of its subtables'
digests, and apply_forward/apply_backward test it before applying the
lookup. For lookups with one subtable - the overwhelming majority -
the subtable's own digest is identical to the accelerator's, so the
per-subtable may_have retest in hb_applicable_t::apply is always true.
Dispatch single-subtable lookups straight to the apply function.

Invalid subtables do not collect and used to rely on their zeroed
entry's empty digest to gatekeep the null apply funcs; trim the zeroed
tail entries at accelerator create time instead, so the apply paths
never see them.

Benchmark-shape instruction counts: Roboto/en-thelittleprince -2.3%,
NotoNastaliqUrdu/fa-thelittleprince -3.7%; outputs unchanged.

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-21 14:46:46 -06:00
Behdad EsfahbodandClaude Fable 5 8273c6d2ad [rust] Batch nominal-glyph lookups in the harfrust bridge (#6193)
Implement harfrust's new FontFuncs::populate_nominal_glyphs callback
(harfrust 0.13.1) with a single hb_font_get_nominal_glyphs call per
run, replacing one FFI round-trip per character during the
normalizer's short-circuit prefill. Speeds up the harfrust side of the
benchmark-shape parity suite by 6.2% on Menlo and 4.6% on
LucidaGrande; shaping outputs are unchanged.

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-21 13:19:37 -06:00
Behdad EsfahbodandClaude Fable 5 d155a10dff [ot-shape] Skip the post-morx digest update if not applying GPOS (#6192)
The buffer digest is only consumed by the OT lookup-apply loop. After
morx substitution it exists solely for a following GPOS pass; when
positioning goes through kerx/kern instead, rebuilding it is wasted
work. Speeds up the Menlo/en-thelittleprince benchmark by 2%; shaping
outputs are unchanged on the AAT benchmark fonts.

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-21 01:10:56 -06:00
Behdad EsfahbodandClaude Fable 5 cf30f712e7 [buffer] Charge move_to moves against max_ops (#6191)
move_to()'s two memmoves - the forward copy into out_info and the
rewind copy-back - were not charged against max_ops, while
shift_forward() already charges its moved tail. A morx insertion
subtable that keeps its mark far behind the insertion point can force
an O(distance) rewind plus re-copy per insertion action, for O(N^2)
total work that the max_ops budget never sees.

Charge count in both branches, mirroring shift_forward()'s failure
pattern. Charging only the rewind branch would also bound the
quadratic, but both memmoves are attacker-drivable work, and
symmetric accounting is simpler to reason about.

Fixes https://github.com/harfbuzz/harfbuzz/issues/6190

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-20 18:10:34 -06:00
Behdad EsfahbodandClaude Fable 5 24b368ec17 [GDEF] Answer mark-filtering-set queries from a cached bit-page (#6188)
Mark-filtering-set checks during lookup application went through a
set-digest pre-filter and then a Coverage binary search in the GDEF
table, for every candidate glyph on every skippy-iterator step.  For
fonts that make heavy use of UseMarkFilteringSet lookups this
dominates shaping time.

Cache each mark set in the GDEF accelerator instead: a set whose
glyphs span fewer than 512 gids (the common case; 102 of 123 sets in
Noto Sans Duployan, 16 of 30 in Gulzar) is flattened into a single
hb_bit_page_t biased at its first glyph, answering exactly in O(1)
with no table access.  Wider sets (eg. possible with 24-bit gids)
keep a set-digest in the same union as a pre-filter ahead of the
out-of-line table fallback, so memory per set is one fixed 68-byte
entry either way.

Shaping the Duployan corpus with Noto Sans Duployan drops 18% in both
instructions retired and wall time (5.25s to 4.27s for three passes of
perf/texts/duployan.txt).  Amiri, Gulzar, Nastaliq, Devanagari, and
Latin fonts are unchanged within noise, with instruction counts
verified flat.

Also adds an HB_NEVER_INLINE macro, used to keep the table fallback
out of the inlined hot path.

Tested with meson test (all 269 tests pass).

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-20 12:37:43 -06:00
Behdad Esfahbod ee9f8d9489 Merge pull request #6187 from harfbuzz/agent/subset-perf
[subset] Misc speedups
2026-08-20 12:37:12 -06:00
Garret Rieger f279195ce7 Remove accidentally committed __pycache__ file. 2026-08-20 11:44:38 -06:00
Behdad EsfahbodandClaude Fable 5 f5ce090554 [subset] Remap ClassDef glyphs through the flat array
Extend the flat old-to-new gid array introduced for Coverage::subset
to ClassDef subsetting: add a map_gsub_glyph() helper on the plan and
use it in ClassDefFormat1/2::subset in place of per-glyph
glyph_map_gsub hash lookups.  The use_class_zero glyph counts switch
from the map to the equivalent glyphset_gsub set.

Tested with meson test (all tests pass).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-20 05:25:15 -06:00
Behdad EsfahbodandClaude Fable 5 f6bcedab83 [subset/cff] Record byte segments instead of tokens when not analyzing hints
The CFF subroutine subsetter recorded one parsed_cs_op_t entry per
charstring token (every operand and operator), then merged adjacent
entries back together in a separate compact() pass.  When hints are
not being analyzed (not dropping hints and not populating the subset
accelerator), per-op granularity is never needed: the only structure
that matters is the subroutine-call sites, whose numbers are
re-encoded with new biases.

In that mode, only note token boundaries (two integer stores) and
flush verbatim byte segments at call sites and at the end of each
string.  CFF1's return/endchar bytes flush through add_op; CFF2 has
no such ops in the stream, so flush explicitly where the interpreter
synthesizes them at end-of-string.  This eliminates the compact()
pass, shrinks the parsed representation by an order of magnitude,
and add_op inlines into the interpreter loop again.

Speeds up CFF subsetting: hb-subset on SourceHanSans (10K glyphs,
all unicodes) improves by ~9.5% total CPU (interleaved A/B against
main with perf stat).  Layout-heavy and glyf fonts unaffected.

Tested with meson test (all 267 tests pass, including CFF1/CFF2
golden-file subset comparisons).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-20 05:21:02 -06:00
Behdad EsfahbodandClaude Fable 5 7990062241 [subset] Remap Coverage glyphs through a flat array
Coverage::subset remapped every covered glyph through the
glyph_map_gsub hash map, paying a hash lookup per glyph.  Build a flat
old-gid-indexed array of new gids (HB_MAP_VALUE_INVALID holes for
unretained glyphs) once per plan, sized to the highest retained GSUB
gid, and index it instead.  The array doubles as the retained-glyph
filter, and the lambda mapper also inlines into the iterator pipeline
where the hb_map_t mapper did not.

Speeds up subsetting layout-heavy fonts: benchmark-subset
subset_unicodes on NotoNastaliqUrdu at 512 codepoints improves by 12%
(26.2ms to 22.9ms), Amiri at 512 by 9%.  No measurable change on
small subsets or glyf-heavy CJK fonts.

Tested with meson test (all 267 tests pass).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-20 05:21:02 -06:00
Behdad Esfahbod cb9460bcbb Merge pull request #6185 from harfbuzz/agent/depend-context-memoization
[depend] Speed up contextual dependency collection
2026-08-20 05:20:24 -06:00
Behdad EsfahbodandClaude Fable 7c349a3f89 [depend] Ignore edge flags in recursion memoization
Edge flags affect neither traversal nor edge identity. A state revisited
with different flags can therefore only attempt edges already present in
the deduplication table, whose first-discovered flags already win. Leave
context-set identity in the key because it does distinguish edges.

This reduces Noto Nastaliq dependency construction from about 110ms and
1.25B instructions to about 106ms and 1.21B instructions.

Dependency graphs match on Noto Nastaliq, Noto Sans Duployan, Gulzar,
Iran Nastaliq, Noto COLRv1, Amiri, and Source Serif Variable.

Tested with: meson test -C build

Co-Authored-By: Claude Fable <noreply@anthropic.com>
Assisted-by: OpenAI Codex <codex@openai.com>
2026-08-20 04:53:24 -06:00
Behdad EsfahbodandClaude Fable 8d0b8738ab [depend] Skip cached chained-context preparation
A chained contextual rule with cached record labels no longer needs to
rebuild its backtrack, lookahead, or input-position sets. Reuse the
resolved labels and recurse directly.

Do not take the fast path for rules with no valid lookup records. Those
rules have no label-cache entries, but their original preparation can
still allocate dependency sets in an observable order.

On Noto Nastaliq, compile-only dependency construction falls from about
133ms and 1.58B instructions to about 110ms and 1.25B instructions.
That is about 4,237x faster than the original 7:44.77 run.

Dependency graphs match on Noto Nastaliq, Noto Sans Duployan, Gulzar,
Iran Nastaliq, Noto COLRv1, Amiri, and Source Serif Variable.

Tested with: meson test -C build

Co-Authored-By: Claude Fable <noreply@anthropic.com>
Assisted-by: OpenAI Codex <codex@openai.com>
2026-08-20 04:47:15 -06:00
Behdad Esfahbod 980cceb98e [depend] Reuse cached context label lookups
The all-cached precheck already resolves every context-set index, but
the recurse loop looked up each key a second time. Retain those indices
in per-depth scratch storage and consume them directly on the cached
path.

On Noto Nastaliq this reduces compile-only construction from about
136ms to about 133ms while preserving byte-identical graph output.

Tested with: meson test -C build

Assisted-by: OpenAI Codex <codex@openai.com>
2026-08-20 04:39:44 -06:00
Behdad Esfahbod 75455f7338 [depend] Skip cached contextual input expansion
Once every lookup record's context label is cached, the expanded input
position glyph sets are no longer consumed. Check the exact label keys
first and bypass that side-effect-free preparation on a full cache hit.

Keep chained backtrack and lookahead set interning in place because its
allocation order determines observable dependency-set indices.

On Noto Nastaliq, compile-only dependency construction falls from about
151ms and 1.87B instructions to about 136ms and 1.59B instructions. The
original 7:44.77 run is now about 3,420x slower.

Dependency graphs match the pre-change output for Noto Nastaliq,
Noto Sans Duployan, Gulzar, Iran Nastaliq, Noto COLRv1, Amiri, and
Source Serif Variable.

Tested with: meson test -C build

Assisted-by: OpenAI Codex <codex@openai.com>
2026-08-20 04:36:35 -06:00