### Motivation
#144 introduced a regression in `HTTPField.Name(parsed:)`.
HTTP/2 and HTTP/3 require field names to be lowercased on the wire (RFC
9113 § 8.2.1, RFC 9114 § 4.1.2). Previously we enforced this by only
accepting lowercase ASCII letters.
In #144 we replaced a specific scan with `HTTPField.isValidToken`, which
is shared in multiple token validation places and returns `valid` for
uppercased names. We checked for `isValid`, which is true for `valid` as
well as `canonical` representations. Allowing `valid` and not just
`canonical` representations is the regression.
This lead to this unwanted behavior:
- `HTTPField.Name(parsed: "Content-Type")` now succeeds, so a decoder
can no longer detect the malformed message it has to reject.
- The returned name violates the documented invariant that
`canonicalName` is lowercased, so it compares and hashes unequal to
`HTTPField.Name("Content-Type")`, which does canonicalise.
This path had no test coverage, so neither regression was caught.
### Changes
Require `.canonical` rather than merely valid token validity in
`init(parsed:)`, restoring the pre-#144 behavior, and document why. Add
`HTTPFieldNameTests` covering both initializers: valid names, pseudo
header fields, invalid characters, the mixed-case canonicalisation that
`init(_:)` performs, and the uppercase rejection that `init(parsed:)`
performs.
### Result
`init(parsed:)` rejects uppercased names again and `canonicalName` is
lowercased for every name it returns.
### Motivation
Checking for equality of multiple http fields creates an array per field
name. However, not every field will actually have multiple values, which
means we can save array allocations for fields that are only set once.
### Modifications
- Added an enum to handle single values without extra allocations
### Result
Faster equality checks.
## Summary
HTTPFields equality does not depend on the order of differently named
fields. Hashing used storage order, so equal values could hash
differently.
This hashes each name group in relative order, then mixes those group
hashes without depending on name order. The existing hash test now
asserts equal hashes.
Fixes#139
### Motivation
We want to have fast code.
### Changes
Add `@inlinable` to a number of functions, to allow better compiler
optimizations.
### Result
Improvements in some benchmarks
### Motivation
When instantiating new HTTPFields, the values need to be validated,
legalized and turned into the internal representation.
We use this representation to expose a string in `HTTPField.value` as
well as to iterate over raw bytes using
`HTTPField.withUnsafeBytesOfValue`.
Currently the internal representation is to use a ISO Latin 1 String to
store the bytes only once. This however means that we need to transcode
back and forth for non ascii values, which causes a lot of extra
computations and allocations.
### Modifications
1. Moved logic to a standalone structure for `HTTPField.Value` rather
than `ISOLatin1String`
2. Added Unit Tests and Benchmarks
3. Swapped internal storage of values to be an enum of either a string
or a value
4. Whenever the bytes stored are valid utf8, we will not always store
the value as a raw string. This means that we need the least amount of
allocations and transcoding when operating with the value through our
public API
5. We only store a raw array of bytes whenever the bytes are not valid
utf8. This speeds up `withUnsafeBytesOfValue` massively and
`HTTPField.value` a little bit
6. During legalization, store the modified bytes in a temporary buffer
instead of using lazy collections to avoid allocating an array and speed
up utf8 validation
7. Use spans whenever possible to speed up access to bytes without
boundary checks
### Result
These are the changes on the benchmarks added in this PR
```
HTTPField-asciiBytesValueRoundTrip
Instructions: 28K -> 6847 (-75%)
Malloc: 1 -> 1
HTTPField-asciiValueRoundTrip
Instructions: 7367 -> 3081 (-58%)
Malloc: 0 -> 0
HTTPField-invalidAsciiBytesValueRoundTrip
Instructions: 133K -> 7255 (-94%)
Malloc: 6 -> 1 (-83%)
HTTPField-invalidAsciiBytesValueRoundTripAsLenientValue
Instructions: 62K -> 6623 (-89%)
Malloc: 3 -> 1 (-66%)
HTTPField-invalidAsciiValueRoundTrip
Instructions: 93K -> 6959 (-92%)
Malloc: 5 -> 1 (-80%)
HTTPField-invalidNonASCIIValueRoundTrip
Instructions: 393K -> 8735 (-97%)
Malloc: 11 -> 1 (-91%)
HTTPField-nonASCIIValueRoundTrip
Instructions: 131K -> 3219 (-97%)
Malloc: 4 -> 0 (-100%)
```
---------
Co-authored-by: Fabian Fett <fabianfett@apple.com>
### Motivation
HTTP/2 and HTTP/3 both carry a numeric error code to say why a stream or
a connection was terminated. HTTPTypes has no currency type for either,
so adopters passed bare integers around or defined their own.
### Modifications
Add HTTP2ErrorCode and HTTP3ErrorCode.
### Result
HTTP/2 and HTTP/3 error codes are first-class currency types.
---------
Co-authored-by: Fabian Fett <fabianfett@apple.com>
### Motivation
Currently constructing a request or response from parsed http fields
requires multiple unnecessary allocations.
Mainly due to:
1. Allocations from resizing the array as we keep appending more values
to it
2. When running validation checks, we temporarily use an array of values
to represent the values for a given field name. When those fields are
set this requires an extra array allocation just to run the check
### Modifications
- Reserve needed capacity during initial of parsed fields to prevent
multiple subsequent allocations
- Avoid allocating an array while validating content length, content
disposition and location headers by validating them at insertion time
and therefore only iterating once
### Result
Reduction in allocations and instructions in benchmarks
Co-authored-by: Fabian Fett <fabianfett@apple.com>
# Motivation
Currently when initializing a Field Name, we create a canonical
lowercased version of the field name. However, it is reasonable to
expect that a significant amount of the time the users will insert a
name string that is already lowercased, so turning it into lowercase
creates an extra unnecessary allocation
# Changes
This changes the validation step for the name to compute in a single
pass whether the name is valid and if it's already in the canonical
lowercased format. If it's already lowercased, then we can skip the
extra allocation and just reuse the same string. if it's valid but not
lowercased we can default to the previous logic.
For ultimate performance this iterates over Spans whenever we use Swift
6.4. For other Swift versions, we default to iterating over the UTF8View
instead, which is slightly slower, but keeps us backwards compatible.
# Result
More performance
### Motivation
#138 introduced code paths that scale quadratically when running an
equality check.
### Changes
- Introduce a new lock-step equality check that performs great for small
fields and fields that share roughly the same order
- Introduce a new lookup equality check that scales linear for vastly
different field orders
### Result
Faster equality checks
### Motivation
We like faster and simpler code. When optimizing performance we focus on
often used use-cases.
### Changes
- Reimplement HTTPFields on top of a simple Array
### Result
- HTTPFields building becomes significantly faster
- HTTPFields lookups are faster for small fields, and only slightly
slower for larger (nearly always offset by reduced build time)
Follow ups:
- Make the equality implementation smarter
### Motivation
`_Storage.append(field:)` updated the name index through optional
chaining:
self.index?[name, default: (first: location, last: 0)].last = location
The optional chaining self.index? is expanded into
if var index = self.index {
index[name, default: (first: location, last: 0)].last = location // this
allocates – as dict is referenced by self.index and index
self.index = index
}
Because of this we have an allocation on every append.
### Changes
Nil-check `self.index` once, then mutate through the force-unwrapped
`self.index![name] = ...`, which is an addressable projection of the
stored property and mutates in place.
### Result
Allocations drop on every append-driven path.
The decoder assumes that the string is within the ISOLatin1 range
without checking, potentially allowing invalid values to be saved in an
HTTPField
rdar://178437865
Add a default enabled trait "FoundationURL" that can be disabled for
users who don't want Foundation dependencies. Also enable
`InternalImportsByDefault`.
Motivation:
Swift 6.0 is no longer supported, we should bump the tools version and
remove it from our CI.
Modifications:
* Bump the Swift tools version to Swift 6.1
* Remove Swift 6.0 jobs where appropriate in main.yml, pull_request.yml
Result:
Code reflects our support window.
---------
Co-authored-by: Guoye Zhang <guoye_zhang@apple.com>
Motivation:
Swift 6.2 has been released, we should add it to our CI coverage.
Modifications:
Add additional Swift 6.2 jobs where appropriate in main.yml,
pull_request.yml
Result:
Improved test coverage.
# Motivation
We just adopted GH actions and want to make sure all the CI checks are green.
# Modification
This PR makes the necessary changes to pass all checks. Including:
- Adding a licenseignore file
- Moving `swift-format` and adopting the config that we use in all other swift on server repos.
# Result
Green CI.