Skip to content

refactor: hoist _node_name out of inner loop in get_entity_context (#806)#809

Open
suhaniiz wants to merge 1 commit into
param20h:devfrom
suhaniiz:refactor/hoist-node-name-loop-806
Open

refactor: hoist _node_name out of inner loop in get_entity_context (#806)#809
suhaniiz wants to merge 1 commit into
param20h:devfrom
suhaniiz:refactor/hoist-node-name-loop-806

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 #806


📝 What does this PR do?

This PR optimizes the performance of the knowledge graph retrieval phase within get_entity_context.

Previously, the statement left = _node_name(graph, node_id) was evaluated inside the tight inner loop (for neighbor_id in neighbors:). Because node_id remains completely constant throughout the iteration of its neighbors, computing its string name repeatedly was redundant.

This change hoists the _node_name evaluation up exactly one block level into the outer loop, caching the string value once per matched node. This saves significant CPU cycles when handling highly connected query 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 refactor strictly alters where the variable assignment happens without modifying the underlying dictionary key generation or graph processing logic. No side effects or breaking changes are introduced.


✅ 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 14:42
@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.

[Refactor]: Hoist _node_name evaluation out of the inner loop in get_entity_context

1 participant