Heuristic:AGenUI AGenUI MeasureFunc Decision Policy
| Knowledge Sources | |
|---|---|
| Domains | UI_Framework, Optimization |
| Last Updated | 2026-10-24 17:21 GMT |
Overview
A 6-rule priority-order decision policy that determines whether to register or clear a Yoga `measureFunc` on a component node, preventing measurement callbacks from shadowing explicit styles or platform-locked sizes.
Description
Yoga's `measureFunc` is a powerful mechanism for intrinsic sizing, but it has a critical side effect: once set, it shadows all style-based sizing (explicit pixels, aspect-ratio). This leads to layout oscillation and wasted measurement calls when the node could be sized directly from styles.
The `shouldUseMeasureFunc()` method in `MeasurementManagerImpl` implements a priority-order rule set (first match wins) that decides whether to `Register` or `Clear` the measureFunc. The rules are evaluated in strict order to avoid both shadowing and Yoga's hard constraint (measureFunc + children are mutually exclusive).
Usage
Use this heuristic when:
- You are extending AGenUI with custom component types that need platform measurement
- You are debugging layout oscillation or measurement cost on a component
- You need to understand why a measureFunc is or is not registered for a given component
The Insight (Rule of Thumb)
The 6 rules evaluated in priority order (first match wins):
- No implementation registered → Clear. No `IMeasurement` for this type; clear any residual measureFunc to avoid drift.
- Platform size locked' → Clear. Platform reported a concrete size; measureFunc must yield so Yoga trusts the platform value (prevents oscillation).
- Has children (without opt-in) → Clear. Yoga hard rule: `measureFunc + children` are mutually exclusive unless `allowsMeasureWithChildren()` returns true (e.g. Tabs).
- Both axes explicit pixels → Clear. Yoga can size directly from styles; a measureFunc would add cost and risk shadowing.
- Aspect-ratio set + at least one axis solvable → Clear. Yoga can derive the other axis via aspect-ratio. If both axes are auto, fall through.
- None of the above → Register. Yoga has no other way to size this node.
- Trade-off: Registering measureFunc gives accurate intrinsic sizing but costs a platform callback per Yoga layout pass. Clearing avoids the callback but requires styles to fully specify size.
- Value: Rule evaluation is O(1) — pure string checks, no Yoga calls.
Reasoning
Yoga enforces exactly one hard rule internally: a node with a measureFunc cannot have children (`YGNodeInsertChild` asserts `!YGNodeHasMeasureFunc`). All other rules are business optimizations documented in the source:
From `agenui_measurement_manager.cpp:45-55`:
// Mapping against Yoga source reality:
// * Yoga itself does NOT decide whether to invoke a measureFunc — once set,
// leaf evaluation will always call it.
// * Yoga enforces exactly one rule internally: a node with a measureFunc
// cannot have children (YGNodeInsertChild asserts !YGNodeHasMeasureFunc).
// * All other rules below are business optimisations: registering measureFunc
// would otherwise SHADOW style-based size (explicit px / aspect-ratio).
The rule implementation from `agenui_measurement_manager.cpp:76-119`:
MeasureDecision shouldUseMeasureFunc(const std::string& type, const MeasureDecisionContext& ctx) {
// [Rule 1] No measurement implementation registered → Clear
if (!impl) return MeasureDecision::Clear;
// [Rule 2] Platform has reported a concrete size → Clear
if (ctx.platformSizeLocked) return MeasureDecision::Clear;
// [Rule 3] measureFunc + children mutually exclusive → Clear
if (ctx.hasChildren && !impl->allowsMeasureWithChildren()) return MeasureDecision::Clear;
// [Rule 4] Both axes explicit pixels → Clear
if (isExplicitPx(ctx.widthStyle) && isExplicitPx(ctx.heightStyle)) return MeasureDecision::Clear;
// [Rule 5] aspect-ratio set + at least one axis solvable → Clear
if (isValidAspectRatio(ctx.aspectRatioStyle)) {
if (anySolvable) return MeasureDecision::Clear;
// fall through: both axes auto, Yoga needs measureFunc for intrinsic size
}
// [Rule 6] Register measureFunc: Yoga has no other way to size this node
return MeasureDecision::Register;
}