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
Allow adding a sequence of headers to HTTPHeaders #1004
Conversation
Motivation: When combining two collections of headers, adding each pair one-by-one using add(name:value:) can cause multiple unnecessary reallocations of the underlying array. Modifications: Provide two additional add methods to HTTPHeaders: one to add a sequence of name/value pairs and another to add the headers from another HTTPHeaders, both of which use `append(contentsOf:)` on the underlying array to avoid additional reallocations. Result: Fewer reallocations when adding multiple headers.
Sources/NIOHTTP1/HTTPTypes.swift
Outdated
/// Add a sequence of header name/value pairs to the block. | ||
/// | ||
/// This method is strictly additive: if there are other entries with the same header | ||
/// name already in the block, this will add new entries. `add` performs case-insensitive |
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.
This comment looks wrong: we don't compare the field names at all, except inasmuch as we're looking for the Connection header.
Sources/NIOHTTP1/HTTPTypes.swift
Outdated
@inlinable | ||
public mutating func add<S: Sequence>(contentsOf other: S) where S.Element == (String, String) { | ||
precondition(!other.contains { !$0.0.utf8.contains(where: { !$0.isASCII }) }, "names must be ASCII") | ||
self.headers.append(contentsOf: other) |
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.
why doesn't this call self.add
for every element (whilst reserving the capacity in self.headers
beforehands)? Don't really like the logic duplication here.
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.
Sure, makes sense!
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.
thanks, that looks great
Motivation:
When combining two collections of headers, adding each pair one-by-one
using add(name:value:) can cause multiple unnecessary reallocations of
the underlying array.
Modifications:
Provide two additional add methods to HTTPHeaders: one to add a sequence
of name/value pairs and another to add the headers from another
HTTPHeaders, both of which use
append(contentsOf:)
on the underlyingarray to avoid additional reallocations.
Result:
Fewer reallocations when adding multiple headers.