Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Studio: look up model picker rows by index map instead of a scan per row (#15674)

Daniel Han8b0f6bdc2f26
diff --git a/studio/frontend/src/features/model-picker/components/model-selector/option-index.ts b/studio/frontend/src/features/model-picker/components/model-selector/option-index.ts
new file mode 100644
index 00000000000..ec4ab040a0b
--- /dev/null
+++ b/studio/frontend/src/features/model-picker/components/model-selector/option-index.ts
@@ -0,0 +1,10 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+export function firstIndexByKey(keys: readonly string[]): Map<string, number> {
+ const indexByKey = new Map<string, number>();
+ keys.forEach((key, index) => {
+ if (!indexByKey.has(key)) indexByKey.set(key, index);
+ });
+ return indexByKey;
+}
diff --git a/studio/frontend/src/features/model-picker/components/model-selector/pickers.tsx b/studio/frontend/src/features/model-picker/components/model-selector/pickers.tsx
index fc746785825..98636a8a106 100644
--- a/studio/frontend/src/features/model-picker/components/model-selector/pickers.tsx
+++ b/studio/frontend/src/features/model-picker/components/model-selector/pickers.tsx
@@ -11,6 +11,7 @@ import {
hubWithdrawsSoleQuant,
loadPickerGgufVariants,
} from "./gguf-discovery";
+import { firstIndexByKey } from "./option-index";
import { ModelMemoryBar } from "@/components/model-memory-bar";
import { shouldRefreshPickerInventoryOnMount } from "@/components/resource-picker/picker-tab-policy";
@@ -384,22 +385,24 @@ function useRovingModelList({
const rawListboxId = useId();
const listboxId = `model-picker-${rawListboxId.replace(/:/g, "")}`;
const [rovingOptionKey, setRovingOptionKey] = useState<string | null>(null);
+ // Looked up once per row: indexOf here made each render O(N^2).
+ const optionIndexByKey = useMemo(() => firstIndexByKey(optionKeys), [optionKeys]);
const preferredOptionKey =
- selectedOptionKey && optionKeys.includes(selectedOptionKey)
+ selectedOptionKey && optionIndexByKey.has(selectedOptionKey)
? selectedOptionKey
: (optionKeys[0] ?? null);
const activeOptionKey =
- rovingOptionKey && optionKeys.includes(rovingOptionKey)
+ rovingOptionKey && optionIndexByKey.has(rovingOptionKey)
? rovingOptionKey
: preferredOptionKey;
const getOptionDomId = useCallback(
(optionKey: string) => {
- const index = optionKeys.indexOf(optionKey);
- return index === -1 ? undefined : `${listboxId}-option-${index}`;
+ const index = optionIndexByKey.get(optionKey);
+ return index === undefined ? undefined : `${listboxId}-option-${index}`;
},
- [listboxId, optionKeys],
+ [listboxId, optionIndexByKey],
);
const focusOption = useCallback(
@@ -422,7 +425,7 @@ function useRovingModelList({
return;
}
- const currentIndex = optionKeys.indexOf(fromOptionKey);
+ const currentIndex = optionIndexByKey.get(fromOptionKey) ?? -1;
let nextIndex = currentIndex === -1 ? 0 : currentIndex;
if (direction === "next") {
if (currentIndex >= optionKeys.length - 1) {
@@ -446,7 +449,7 @@ function useRovingModelList({
setRovingOptionKey(nextOptionKey);
focusOption(nextOptionKey);
},
- [focusOption, onNavigatePastEnd, onNavigatePastStart, optionKeys],
+ [focusOption, onNavigatePastEnd, onNavigatePastStart, optionIndexByKey, optionKeys],
);
const getOptionProps = useCallback(
diff --git a/studio/frontend/tests/model-picker-option-index.test.ts b/studio/frontend/tests/model-picker-option-index.test.ts
new file mode 100644
index 00000000000..c01b22ee942
--- /dev/null
+++ b/studio/frontend/tests/model-picker-option-index.test.ts
@@ -0,0 +1,28 @@
+// SPDX-License-Identifier: AGPL-3.0-only
+// Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+
+import assert from "node:assert/strict";
+import test from "node:test";
+
+import { firstIndexByKey } from "../src/features/model-picker/components/model-selector/option-index.ts";
+
+import { readSrc } from "./helpers/kit.ts";
+
+const PICKERS = readSrc("features/model-picker/components/model-selector/pickers.tsx");
+
+test("the index map answers exactly what indexOf and includes did, first match on duplicates", () => {
+ const keys = ["hub::a", "connected::b", "hub::a", "local::c", ""];
+ const index = firstIndexByKey(keys);
+ for (const key of [...keys, "missing", "hub::"]) {
+ assert.equal(index.get(key) ?? -1, keys.indexOf(key), key);
+ assert.equal(index.has(key), keys.includes(key), key);
+ }
+ assert.equal(firstIndexByKey([]).size, 0);
+});
+
+test("the roving list no longer scans optionKeys per row", () => {
+ const start = PICKERS.indexOf("function useRovingModelList(");
+ const body = PICKERS.slice(start, PICKERS.indexOf("\n}\n", start));
+ assert.doesNotMatch(body, /optionKeys\.(indexOf|includes)\(/);
+ assert.match(body, /useMemo\(\(\) => firstIndexByKey\(optionKeys\), \[optionKeys\]\)/);
+});