Skip to content

Keep index into buffer to avoid repeatedly scanning the buffer #501

Open
@simonjbeaumont

Description

@simonjbeaumont

(Creating this issue from discussion on PR: apple/swift-openapi-runtime#91 (comment)).

There are a number of state machines in the runtime library for transforming async sequences of bytes into async sequences of some other structure, e.g. JSON Lines, SSE, JSON Sequences.

Each of these is searching for a delimiter in the currently buffered data and emitting elements if/when it finds a delimiter.

If no delimiter is present in the buffer, it waits for more bytes. But each time it receives more bytes, it will search all buffered bytes for delimiter, including through the bytes that have already been searched.

In the event of any fragmentation of the elements in the byte stream this will be more work than necessary and, with pathological fragmentation, leads to quadratic algorithm that would otherwise be linear.

On the surface, this could be solved by maintaining an index /cursor in the state machine to keep track of which bytes have already been searched for the delimiter. But the devil will be in the detail.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/runtimeAffects: the runtime library.kind/enhancementImprovements to existing feature.size/MMedium task. (A couple of days of work.)

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions