opencode icon indicating copy to clipboard operation
opencode copied to clipboard

perf: optimize model dialog visibility lookups

Open shkumbinhasani opened this issue 2 weeks ago • 0 comments

Summary

  • Fixes model dialog lag that occurs during longer sessions
  • Replaces O(n) array lookups with O(1) Set/Map lookups in the visible() function

Problem

The visible() function in packages/app/src/context/local.tsx was called inside a .filter() loop for each model. Each call performed:

  1. A linear search through store.user array
  2. A linear search through latest() array (which involves expensive date computations)

With many models, this became O(n²) and caused noticeable lag when opening the model selector.

Solution

Added two memoized lookup structures:

  • latestSet: A Set for O(1) lookup of whether a model is "latest"
  • userVisibilityMap: A Map for O(1) lookup of user visibility preferences

The memos are only recomputed when their underlying data changes, not on every visible() call.

shkumbinhasani avatar Jan 04 '26 00:01 shkumbinhasani