GeezerVison is showing github.com — perf(core): quick wins — WASM defer, tool cache, adaptive polling, ri… · LuisAlbertoMK/opencode@7eca550
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/cli/script/build.ts +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -136,8 +136,9 @@ if (Script.channel === "vMK-dev") { | 136 | 136 | platformTarget.arch, | 137 | 137 | ].join("-") | 138 | 138 | const distName = targetName.replace(binary, "cli") | 139 | | - const src = `./dist/${distName}/bin/${binary}` | 140 | | - const dest = "./dist/opencode-vMK.exe" | | 139 | + const ext = platformTarget.os === "win32" ? ".exe" : "" | | 140 | + const src = `./dist/${distName}/bin/${binary}${ext}` | | 141 | + const dest = `./dist/opencode-vMK${ext}` | 141 | 142 | if (fs.existsSync(src)) { | 142 | 143 | await Bun.write(dest, await Bun.file(src).arrayBuffer()) | 143 | 144 | console.log(`[vMK] Created ${dest} from ${src}`) | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/session/compaction.ts +9-1Lines changed: 9 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -77,13 +77,21 @@ type Input = { | 77 | 77 | readonly request: LLMRequest | 78 | 78 | } | 79 | 79 |
| 80 | | -const estimateCache = new LruCache<string, number>(10, 30_000) | | 80 | +// vMK: Cache size 10→100 + WeakMap for fast identity lookup on repeated calls | | 81 | +const estimateCache = new LruCache<string, number>(100, 30_000) | | 82 | +const estimateWeak = new WeakMap<object, number>() | 81 | 83 | const estimate = (value: unknown) => { | | 84 | + // Fast path: if value is an object we've seen before, return cached result | | 85 | + if (typeof value === "object" && value !== null) { | | 86 | + const weak = estimateWeak.get(value as object) | | 87 | + if (weak !== undefined) return weak | | 88 | + } | 82 | 89 | const json = JSON.stringify(value) | 83 | 90 | const cached = estimateCache.get(json) | 84 | 91 | if (cached !== undefined) return cached | 85 | 92 | const result = Token.estimate(json) | 86 | 93 | estimateCache.set(json, result) | | 94 | + if (typeof value === "object" && value !== null) estimateWeak.set(value as object, result) | 87 | 95 | return result | 88 | 96 | } | 89 | 97 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/session/message-updater.ts +28-2Lines changed: 28 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -87,8 +87,34 @@ export function memory(state: MemoryState): Adapter { | 87 | 87 | // Limit in-memory history to prevent unbounded growth. | 88 | 88 | // Source of truth is the database; this is a fast projection. | 89 | 89 | if (state.messages.length > 1000) { | 90 | | - const removed = state.messages.splice(0, state.messages.length - 1000) | 91 | | - rebuildIndex() | | 90 | + const evictCount = state.messages.length - 1000 | | 91 | + state.messages.splice(0, evictCount) | | 92 | + // vMK: Incremental index shift instead of O(n) rebuildIndex() | | 93 | + const newMessageIndex = new Map<SessionMessage.ID, number>() | | 94 | + const newShellIndex = new Map<string, number>() | | 95 | + latestAssistantIdx = -1 | | 96 | + for (const [id, idx] of messageIndex) { | | 97 | + const newIdx = idx - evictCount | | 98 | + if (newIdx >= 0) newMessageIndex.set(id, newIdx) | | 99 | + } | | 100 | + for (const [callID, idx] of shellIndex) { | | 101 | + const newIdx = idx - evictCount | | 102 | + if (newIdx >= 0) newShellIndex.set(callID, newIdx) | | 103 | + } | | 104 | + // Fix: re-derive latestAssistantIdx from shifted index | | 105 | + if (latestAssistantIdx >= 0) { | | 106 | + const shifted = latestAssistantIdx - evictCount | | 107 | + latestAssistantIdx = shifted >= 0 ? shifted : -1 | | 108 | + } | | 109 | + // Update the index maps in-place | | 110 | + messageIndex.clear() | | 111 | + for (const [id, idx] of newMessageIndex) messageIndex.set(id, idx) | | 112 | + shellIndex.clear() | | 113 | + for (const [callID, idx] of newShellIndex) shellIndex.set(callID, idx) | | 114 | + // Re-scan only the tail to find latest assistant (O(1000) worst case) | | 115 | + for (let i = state.messages.length - 1; i >= 0; i--) { | | 116 | + if (state.messages[i]!.type === "assistant") { latestAssistantIdx = i; break } | | 117 | + } | 92 | 118 | } | 93 | 119 | }) | 94 | 120 | }, | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/tool/registry.ts +15-1Lines changed: 15 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -80,6 +80,11 @@ const registryLayer = Layer.effect( | 80 | 80 | : { result, output: bounded.output } | 81 | 81 | }) | 82 | 82 |
| | 83 | + // vMK: Generation counter to detect when tool registrations change | | 84 | + let materializationGen = 0 | | 85 | + let cachedGen = -1 | | 86 | + let cachedResult: Materialization | null = null | | 87 | + | 83 | 88 | return Service.of({ | 84 | 89 | register: Effect.fn("ToolRegistry.register")(function* (tools) { | 85 | 90 | const entries = Object.entries(tools) | @@ -90,34 +95,43 @@ const registryLayer = Layer.effect( | 90 | 95 | const token = {} | 91 | 96 | for (const [name, tool] of entries) | 92 | 97 | local.set(name, [...(local.get(name) ?? []), { token, registration: { identity: {}, tool } }]) | | 98 | + materializationGen++ // vMK: invalidate cache | 93 | 99 | yield* Effect.addFinalizer(() => | 94 | 100 | Effect.sync(() => { | 95 | 101 | for (const [name] of entries) { | 96 | 102 | const registrations = local.get(name)?.filter((registration) => registration.token !== token) ?? [] | 97 | 103 | if (registrations.length > 0) local.set(name, registrations) | 98 | 104 | else local.delete(name) | 99 | 105 | } | | 106 | + materializationGen++ // vMK: invalidate cache on deregister | 100 | 107 | }), | 101 | 108 | ) | 102 | 109 | }), | 103 | 110 | ) | 104 | 111 | }), | 105 | 112 | materialize: Effect.fn("ToolRegistry.materialize")(function* (permissions = []) { | | 113 | + // vMK: Return cached materialization if registrations haven't changed | | 114 | + if (cachedResult && cachedGen === materializationGen) { | | 115 | + return cachedResult | | 116 | + } | 106 | 117 | const registrations = new Map(applications.entries()) | 107 | 118 | for (const [name, entries] of local) { | 108 | 119 | const registration = entries.at(-1)?.registration | 109 | 120 | if (registration) registrations.set(name, registration) | 110 | 121 | } | 111 | 122 | for (const [name, registration] of registrations) | 112 | 123 | if (whollyDisabled(permission(registration.tool, name), permissions)) registrations.delete(name) | 113 | | - return { | | 124 | + const result: Materialization = { | 114 | 125 | definitions: Array.from(registrations, ([name, registration]) => definition(name, registration.tool)), | 115 | 126 | settle: (input) => { | 116 | 127 | const registration = registrations.get(input.call.name) | 117 | 128 | if (registration) return settleWith(input, registration.identity) | 118 | 129 | return Effect.succeed({ result: { type: "error", value: `Unknown tool: ${input.call.name}` } }) | 119 | 130 | }, | 120 | 131 | } | | 132 | + cachedGen = materializationGen | | 133 | + cachedResult = result | | 134 | + return result | 121 | 135 | }), | 122 | 136 | }) | 123 | 137 | }), | |
Collapse file Copy file name to clipboardExpand all lines: packages/script/src/index.ts +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -34,7 +34,9 @@ const IS_PREVIEW = CHANNEL !== "latest" | 34 | 34 | const VERSION = await (async () => { | 35 | 35 | if (env.OPENCODE_VERSION) return env.OPENCODE_VERSION | 36 | 36 | if (IS_PREVIEW) { | 37 | | - const pkg = await Bun.file(path.resolve(import.meta.dir, "../../opencode/package.json")).json() | | 37 | + // vMK: opencode package moved from packages/opencode to packages/cli | | 38 | + const pkgPath = path.resolve(import.meta.dir, "../../cli/package.json") | | 39 | + const pkg = await Bun.file(pkgPath).json() | 38 | 40 | return `${pkg.version || "0.0.0"}-${CHANNEL}` | 39 | 41 | } | 40 | 42 | const version = await fetch("https://registry.npmjs.org/opencode-ai/latest") | |
Collapse file Copy file name to clipboardExpand all lines: packages/tui/src/component/virtual-list.tsx +34-7Lines changed: 34 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -20,7 +20,8 @@ import { | 20 | 20 | } from "solid-js" | 21 | 21 | import type { ScrollBoxRenderable } from "@opentui/core" | 22 | 22 |
| 23 | | -const POLL_MS = 100 // Check scroll position every 100ms | | 23 | +const POLL_ACTIVE_MS = 100 // Check scroll position every 100ms when scrolling | | 24 | +const POLL_IDLE_MS = 500 // Check every 500ms when idle (no scroll changes) | 24 | 25 | const ESTIMATED_HEIGHT = 5 // Fallback height per item when not yet measured | 25 | 26 | const OVERSCAN = 3 // Extra items above/below viewport | 26 | 27 |
| @@ -64,20 +65,44 @@ export function VirtualList<T>(props: VirtualListProps<T>) { | 64 | 65 | const ref = props.scrollRef() | 65 | 66 | if (!ref || ref.isDestroyed) return | 66 | 67 |
| 67 | | - const id = setInterval(() => { | | 68 | + // vMK: Two-tier polling — 100ms when scrolling, 500ms when idle | | 69 | + let lastScrollTop = r.scrollTop | | 70 | + let idleCycles = 0 | | 71 | + let currentInterval = POLL_ACTIVE_MS | | 72 | + | | 73 | + const tick = () => { | 68 | 74 | try { | 69 | | - const r = props.scrollRef() | 70 | | - if (!r || r.isDestroyed) { | | 75 | + const r2 = props.scrollRef() | | 76 | + if (!r2 || r2.isDestroyed) { | 71 | 77 | clearInterval(id) | 72 | 78 | return | 73 | 79 | } | 74 | 80 |
| 75 | 81 | // --- scroll tracking --- | 76 | | - const st = r.scrollTop | 77 | | - const vh = r.height | | 82 | + const st = r2.scrollTop | | 83 | + const vh = r2.height | | 84 | + const changed = Math.abs(lastScrollTop - st) > 0.5 || viewportHeight() !== vh | 78 | 85 | setScrollTop((prev) => (Math.abs(prev - st) > 0.5 ? st : prev)) | 79 | 86 | setViewportHeight((prev) => (prev !== vh ? vh : prev)) | 80 | 87 |
| | 88 | + // vMK: adaptive polling — slow down when idle | | 89 | + if (changed) { | | 90 | + idleCycles = 0 | | 91 | + if (currentInterval !== POLL_ACTIVE_MS) { | | 92 | + clearInterval(id) | | 93 | + currentInterval = POLL_ACTIVE_MS | | 94 | + id = setInterval(tick, currentInterval) | | 95 | + } | | 96 | + } else { | | 97 | + idleCycles++ | | 98 | + if (idleCycles > 5 && currentInterval !== POLL_IDLE_MS) { | | 99 | + clearInterval(id) | | 100 | + currentInterval = POLL_IDLE_MS | | 101 | + id = setInterval(tick, currentInterval) | | 102 | + } | | 103 | + } | | 104 | + lastScrollTop = st | | 105 | + | 81 | 106 | // --- height measurement --- | 82 | 107 | // Read Yoga-computed heights from currently rendered box refs. | 83 | 108 | // Heights are unknown until Yoga layout runs (next frame after SolidJS commit). | @@ -100,7 +125,9 @@ export function VirtualList<T>(props: VirtualListProps<T>) { | 100 | 125 | } catch { | 101 | 126 | // scrollbox may be in inconsistent state during destruction | 102 | 127 | } | 103 | | - }, POLL_MS) | | 128 | + } | | 129 | + | | 130 | + let id = setInterval(tick, currentInterval) | 104 | 131 |
| 105 | 132 | onCleanup(() => clearInterval(id)) | 106 | 133 | }) | |
Collapse file Copy file name to clipboardExpand all lines: packages/web/package.json +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 32 | 32 | "vscode-languageserver-types": "3.17.5" | 33 | 33 | }, | 34 | 34 | "devDependencies": { | 35 | | - "opencode": "workspace:*", | | 35 | + "@opencode-ai/cli": "workspace:*", | 36 | 36 | "@types/node": "catalog:", | 37 | 37 | "@astrojs/check": "0.9.6", | 38 | 38 | "typescript": "catalog:" | |
|