Files
deepseek-harness/packages/util/deque

description, kind
description kind
Circular deque for Host and browser packages that need amortized constant-time queue operations, immediate release of removed entries, and bounded vacant storage. package-library

@deepseek-ai/dsh-deque

English | 中文

Summary

dsh-deque lets Host and browser packages drain long-lived in-process queues without moving every remaining entry after each removal. Callers append or prepend entries and remove them from the front with amortized constant-time operations. The deque owns entry order and backing-storage release; each consumer still owns wake-up, failure, cancellation, capacity, and overload behavior.

Table of Contents


Use this package

When to use it

Use Deque<T> when entries can accumulate across asynchronous work and the consumer needs FIFO removal, optional front insertion, or explicit queue clearing. Finite local worklists can stay as arrays when their maximum size makes head removal cost irrelevant.

Entry point

Import the deque, append entries at the tail, and check size before removing an entry whose type may include undefined:

import { Deque } from '@deepseek-ai/dsh-deque'

const frames = new Deque<string>()
frames.pushBack('first')
frames.pushFront('before-first')

while (frames.size > 0) {
  console.log(frames.popFront())
}

The methods do not impose a queue limit or translate consumer failures. See src/index.ts for the exact TypeScript contract.


Understand the implementation

Implementation internals — click to expand

The deque stores entries in a circular array. Removing an entry clears that slot immediately, while geometric growth and quarter-full shrinking keep copying work amortized constant time and prevent a head cursor from retaining indefinitely growing vacant storage.

Source map

File Role
src/index.ts Circular deque operations and backing-storage lifecycle
src/invariant.ts Invariant companion (no runtime invariant; ordering and storage lifecycle are exercised by unit tests)
tests/deque.spec.ts FIFO, front insertion, wrapping, growth, compaction, clearing, and reuse coverage
benchmarks/drain.ts Reproducible backlog-drain timing across increasing queue sizes

Further Exploration


Model Experience

None, as this in-process collection registers nothing model-facing.

KV Cache effect

Nothing here enters a model request, so provider cache reuse is unaffected.

Known Limitations and Deferred Work

  • No capacity policy — the deque does not bound, coalesce, or reject entries; each consumer must define overload behavior appropriate to its stream.

Dev Note

Working context for maintainers — click to expand

None.