From a1fa46cbde3cb6d8238a7d27c8b78a3fb41c85a8 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 26 Aug 2026 17:09:22 -0600 Subject: [PATCH] [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 --- src/hb-cff-interp-cs-common.hh | 2 +- src/hb-limits.hh | 48 ++++++++++++---------------------- src/hb.hh | 2 +- src/test-algs.cc | 10 +++---- 4 files changed, 22 insertions(+), 40 deletions(-) diff --git a/src/hb-cff-interp-cs-common.hh b/src/hb-cff-interp-cs-common.hh index eb1d11229..fd91dc4c4 100644 --- a/src/hb-cff-interp-cs-common.hh +++ b/src/hb-cff-interp-cs-common.hh @@ -189,7 +189,7 @@ struct cs_interp_env_t : interp_env_t void set_budget (int64_t *budget_) { budget = budget_; } int64_t *get_budget () const { return budget; } - bool spend_budget (int64_t cost, int64_t mult = 1) + bool spend_budget (unsigned int cost, unsigned int mult = 1) { return !budget || hb_budget_spend (*budget, cost, mult); } const number_t &get_x () const { return pt.x; } diff --git a/src/hb-limits.hh b/src/hb-limits.hh index 854351f64..6c7f3d180 100644 --- a/src/hb-limits.hh +++ b/src/hb-limits.hh @@ -163,17 +163,17 @@ /* Fixed relative weights. Keep the value in the name so charge sites are * easy to audit; tune the session caps below, not these values. */ -#define HB_BUDGET_1 ((int64_t) 1) -#define HB_BUDGET_2 ((int64_t) 2) -#define HB_BUDGET_4 ((int64_t) 4) -#define HB_BUDGET_8 ((int64_t) 8) -#define HB_BUDGET_16 ((int64_t) 16) -#define HB_BUDGET_32 ((int64_t) 32) -#define HB_BUDGET_64 ((int64_t) 64) -#define HB_BUDGET_128 ((int64_t) 128) -#define HB_BUDGET_256 ((int64_t) 256) -#define HB_BUDGET_512 ((int64_t) 512) -#define HB_BUDGET_1024 ((int64_t) 1024) +#define HB_BUDGET_1 1u +#define HB_BUDGET_2 2u +#define HB_BUDGET_4 4u +#define HB_BUDGET_8 8u +#define HB_BUDGET_16 16u +#define HB_BUDGET_32 32u +#define HB_BUDGET_64 64u +#define HB_BUDGET_128 128u +#define HB_BUDGET_256 256u +#define HB_BUDGET_512 512u +#define HB_BUDGET_1024 1024u /* A generous per-glyph fallback. Paint backends use their session cap so * repeated outline traversals consume one aggregate budget. */ @@ -181,27 +181,13 @@ #define HB_BUDGET_GLYPH ((int64_t) 1 << 26) #endif -/* Precharge COST * MULT without signed overflow. Exhaustion is latched at - * -1 so repeated calls remain safe and fail cheaply. */ -static inline bool -hb_budget_spend (int64_t &budget, int64_t cost, int64_t mult = 1) +/* Precharge COST * MULT. Callers bound COST and MULT structurally, and live + * budgets are concrete non-negative values when a session starts. */ +static HB_ALWAYS_INLINE bool +hb_budget_spend (int64_t &budget, unsigned int cost, unsigned int mult = 1) { - if (cost < 0 || mult < 0 || - (cost && mult > INT64_MAX / cost)) - { - budget = -1; - return false; - } - - int64_t charge = cost * mult; - if (budget < charge) - { - budget = -1; - return false; - } - - budget -= charge; - return true; + budget -= cost * mult; + return budget >= 0; } /* One VARC glyph (draw or extents), shared by all leaf glyphs loaded diff --git a/src/hb.hh b/src/hb.hh index 27f400a8e..0c6628a6f 100644 --- a/src/hb.hh +++ b/src/hb.hh @@ -160,7 +160,6 @@ #include "hb-config.hh" -#include "hb-limits.hh" /* @@ -557,6 +556,7 @@ extern "C" void hb_free_impl(void *ptr); /* Headers we include for everyone. Keep topologically sorted by dependency. * They express dependency amongst themselves, but no other file should include * them directly.*/ +#include "hb-limits.hh" #include "hb-cplusplus.hh" #include "hb-meta.hh" #include "hb-mutex.hh" diff --git a/src/test-algs.cc b/src/test-algs.cc index 102837857..ead87e28e 100644 --- a/src/test-algs.cc +++ b/src/test-algs.cc @@ -112,7 +112,7 @@ main (int argc, char **argv) hb_always_assert (!hb_budget_spend (budget, 3)); hb_always_assert (budget == -1); hb_always_assert (!hb_budget_spend (budget, 1)); - hb_always_assert (budget == -1); + hb_always_assert (budget == -2); budget = 0; hb_always_assert (hb_budget_spend (budget, 0)); @@ -120,12 +120,8 @@ main (int argc, char **argv) hb_always_assert (budget == -1); budget = INT64_MAX; - hb_always_assert (!hb_budget_spend (budget, INT64_MAX, 2)); - hb_always_assert (budget == -1); - - budget = INT64_MIN + 1; - hb_always_assert (!hb_budget_spend (budget, 1)); - hb_always_assert (budget == -1); + hb_always_assert (hb_budget_spend (budget, 1024)); + hb_always_assert (budget == INT64_MAX - 1024); x = 1; hb_always_assert (++hb_inc (x) == 3);