Skip to content

Add StaticInputHandler protocol for zero-allocation input filling - #188

Draft
stikves wants to merge 2 commits into
apple:mainfrom
stikves:sukru/input-handler-public
Draft

Add StaticInputHandler protocol for zero-allocation input filling#188
stikves wants to merge 2 commits into
apple:mainfrom
stikves:sukru/input-handler-public

Conversation

@stikves

@stikves stikves commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

Adds InputBuffers and StaticInputHandler, a protocol for filling pre-allocated input buffers in-place without per-step allocation.

Design

public protocol StaticInputHandler: Sendable {
    var inputNames: [String] { get }
    func registerBuffers(into buffers: inout InputBuffers)
    func fill(_ context: InputContext, into buffers: inout InputBuffers) throws
}
  • Engine owns InputBuffers, creates once at init, passes inout each step
  • _modify accessor on InputBuffers subscript avoids COW (26× vs get/set)
  • Non-mutating fill — handler holds no per-step state
  • Backward compatible: existing SyncInputHandler unchanged

Primary benefit is architectural (no COW risk, compile-time ownership via inout) rather than raw throughput.

stikves and others added 2 commits August 20, 2026 15:10
Introduces InputBuffers (pre-allocated, reused across steps) and the
StaticInputHandler protocol (inout fill pattern). TokenStaticInputHandler
fills input_ids and position_ids without per-step allocation.

Matches the state handler's bind(into:) pattern. The _modify accessor on
InputBuffers subscript avoids COW overhead (measured 26× improvement over
get/set in benchmarks).

Existing SyncInputHandler is unchanged — this is additive.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant