Skip to content

improvement(knowledge): react-doctor perf pass across the knowledge base module#5332

Merged
waleedlatif1 merged 4 commits into
stagingfrom
worktree-kb-react-doctor
Jul 1, 2026
Merged

improvement(knowledge): react-doctor perf pass across the knowledge base module#5332
waleedlatif1 merged 4 commits into
stagingfrom
worktree-kb-react-doctor

Conversation

@waleedlatif1

Copy link
Copy Markdown
Collaborator

Summary

  • Performance-only pass over the Knowledge Base module guided by react-doctor — findings 95 → 69, clearing every JS-perf, async, lazy-ref, and module-scope category and fixing the displayChunks exhaustive-deps cluster.
  • Memoize unstable array fallbacks that were defeating downstream memos (displayChunks in document.tsx, requiredScopes in connectors-section).
  • Lazy-init the cooldownTimers Set ref instead of allocating a new Set every render.
  • Hoist the pure buildUploadedFile helper to module scope so it isn't rebuilt per render.
  • Collapse filter().map() / map().filter(Boolean) chains into single reduce/flatMap passes.
  • Replace find()-in-a-loop with Map lookups (O(n²) → O(n)).
  • [...arr].sort()arr.toSorted(); parallelize independent server-component awaits with Promise.all.
  • Add a paired sim-react-performance rule (.claude + .cursor) documenting these patterns for future work.

All changes are behavior-preserving; the risky architectural refactors react-doctor also flags (god-component splits, useReducer conversions, prop-callback rearchitecture) were intentionally left out of scope.

Type of Change

  • Improvement (performance)

Testing

  • tsc --noEmit: 0 errors; biome check: clean.
  • Three independent parallel audits (behavioral-equivalence, React-hooks-correctness, type/build) each returned zero regressions.

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel

vercel Bot commented Jul 1, 2026

Copy link
Copy Markdown

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

Project Deployment Actions Updated (UTC)
docs Ready Ready Preview, Comment Jul 1, 2026 8:22pm

Request Review

@cursor

cursor Bot commented Jul 1, 2026

Copy link
Copy Markdown

PR Summary

Low Risk
Changes are micro-optimizations and documentation; no auth, billing, or API contract changes, with logic preserved via equivalent reduce/Map/memo patterns.

Overview
Performance-only pass across the Knowledge Base module (react-doctor–guided), with no intended product behavior changes.

React render paths: Memoizes unstable lists so downstream hooks stop thrashing— notably displayChunks in the document chunk view (search pagination .slice() no longer allocates a fresh array every render) and requiredScopes on connector cards. Lazy-inits the sync cooldown timer Set ref instead of allocating each render, and hoists the pure buildUploadedFile helper to module scope in the upload hook. Replaces repeated find() in loops with Map lookups (tag definitions, connector config fields, selector dependencies). Collapses filter().map() chains into single reduce / flatMap passes (filters, tag UI, CSV multi-value parsing).

Server components: Knowledge base and document pages now await Promise.all([params, searchParams]) so independent route props resolve in parallel.

Docs: Adds a “Run independent awaits in parallel” section to sim-react-performance.md (paired with existing guidance on copy-then-sort vs ES2023 array methods on the client).

Reviewed by Cursor Bugbot for commit acd8d97. Configure here.

@greptile-apps

greptile-apps Bot commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

A focused performance pass over the Knowledge Base module guided by react-doctor, targeting memoization gaps, O(n²) lookup patterns, and redundant allocations — all changes are behavior-preserving.

  • Memoization fixes: displayChunks in document.tsx and requiredScopes in connectors-section are now memoized, stabilising downstream hook deps; cooldownTimers uses lazy ??= init to avoid allocating a Set on every render.
  • Algorithmic improvements: find()-in-a-loop replaced with Map lookups in base.tsx, connector-selector-field, and use-connector-config-fields; filter().map() chains collapsed into single reduce/flatMap passes across several files.
  • Server-component parallelism: Sequential await params / await searchParams in both page entry points consolidated into Promise.all, consistent with the new sim-react-performance rule added in this PR.

Confidence Score: 5/5

All changes are behaviour-preserving refactors with no logic path alterations; safe to merge.

Every change either tightens a memo dependency array, removes a redundant allocation, or replaces a multi-pass chain with a single pass — none touch control flow or data mutations. The one dep-array entry that was widened (selectedChunk vs selectedChunk?.chunkIndex) produces the same rendered output; it only causes the breadcrumb memo to recompute slightly more often than needed.

document.tsx — the editChunkBreadcrumbs dep array uses the full selectedChunk object where the tighter selectedChunk?.chunkIndex was in the original.

Important Files Changed

Filename Overview
apps/sim/app/workspace/[workspaceId]/knowledge/[id]/[documentId]/document.tsx Memoizes displayChunks to stabilise downstream deps; dep widened on editChunkBreadcrumbs from selectedChunk?.chunkIndex to the full object — minor over-reactivity in a perf PR.
apps/sim/app/workspace/[workspaceId]/knowledge/[id]/components/connectors-section/connectors-section.tsx Lazy-inits cooldownTimersRef via ??= to avoid allocating a Set on every render; memoizes requiredScopes fallback array — both are correct and safe.
apps/sim/app/workspace/[workspaceId]/knowledge/hooks/use-knowledge-upload.ts Hoists buildUploadedFile to module scope — pure function with no closure over hook state, so the move is correct and eliminates per-render recreation.
apps/sim/app/workspace/[workspaceId]/knowledge/[id]/hooks/use-connector-config-fields.ts Adds fieldsById to the dependentFieldIds memo dep array — correct fix since fieldsById is now referenced inside the memo; eliminates a potential stale-closure bug.
apps/sim/app/workspace/[workspaceId]/knowledge/[id]/page.tsx Parallelises independent params/searchParams awaits with Promise.all — correct and consistent with the new performance rule.
apps/sim/app/workspace/[workspaceId]/knowledge/[id]/base.tsx Collapses filter().map() chains to single reduce passes in two memos; builds a Map for the O(n²) getDocumentTags loop — all behaviorally equivalent.

Reviews (3): Last reviewed commit: "Merge remote-tracking branch 'origin/sta..." | Re-trigger Greptile

Comment thread apps/sim/app/workspace/[workspaceId]/knowledge/[id]/[documentId]/document.tsx Outdated
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cursor review

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit fc69761. Configure here.

…doctor

# Conflicts:
#	.claude/rules/sim-react-performance.md
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cursor review

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit acd8d97. Configure here.

@waleedlatif1 waleedlatif1 merged commit 9204b4a into staging Jul 1, 2026
12 checks passed
@waleedlatif1 waleedlatif1 deleted the worktree-kb-react-doctor branch July 1, 2026 20:23
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