[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>
This commit is contained in:
Behdad Esfahbod committed 2026-08-26 17:09:22 -06:00
1 parent 037aa79451
commit a1fa46cbde
4 files changed
+22 -40

No files matched your search

+1 -1
View File
@@ -189,7 +189,7 @@ struct cs_interp_env_t : interp_env_t<ARG>
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; }
+17 -31
View File
@@ -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
+1 -1
View File
@@ -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"
+3 -7
View File
@@ -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);