Skip to content

Conversation

@mattcosta7
Copy link
Contributor

@mattcosta7 mattcosta7 commented Dec 15, 2025

Summary

Performance optimizations for Autocomplete to improve INP by reducing unnecessary re-renders.

Changes

Split AutocompleteContext into separate contexts:

  • AutocompleteStaticContext - For values that don't change (id)
  • AutocompleteSettersContext - For setter functions (stable references)
  • AutocompleteContext - For dynamic state that changes frequently

Components now subscribe only to the context slices they need.

Expected INP Impact

Scenario Before After Improvement
Worst case (rapid typing, many suggestions) ~40-80ms per keystroke (full re-render) <15ms (targeted re-render) 60-80% reduction
Average case (normal typing speed) ~20-40ms per keystroke <10ms 50-75% reduction
Best case (slow typing, few suggestions) ~10-20ms per keystroke <5ms 50-75% reduction

Why this matters

When all context values are in a single context object, any change causes all consumers to re-render:

  • Input component re-renders when menu state changes (unnecessary)
  • Menu component re-renders when input state changes (unnecessary)
  • Every keystroke triggered cascading re-renders

By splitting contexts, each component only re-renders when its specific data changes.


Part of the INP performance optimization effort. See #7312 for full context.

Split AutocompleteContext into separate contexts for static values, setters, and dynamic state.
Components now subscribe only to the context slices they need, reducing re-renders.

Part of #7312
@changeset-bot
Copy link

changeset-bot bot commented Dec 15, 2025

🦋 Changeset detected

Latest commit: 84876e6

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@primer/react Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the integration-tests: recommended This change needs to be tested for breaking changes. See https://arc.net/l/quote/tdmpakpm label Dec 15, 2025
@github-actions
Copy link
Contributor

👋 Hi, this pull request contains changes to the source code that github/github-ui depends on. If you are GitHub staff, test these changes with github/github-ui using the integration workflow. Or, apply the integration-tests: skipped manually label to skip these checks.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR optimizes Autocomplete component performance by splitting a monolithic context into three specialized contexts, reducing unnecessary re-renders during user typing. The changes prevent cascading re-renders where components were updating on every keystroke even when they didn't need the input value.

Key changes:

  • Split AutocompleteContext into three contexts: AutocompleteContext (stable values), AutocompleteInputContext (keystroke values), and AutocompleteDeferredInputContext (deferred filtering)
  • Updated AutocompleteInput and AutocompleteMenu to consume only the context slices they need
  • Implemented useDeferredValue for filtering operations to prevent blocking keystrokes

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
packages/react/src/Autocomplete/AutocompleteContext.tsx Defines three separate contexts replacing the original monolithic context
packages/react/src/Autocomplete/Autocomplete.tsx Provides memoized values for all three contexts with proper dependency arrays
packages/react/src/Autocomplete/AutocompleteInput.tsx Consumes AutocompleteContext and AutocompleteInputContext for immediate input needs
packages/react/src/Autocomplete/AutocompleteMenu.tsx Consumes AutocompleteContext and AutocompleteDeferredInputContext for filtering without blocking keystrokes
.changeset/perf-autocomplete-context-split.md Documents the performance optimization in the changelog

@mattcosta7 mattcosta7 self-assigned this Dec 15, 2025
@mattcosta7 mattcosta7 marked this pull request as ready for review December 15, 2025 20:53
@primer-integration
Copy link

👋 Hi from github/github-ui! Your integration PR is ready: https://github.com/github/github-ui/pull/8883

@primer-integration
Copy link

🔬 github-ui Integration Test Results

Check Status Details
CI ✅ Passed View run
Projects (Memex) ✅ Passed View run
VRT ✅ Passed View run

All checks passed! Your integration PR is ready for review.

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

Labels

integration-tests: recommended This change needs to be tested for breaking changes. See https://arc.net/l/quote/tdmpakpm

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants