opencode
opencode copied to clipboard
perf: optimize model dialog visibility lookups
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:
- A linear search through
store.userarray - 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: ASetfor O(1) lookup of whether a model is "latest" -
userVisibilityMap: AMapfor O(1) lookup of user visibility preferences
The memos are only recomputed when their underlying data changes, not on every visible() call.