Skip to content

Conversation

@Nondukishor
Copy link

Rewritten LRU with safer flows, explicit APIs, and improved cleanup handling

Summary:

This PR rewrites the internal LRU cache implementation to improve safety, maintainability, and predictable behavior. The previous implementation relied on implicit assumptions about node validity, list integrity, and scheduler availability. This updated version strengthens those areas and introduces a more explicit and robust API.

Key Improvements

  • Stricter Flow types for Entry and the public LRU interface.
  • Stronger doubly-linked-list invariants with defensive guards against corrupted or deleted entries.
  • Safe fallback scheduling when React Scheduler is unavailable.
  • More reliable cleanup behavior, ensuring that onDelete errors do not interrupt eviction.
  • Explicit and expanded APIs, including:
    • add
    • access
    • update
    • remove
    • setLimit
    • dump
    • getSize
    • getLimit
  • Immediate cleanup scheduling within add() to prevent uncontrolled memory growth.
  • Frozen public API object to avoid accidental mutation.
  • Improved GC friendliness by fully detaching deleted nodes.
  • Enhanced debugging utilities through dump() and size helpers.

Why this change?

  • While functional, the earlier implementation had several limitations:
  • Accessing or updating deleted entries could happen silently.
  • Corrupted circular list states were not detected.
  • Errors thrown inside onDelete could interrupt eviction.
  • No direct method existed to remove an entry explicitly.
  • Lack of scheduler fallback.
  • Debugging and inspecting internal cache state was difficult.
    This rewrite improves the resilience of the LRU cache and ensures predictable cleanup behavior even in error scenarios.

##How did you test this change?

  • Manually tested list behavior including add, access, update, and remove.
  • Verified correct eviction ordering by exceeding cache limits.
  • Simulated both presence and absence of the React Scheduler to confirm fallback logic.
  • Ensured errors thrown inside onDelete do not break cleanup.
  • Verified deleted entries cannot be accessed or updated.

Ran the full test suite:
yarn test
yarn test --prod

Verified Flow type checks:
yarn flow

Verified formatting and linting:
yarn prettier
yarn lint / yarn linc

Rewritten LRU with safer flows, explicit APIs, and improved cleanup handling.
@meta-cla
Copy link

meta-cla bot commented Dec 2, 2025

Hi @Nondukishor!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla meta-cla bot added the CLA Signed label Dec 2, 2025
@meta-cla
Copy link

meta-cla bot commented Dec 2, 2025

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant