swift-server / swift-server/swift-http-server

Offer ResponseSender.Writer RawSpan APIs

Open
#123 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Swift
Stars
44
Forks
13
Avg merge
4d 3h
Merged PRs (30d)
8

Description

We should offer RawSpan APIs for the ResponseSender.Writer to avoid an extra copy when it takes the container and copies it to a ByteBuffer. If we have a RawSpan API we can bypass that allocation for every write. I verified this in Vapor 5 where currently we have to stage every chunk into a UniqueArray. I've tested this and it reduces the copy for every write, which can add up when streaming large responses. It reduces it down to a single copy in the pipeline. Will submit a PR with the new APIs.

We can write overloads to do a borrow instead of the copy:

public mutating func write(_ bytes: RawSpan) async throws(WriteFailure)
public consuming func finish(bytes: RawSpan, finalElement: consuming HTTPFields?) async throws(WriteFailure)

Note that we'd need an overload for finish as well which covers non-streaming cases.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at ResponseSender.Writer and trace how writes currently stage chunks in a UniqueArray before copying to a ByteBuffer. Compare the existing write and finish APIs, including the non-streaming finish path. Done means RawSpan overloads cover writing and finishing while reducing the pipeline to a single copy.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
api, backend, performance
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.