Skip to content

perf(thinking-state): ThinkingState.prune() uses O(n) Array.shift(), making each eviction linear-cost #74

@github-actions

Description

@github-actions

Problem

apps/desktop/src/main/gateway/deepseek/state.ts L114–123

private prune(): void {
  while (this.recordOrder.length > this.limit) {
    const id = this.recordOrder.shift()!; // O(n) left-shift
    this.records.delete(id);
  }
  while (this.textOrder.length > this.limit) {
    const key = this.textOrder.shift()!;  // O(n)
    this.textRecords.delete(key);
  }
}

Array.shift() is an O(n) operation that shifts every remaining element on each eviction. With a default limit of 1024, this fires frequently during heavy tool-call workloads.

Suggested fix

Replace shift() with a circular index (ring buffer) or an index: number pointer to bring prune down to O(1). Alternatively, use a proper deque such as the denque package.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions