Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
simonjbeaumont opened this issue Jan 8, 2024 · 1 comment
Open
Labels
area/runtime Affects: the runtime library. kind/enhacement Improvements to existing feature. size/M Medium task. (A couple of days of work.)
Milestone

Comments

@simonjbeaumont
Copy link
Collaborator

(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.

@simonjbeaumont simonjbeaumont transferred this issue from apple/swift-openapi-runtime Jan 8, 2024
@simonjbeaumont simonjbeaumont added area/runtime Affects: the runtime library. kind/enhacement Improvements to existing feature. size/M Medium task. (A couple of days of work.) labels Jan 8, 2024
@czechboy0 czechboy0 added this to the Post-1.0 milestone Jan 8, 2024
@czechboy0
Copy link
Collaborator

Would likely make sense to wrap the storage + cursor in a new struct type, and adopt it in these state machines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/runtime Affects: the runtime library. kind/enhacement Improvements to existing feature. size/M Medium task. (A couple of days of work.)
Projects
None yet
Development

No branches or pull requests

2 participants