Skip to content

perf: optimize fallback entity matching via lazy metadata caching (#805)#810

Open
suhaniiz wants to merge 1 commit into
param20h:devfrom
suhaniiz:optimization/fallback-matching-805
Open

perf: optimize fallback entity matching via lazy metadata caching (#805)#810
suhaniiz wants to merge 1 commit into
param20h:devfrom
suhaniiz:optimization/fallback-matching-805

Conversation

@suhaniiz

@suhaniiz suhaniiz commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

📋 PR Checklist

Thank you for contributing to PDF-Assistant-RAG! 🎉
Please fill out this template before submitting. PRs without it filled in will be closed.


🔗 Related Issue

Closes #805


📝 What does this PR do?

This PR optimizes the fallback entity matching logic inside _match_query_nodes to eliminate expensive $O(N)$ full-graph sequential scans on every incoming fallback query.

Changes introduced:

  • Implemented a lazy-loading inverted index mechanism (_name_to_node_id) cached directly inside the NetworkX graph.graph metadata dictionary.
  • On the initial fallback execution for a given graph, a dictionary mapping lowercased entity names to their respective node_ids is pre-computed exactly once.
  • Subsequent fallback lookups bypass linear array iterations entirely, executing lookups over unique entity strings in $O(M)$ time where $M$ is bounded by unique names, drastically reducing query execution latency for dense or massive knowledge graphs.

🗂️ Type of Change

  • 🐛 Bug fix
  • ✨ New feature
  • 🔧 Refactor / code cleanup
  • 📝 Documentation update
  • 🎨 UI / styling change
  • ⚙️ CI / tooling / config change
  • 🧪 Tests

🧪 How was this tested?

  • Ran the backend locally (uvicorn app.main:app --reload)
  • Tested the affected API endpoints manually
  • Added / updated tests

📸 Screenshots (if UI change)

N/A


⚠️ Anything to flag for reviewers?

The lookup index is cleanly scoped within NetworkX's native graph property metadata dictionary, meaning it remains tied to the lifetime of the graph object in memory without introducing global state bottlenecks or database mutation concerns.


✅ Self-Review Checklist

  • My branch is based on dev, not main
  • I have not added any secrets / API keys
  • I have not modified main branch or any HuggingFace deployment config
  • My code follows the existing style (no unnecessary formatting changes)
  • I have updated relevant docs / comments if needed

@suhaniiz suhaniiz requested a review from param20h as a code owner July 7, 2026 16:05
@vercel

vercel Bot commented Jul 7, 2026

Copy link
Copy Markdown

@suhaniiz is attempting to deploy a commit to the param20h's projects Team on Vercel.

A member of the Team first needs to authorize it.

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.

[Enhancement]: Optimize fallback entity matching in _match_query_nodes to avoid full graph scans

1 participant