GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — perf(core): quick wins — WASM defer, tool cache, adaptive polling, ri… · LuisAlbertoMK/opencode@7eca550

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages
    • cli/script
      • build.ts
    • core/src
      • session
        • compaction.ts
        • message-updater.ts
      • tool
        • registry.ts
    • script/src
      • index.ts
    • tui/src/component
      • virtual-list.tsx
    • web
      • package.json
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/cli/script/build.ts‎

Copy file name to clipboardExpand all lines: packages/cli/script/build.ts
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,9 @@ if (Script.channel === "vMK-dev") {
136136
platformTarget.arch,
137137
].join("-")
138138
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}`
141142
if (fs.existsSync(src)) {
142143
await Bun.write(dest, await Bun.file(src).arrayBuffer())
143144
console.log(`[vMK] Created ${dest} from ${src}`)
Collapse file

‎packages/core/src/session/compaction.ts‎

Copy file name to clipboardExpand all lines: packages/core/src/session/compaction.ts
+9-1Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,21 @@ type Input = {
7777
readonly request: LLMRequest
7878
}
7979

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>()
8183
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+
}
8289
const json = JSON.stringify(value)
8390
const cached = estimateCache.get(json)
8491
if (cached !== undefined) return cached
8592
const result = Token.estimate(json)
8693
estimateCache.set(json, result)
94+
if (typeof value === "object" && value !== null) estimateWeak.set(value as object, result)
8795
return result
8896
}
8997

Collapse file

‎packages/core/src/session/message-updater.ts‎

Copy file name to clipboardExpand all lines: packages/core/src/session/message-updater.ts
+28-2Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,34 @@ export function memory(state: MemoryState): Adapter {
8787
// Limit in-memory history to prevent unbounded growth.
8888
// Source of truth is the database; this is a fast projection.
8989
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+
}
92118
}
93119
})
94120
},
Collapse file

‎packages/core/src/tool/registry.ts‎

Copy file name to clipboardExpand all lines: packages/core/src/tool/registry.ts
+15-1Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ const registryLayer = Layer.effect(
8080
: { result, output: bounded.output }
8181
})
8282

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+
8388
return Service.of({
8489
register: Effect.fn("ToolRegistry.register")(function* (tools) {
8590
const entries = Object.entries(tools)
@@ -90,34 +95,43 @@ const registryLayer = Layer.effect(
9095
const token = {}
9196
for (const [name, tool] of entries)
9297
local.set(name, [...(local.get(name) ?? []), { token, registration: { identity: {}, tool } }])
98+
materializationGen++ // vMK: invalidate cache
9399
yield* Effect.addFinalizer(() =>
94100
Effect.sync(() => {
95101
for (const [name] of entries) {
96102
const registrations = local.get(name)?.filter((registration) => registration.token !== token) ?? []
97103
if (registrations.length > 0) local.set(name, registrations)
98104
else local.delete(name)
99105
}
106+
materializationGen++ // vMK: invalidate cache on deregister
100107
}),
101108
)
102109
}),
103110
)
104111
}),
105112
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+
}
106117
const registrations = new Map(applications.entries())
107118
for (const [name, entries] of local) {
108119
const registration = entries.at(-1)?.registration
109120
if (registration) registrations.set(name, registration)
110121
}
111122
for (const [name, registration] of registrations)
112123
if (whollyDisabled(permission(registration.tool, name), permissions)) registrations.delete(name)
113-
return {
124+
const result: Materialization = {
114125
definitions: Array.from(registrations, ([name, registration]) => definition(name, registration.tool)),
115126
settle: (input) => {
116127
const registration = registrations.get(input.call.name)
117128
if (registration) return settleWith(input, registration.identity)
118129
return Effect.succeed({ result: { type: "error", value: `Unknown tool: ${input.call.name}` } })
119130
},
120131
}
132+
cachedGen = materializationGen
133+
cachedResult = result
134+
return result
121135
}),
122136
})
123137
}),
Collapse file

‎packages/script/src/index.ts‎

Copy file name to clipboardExpand all lines: packages/script/src/index.ts
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ const IS_PREVIEW = CHANNEL !== "latest"
3434
const VERSION = await (async () => {
3535
if (env.OPENCODE_VERSION) return env.OPENCODE_VERSION
3636
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()
3840
return `${pkg.version || "0.0.0"}-${CHANNEL}`
3941
}
4042
const version = await fetch("https://registry.npmjs.org/opencode-ai/latest")
Collapse file

‎packages/tui/src/component/virtual-list.tsx‎

Copy file name to clipboardExpand all lines: packages/tui/src/component/virtual-list.tsx
+34-7Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ import {
2020
} from "solid-js"
2121
import type { ScrollBoxRenderable } from "@opentui/core"
2222

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)
2425
const ESTIMATED_HEIGHT = 5 // Fallback height per item when not yet measured
2526
const OVERSCAN = 3 // Extra items above/below viewport
2627

@@ -64,20 +65,44 @@ export function VirtualList<T>(props: VirtualListProps<T>) {
6465
const ref = props.scrollRef()
6566
if (!ref || ref.isDestroyed) return
6667

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 = () => {
6874
try {
69-
const r = props.scrollRef()
70-
if (!r || r.isDestroyed) {
75+
const r2 = props.scrollRef()
76+
if (!r2 || r2.isDestroyed) {
7177
clearInterval(id)
7278
return
7379
}
7480

7581
// --- 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
7885
setScrollTop((prev) => (Math.abs(prev - st) > 0.5 ? st : prev))
7986
setViewportHeight((prev) => (prev !== vh ? vh : prev))
8087

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+
81106
// --- height measurement ---
82107
// Read Yoga-computed heights from currently rendered box refs.
83108
// Heights are unknown until Yoga layout runs (next frame after SolidJS commit).
@@ -100,7 +125,9 @@ export function VirtualList<T>(props: VirtualListProps<T>) {
100125
} catch {
101126
// scrollbox may be in inconsistent state during destruction
102127
}
103-
}, POLL_MS)
128+
}
129+
130+
let id = setInterval(tick, currentInterval)
104131

105132
onCleanup(() => clearInterval(id))
106133
})
Collapse file

‎packages/web/package.json‎

Copy file name to clipboardExpand all lines: packages/web/package.json
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"vscode-languageserver-types": "3.17.5"
3333
},
3434
"devDependencies": {
35-
"opencode": "workspace:*",
35+
"@opencode-ai/cli": "workspace:*",
3636
"@types/node": "catalog:",
3737
"@astrojs/check": "0.9.6",
3838
"typescript": "catalog:"

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.