Skip to content

optimized O(n²) pool lookup to O(n) with Map in getPoolsBorrow#2716

Open
adityamane765 wants to merge 1 commit into
DefiLlama:masterfrom
adityamane765:fix/pool-lookup-map
Open

optimized O(n²) pool lookup to O(n) with Map in getPoolsBorrow#2716
adityamane765 wants to merge 1 commit into
DefiLlama:masterfrom
adityamane765:fix/pool-lookup-map

Conversation

@adityamane765
Copy link
Copy Markdown

@adityamane765 adityamane765 commented May 30, 2026

getPoolsBorrow was calling pools.find() inside a .map() over lendBorrow -> resulting in O(n²) comparisons on every request. With thousands of pools on both sides this means millions of string comparisons per call. Replaced with a Map keyed by pool id built once before the loop, reducing the join to O(n).

Summary by CodeRabbit

  • Refactor
    • Improved performance of pool borrow data retrieval operations for faster response times.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 30, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: ad3962e0-5e02-4670-a83e-4c5ab6224260

📥 Commits

Reviewing files that changed from the base of the PR and between daaad80 and e2f90c2.

📒 Files selected for processing (1)
  • src/api/controllers/enriched.js

📝 Walkthrough

Walkthrough

This PR optimizes the getPoolsBorrow function in the enriched pools controller by replacing a per-item linear array search with a Map-based lookup keyed by pool identifier. The response shape and sorting behavior remain unchanged.

Changes

Pool lookup optimization

Layer / File(s) Summary
Pool lookup optimization in getPoolsBorrow
src/api/controllers/enriched.js
The borrow-side enrichment now constructs a poolsMap keyed by pool and uses Map.get() for lookups instead of iterating through the array with pools.find() for each item.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

🐰 A Map appears where searches once did toil,
No more shall linear scans make lookups coil,
Pool by pool, fast keys now guide the way,
O(1) hops replace O(n)'s delay! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly describes the main optimization: replacing O(n²) pool lookup with O(n) using a Map in getPoolsBorrow, which matches the primary change in the changeset.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant