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

Prevent unnecessary Copy-on-Write behaviour in HTTPFields #458

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Sources/HummingbirdCore/Request/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ public struct Request: Sendable {
/// Body of HTTP request
public var body: RequestBody
/// Request HTTP method
@inlinable
public var method: HTTPRequest.Method { self.head.method }
/// Request HTTP headers
@inlinable
public var headers: HTTPFields { self.head.headerFields }

// MARK: Initialization
Expand Down
30 changes: 16 additions & 14 deletions Sources/HummingbirdCore/Response/Response.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,34 @@ import HTTPTypes

/// Holds all the required to generate a HTTP Response
public struct Response: Sendable {
public var head: HTTPResponse
@inlinable
public var head: HTTPResponse {
get { HTTPResponse(status: status, headerFields: headers) }
set {
self.status = newValue.status
self.headers = newValue.headerFields
}
}

public var body: ResponseBody {
didSet {
if let contentLength = body.contentLength {
self.head.headerFields[.contentLength] = String(describing: contentLength)
self.headers[.contentLength] = String(describing: contentLength)
}
}
}

public init(status: HTTPResponse.Status, headers: HTTPFields = .init(), body: ResponseBody = .init()) {
self.head = .init(status: status, headerFields: headers)
self.status = status
self.headers = headers
self.body = body
if let contentLength = body.contentLength, headers[.contentLength] == nil {
self.head.headerFields[.contentLength] = String(describing: contentLength)
if let contentLength = body.contentLength, self.headers[.contentLength] == nil {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self. was missing, leading to the original headers still being references. This lead to there being two active copies, which would cause a CoW

self.headers[.contentLength] = String(describing: contentLength)
}
}

public var status: HTTPResponse.Status {
get { self.head.status }
set { self.head.status = newValue }
}

public var headers: HTTPFields {
get { self.head.headerFields }
set { self.head.headerFields = newValue }
}
public var status: HTTPResponse.Status
public var headers: HTTPFields
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First of all, modifying response.headers was leading to a CoW. This is because headers was a getter/setter, thus copied the value from head. I've reversed this relationship


/// Return HEAD response based off this response
public func createHeadResponse() -> Response {
Expand Down
Loading