Skip to content

fix: incorrect cache validation logic causing cache false hit#1965

Open
MaksimGalois wants to merge 1 commit into
facebook:mainfrom
MaksimGalois:fix/incorrect-cache-invalidation
Open

fix: incorrect cache validation logic causing cache false hit#1965
MaksimGalois wants to merge 1 commit into
facebook:mainfrom
MaksimGalois:fix/incorrect-cache-invalidation

Conversation

@MaksimGalois
Copy link
Copy Markdown

Summary

In YGLayout.cpp, when calling newSizeIsStricterAndStillValid to check measure cache eligibility, the current constraint size is passed as net remaining space (availableHeight - marginColumn). However, the historical constraint lastSize is erroneously passed as gross space (lastAvailableHeight) without subtracting the margin.

This asymmetric comparison (comparing net space with gross space) leads Yoga to falsely assume that the available space has shrunk (triggering the lastSize > size branch) when the container actually grows. Consequently, a truncated cache result from a previously compressed pass is incorrectly reused.

Solution

Deduct the corresponding margin size (marginColumn / marginRow) from lastAvailableHeight / lastAvailableWidth respectively when calling newSizeIsStricterAndStillValid to ensure symmetrical net-to-net boundary comparisons.

Changelog

[C++] [Fixed] - Correct net layout space comparison in measure cache check by deducting margin.

Test Plan & Minimal Reproducible Example

Added a new dedicated unit test remeasure_when_container_grows_with_child_margin in YGMeasureCacheTest.cpp, which serves as a MRE for this specific bug.

  • Before this fix: The test fails as the child incorrectly hits the stale measure cache and retains a compressed height of 5.0f instead of re-measuring to its desired height of 10.0f.
  • After this fix: The test passes successfully with all assertions green.

Reproduction and Verification Steps:

  1. First Layout Pass: The parent container is initialized with a restricted height (10.0f). Due to a child margin (5.0f), the net available space for the child is constrained to 5.0f. The child's measured layout height is compressed to 5.0f and cached erroneously.
  2. Container Expansion: The parent container's height expands to 15.0f, releasing enough net available space (10.0f) to completely accommodate the child's desired target height (10.0f).
  3. Second Layout Pass (The Bug Location):
    • Before this fix: Due to the asymmetric boundary check in newSizeIsStricterAndStillValid, Yoga incorrectly triggers a false cache hit during the second pass. It blindly reuses the stale, compressed height of 5.0f from the cache without re-measuring, causing the measureCount assertion to fail (retains 1 instead of 2).
    • After this fix: Symmetrical net-to-net boundary validation ensures that the cache is properly invalidated when space expands. Yoga correctly triggers a re-measurement (measureCount increments to 2) and the child layout height successfully recovers to its correct desired height of 10.0f. All assertions pass green.

@vercel
Copy link
Copy Markdown

vercel Bot commented May 25, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
yoga-website Ready Ready Preview, Comment May 25, 2026 7:56am

Request Review

@meta-cla meta-cla Bot added the CLA Signed label May 25, 2026
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label May 25, 2026
@meta-codesync
Copy link
Copy Markdown

meta-codesync Bot commented May 26, 2026

@zeyap has imported this pull request. If you are a Meta employee, you can view this in D106414616.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant