-
Notifications
You must be signed in to change notification settings - Fork 58
PseudoHeaderFields adopting CoW to reduce size #88
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -162,32 +162,88 @@ public struct HTTPResponse: Sendable, Hashable { | |
let code = | ||
Int(codeIterator.next()! - 48) * 100 + Int(codeIterator.next()! - 48) * 10 | ||
+ Int(codeIterator.next()! - 48) | ||
return Status(uncheckedCode: code, reasonPhrase: self.reasonPhrase) | ||
return Status(uncheckedCode: code, reasonPhrase: self.pseudoHeaderFields.reasonPhrase) | ||
} | ||
set { | ||
self.pseudoHeaderFields.status.rawValue = ISOLatin1String(unchecked: newValue.fieldValue) | ||
self.reasonPhrase = newValue.reasonPhrase | ||
self.pseudoHeaderFields.reasonPhrase = newValue.reasonPhrase | ||
} | ||
} | ||
|
||
/// The pseudo header fields of a response. | ||
public struct PseudoHeaderFields: Sendable, Hashable { | ||
private final class _Storage: @unchecked Sendable, Hashable { | ||
var status: HTTPField | ||
var reasonPhrase: String | ||
|
||
init(status: HTTPField, reasonPhrase: String) { | ||
self.status = status | ||
self.reasonPhrase = reasonPhrase | ||
} | ||
|
||
func copy() -> Self { | ||
.init( | ||
status: self.status, | ||
reasonPhrase: self.reasonPhrase | ||
) | ||
} | ||
|
||
static func == (lhs: _Storage, rhs: _Storage) -> Bool { | ||
lhs.status == rhs.status | ||
} | ||
|
||
func hash(into hasher: inout Hasher) { | ||
hasher.combine(self.status) | ||
} | ||
} | ||
|
||
private var _storage: _Storage | ||
|
||
/// The underlying ":status" pseudo header field. | ||
/// | ||
/// The value of this field must be 3 ASCII decimal digits. | ||
public var status: HTTPField { | ||
willSet { | ||
get { | ||
self._storage.status | ||
} | ||
set { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same note on these accessors re |
||
precondition(newValue.name == .status, "Cannot change pseudo-header field name") | ||
precondition(Status.isValidStatus(newValue.rawValue._storage), "Invalid status code") | ||
|
||
if !isKnownUniquelyReferenced(&self._storage) { | ||
self._storage = self._storage.copy() | ||
} | ||
self._storage.status = newValue | ||
} | ||
} | ||
|
||
var reasonPhrase: String { | ||
get { | ||
self._storage.reasonPhrase | ||
} | ||
set { | ||
if !isKnownUniquelyReferenced(&self._storage) { | ||
self._storage = self._storage.copy() | ||
} | ||
self._storage.reasonPhrase = newValue | ||
} | ||
} | ||
|
||
private init(status: HTTPField) { | ||
self._storage = .init(status: status, reasonPhrase: "") | ||
} | ||
|
||
init(status: Status) { | ||
self._storage = .init( | ||
status: HTTPField(name: .status, uncheckedValue: ISOLatin1String(unchecked: status.fieldValue)), | ||
reasonPhrase: status.reasonPhrase | ||
) | ||
} | ||
} | ||
|
||
/// The pseudo header fields. | ||
public var pseudoHeaderFields: PseudoHeaderFields | ||
|
||
private var reasonPhrase: String | ||
|
||
/// The response header fields. | ||
public var headerFields: HTTPFields | ||
|
||
|
@@ -196,20 +252,9 @@ public struct HTTPResponse: Sendable, Hashable { | |
/// - status: The status code and an optional reason phrase. | ||
/// - headerFields: The response header fields. | ||
public init(status: Status, headerFields: HTTPFields = [:]) { | ||
let statusField = HTTPField(name: .status, uncheckedValue: ISOLatin1String(unchecked: status.fieldValue)) | ||
self.pseudoHeaderFields = .init(status: statusField) | ||
self.reasonPhrase = status.reasonPhrase | ||
self.pseudoHeaderFields = .init(status: status) | ||
self.headerFields = headerFields | ||
} | ||
|
||
public func hash(into hasher: inout Hasher) { | ||
hasher.combine(self.pseudoHeaderFields) | ||
hasher.combine(self.headerFields) | ||
} | ||
|
||
public static func == (lhs: HTTPResponse, rhs: HTTPResponse) -> Bool { | ||
lhs.pseudoHeaderFields == rhs.pseudoHeaderFields && lhs.headerFields == rhs.headerFields | ||
} | ||
} | ||
|
||
extension HTTPResponse: CustomDebugStringConvertible { | ||
|
@@ -273,7 +318,7 @@ extension HTTPResponse: Codable { | |
public func encode(to encoder: Encoder) throws { | ||
var container = encoder.container(keyedBy: CodingKeys.self) | ||
try container.encode(self.pseudoHeaderFields, forKey: .pseudoHeaderFields) | ||
try container.encode(self.reasonPhrase, forKey: .reasonPhrase) | ||
try container.encode(self.pseudoHeaderFields.reasonPhrase, forKey: .reasonPhrase) | ||
try container.encode(self.headerFields, forKey: .headerFields) | ||
} | ||
|
||
|
@@ -288,7 +333,7 @@ extension HTTPResponse: Codable { | |
guard Status.isValidReasonPhrase(reasonPhrase) else { | ||
throw DecodingError.invalidReasonPhrase(reasonPhrase) | ||
} | ||
self.reasonPhrase = reasonPhrase | ||
self.pseudoHeaderFields.reasonPhrase = reasonPhrase | ||
self.headerFields = try container.decode(HTTPFields.self, forKey: .headerFields) | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A bit of a nit, but these accessors probably want to either replace
set
with_modify
or to use@inlinable
. Otherwise minor modifications can risk CoWing the underlying strings where it isn't necessary to do so.This isn't a huge deal as I don't think there's a lot of minor string modifications happening this way (pretty rare to do
request.path.value += "/something/else"
) but it'd be nice to avoid the CoW in that case.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not very familiar with
_modify
. Does this look reasonable?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe that will be fine, though
_modify
is extremely sensitive to the exact nature of its flow control. I recommend actually building this under-O
and checking that there isn't a call tomalloc
or similar there.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a call to
malloc
after this change. Not sure how to get rid of it.libHTTPTypes.dylib.zip
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will take a look