Skip to content

perf(core): sort integrations once per rebuild - #59402

Merged
rekram1-node merged 1 commit into
devfrom
sort-integrations-once
Oct 26, 2026
Merged

rekram1-node merged 1 commit into
devfrom
sort-integrations-once

Conversation

@rekram1-node

Copy link
Copy Markdown
Collaborator

Problem

Integration.list() re-sorted every integration by name with localeCompare on each call. Provider.snapshot() calls it on every model lookup (a few times per step) and turns the result straight into a Map, so that sort was mostly wasted work. The integration definitions only change when the integration state is rebuilt.

Change

I sort the entries once per rebuilt state, memoized on the identity of state.get().integrations (each rebuild produces a new Map), and list() just maps over the sorted entries.

  list()
-   project every entry -> toSorted(by name)
+   sortedEntries()        // cached per rebuilt state
+     -> project every entry

The sort key is the same: project() takes name straight from entry.ref.name and credentials never change it, and toSorted is stable, so the output order and shape are identical to before.

With 100 registered integrations and no credentials, a quick local benchmark went from about 126 µs to 43 µs per list() call.

Out of scope

Credential reads in list() and how Provider.snapshot() consumes the result are unchanged.

@opencode-agent opencode-agent Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks correct. State.create builds a fresh integrations Map on every rebuild and never changes it after publishing, so caching the sorted entries on the Map's identity can't go stale. project() takes name straight from ref.name, so list() returns the same order and shape as before. In packages/core, the integration and provider tests pass and typecheck:tsc is clean.

Public API: unchanged.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant