mirror of
https://github.com/apple/swift-http-types
synced 2026-11-28 05:14:20 +00:00
Add inlinables (#149)
### 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
This commit is contained in:
1 parent
f41c097554
commit
6aff14dae9
6 files changed
+312
-81
No files matched your search
@@ -21,28 +21,36 @@ public struct HTTPField: Sendable, Hashable {
|
||||
/// The strategy for whether the field is indexed in the HPACK or QPACK dynamic table.
|
||||
public struct DynamicTableIndexingStrategy: Sendable, Hashable {
|
||||
/// Default strategy.
|
||||
@inlinable
|
||||
public static var automatic: Self { .init(uncheckedValue: 0) }
|
||||
|
||||
/// Always put this field in the dynamic table if possible.
|
||||
@inlinable
|
||||
public static var prefer: Self { .init(uncheckedValue: 1) }
|
||||
|
||||
/// Don't put this field in the dynamic table.
|
||||
@inlinable
|
||||
public static var avoid: Self { .init(uncheckedValue: 2) }
|
||||
|
||||
/// Don't put this field in the dynamic table, and set a flag to disallow intermediaries to
|
||||
/// index this field.
|
||||
@inlinable
|
||||
public static var disallow: Self { .init(uncheckedValue: 3) }
|
||||
|
||||
fileprivate let rawValue: UInt8
|
||||
@usableFromInline
|
||||
let rawValue: UInt8
|
||||
|
||||
private static let maxRawValue: UInt8 = 3
|
||||
@inlinable
|
||||
static var maxRawValue: UInt8 { 3 }
|
||||
|
||||
private init(uncheckedValue: UInt8) {
|
||||
@inlinable
|
||||
init(uncheckedValue: UInt8) {
|
||||
assert(uncheckedValue <= Self.maxRawValue)
|
||||
self.rawValue = uncheckedValue
|
||||
}
|
||||
|
||||
fileprivate init?(rawValue: UInt8) {
|
||||
@inlinable
|
||||
init?(rawValue: UInt8) {
|
||||
if rawValue > Self.maxRawValue {
|
||||
return nil
|
||||
}
|
||||
@@ -55,6 +63,7 @@ public struct HTTPField: Sendable, Hashable {
|
||||
/// - name: The HTTP field name.
|
||||
/// - value: The HTTP field value is initialized from the UTF-8 encoded bytes of the string.
|
||||
/// Invalid bytes are converted into space characters.
|
||||
@inlinable
|
||||
public init(name: Name, value: String) {
|
||||
self.name = name
|
||||
self.rawValue = HTTPField.Value(legalize: value)
|
||||
@@ -64,6 +73,7 @@ public struct HTTPField: Sendable, Hashable {
|
||||
/// - Parameters:
|
||||
/// - name: The HTTP field name.
|
||||
/// - value: The HTTP field value. Invalid bytes are converted into space characters.
|
||||
@inlinable
|
||||
public init(name: Name, value: some Collection<UInt8>) {
|
||||
self.name = name
|
||||
self.rawValue = HTTPField.Value(legalize: value)
|
||||
@@ -75,6 +85,7 @@ public struct HTTPField: Sendable, Hashable {
|
||||
/// - lenientValue: The HTTP field value. Newlines and NULs are converted into space
|
||||
/// characters.
|
||||
@available(HTTPTypes 1.1, *)
|
||||
@inlinable
|
||||
public init(name: Name, lenientValue: some Collection<UInt8>) {
|
||||
self.name = name
|
||||
self.rawValue = HTTPField.Value(lenient: lenientValue)
|
||||
@@ -96,6 +107,7 @@ public struct HTTPField: Sendable, Hashable {
|
||||
///
|
||||
/// If the field is not UTF-8 encoded, `withUnsafeBytesOfValue` can be used to access the
|
||||
/// underlying bytes of the field value.
|
||||
@inlinable
|
||||
public var value: String {
|
||||
get {
|
||||
self.rawValue.string
|
||||
@@ -114,6 +126,7 @@ public struct HTTPField: Sendable, Hashable {
|
||||
///
|
||||
/// - Parameter body: The closure to be invoked with the buffer.
|
||||
/// - Returns: Result of the `body` closure.
|
||||
@inlinable
|
||||
public func withUnsafeBytesOfValue<Result, Failure: Error>(
|
||||
_ body: (UnsafeBufferPointer<UInt8>) throws(Failure) -> Result
|
||||
) throws(Failure) -> Result {
|
||||
@@ -123,6 +136,7 @@ public struct HTTPField: Sendable, Hashable {
|
||||
/// The strategy for whether the field is indexed in the HPACK or QPACK dynamic table.
|
||||
public var indexingStrategy: DynamicTableIndexingStrategy = .automatic
|
||||
|
||||
@usableFromInline
|
||||
var rawValue: HTTPField.Value
|
||||
|
||||
/// Whether the string is valid for an HTTP field value based on RFC 9110.
|
||||
@@ -131,6 +145,7 @@ public struct HTTPField: Sendable, Hashable {
|
||||
///
|
||||
/// - Parameter value: The string to validate.
|
||||
/// - Returns: Whether the string is valid.
|
||||
@inlinable
|
||||
public static func isValidValue(_ value: String) -> Bool {
|
||||
Self.Value.isValid(value)
|
||||
}
|
||||
@@ -141,6 +156,7 @@ public struct HTTPField: Sendable, Hashable {
|
||||
///
|
||||
/// - Parameter value: The byte collection to validate.
|
||||
/// - Returns: Whether the byte collection is valid.
|
||||
@inlinable
|
||||
public static func isValidValue(_ value: some Collection<UInt8>) -> Bool {
|
||||
Self.Value.isValid(value)
|
||||
}
|
||||
@@ -196,11 +212,13 @@ extension HTTPField: Codable {
|
||||
|
||||
@available(HTTPTypes 1.0, *)
|
||||
extension HTTPField {
|
||||
private enum TokenValidity {
|
||||
@usableFromInline
|
||||
enum TokenValidity {
|
||||
case invalid
|
||||
case valid
|
||||
case canonical
|
||||
|
||||
@inlinable
|
||||
var isValid: Bool {
|
||||
switch self {
|
||||
case .valid, .canonical:
|
||||
@@ -211,18 +229,22 @@ extension HTTPField {
|
||||
}
|
||||
}
|
||||
|
||||
private static var digits: ClosedRange<UInt8> {
|
||||
@inlinable
|
||||
static var digits: ClosedRange<UInt8> {
|
||||
UInt8(ascii: "0")...UInt8(ascii: "9")
|
||||
}
|
||||
|
||||
private static var lowerCaseLetters: ClosedRange<UInt8> {
|
||||
@inlinable
|
||||
static var lowerCaseLetters: ClosedRange<UInt8> {
|
||||
UInt8(ascii: "a")...UInt8(ascii: "z")
|
||||
}
|
||||
|
||||
private static var upperCaseLetters: ClosedRange<UInt8> {
|
||||
@inlinable
|
||||
static var upperCaseLetters: ClosedRange<UInt8> {
|
||||
UInt8(ascii: "A")...UInt8(ascii: "Z")
|
||||
}
|
||||
|
||||
@inlinable
|
||||
static func validatedCanonicalName(_ name: String) -> String? {
|
||||
switch Self.tokenValidity(name) {
|
||||
case .canonical:
|
||||
@@ -234,20 +256,24 @@ extension HTTPField {
|
||||
}
|
||||
}
|
||||
|
||||
@inlinable
|
||||
static func isValidToken(_ token: String) -> Bool {
|
||||
Self.tokenValidity(token).isValid
|
||||
}
|
||||
|
||||
@inlinable
|
||||
static func isValidToken(_ token: Substring) -> Bool {
|
||||
Self.tokenValidity(token).isValid
|
||||
}
|
||||
|
||||
@inlinable
|
||||
static func isValidToken(_ bytes: some Collection<UInt8>) -> Bool {
|
||||
Self.tokenValidity(bytes).isValid
|
||||
}
|
||||
|
||||
#if compiler(>=6.3) && !(os(watchOS) && _pointerBitWidth(_32))
|
||||
private static func tokenValidity(_ token: String) -> TokenValidity {
|
||||
@inlinable
|
||||
static func tokenValidity(_ token: String) -> TokenValidity {
|
||||
#if canImport(Darwin)
|
||||
if #available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, visionOS 26.0, *) {
|
||||
return Self.tokenValidity(token.utf8.span)
|
||||
@@ -258,7 +284,8 @@ extension HTTPField {
|
||||
#endif
|
||||
}
|
||||
|
||||
private static func tokenValidity(_ token: Substring) -> TokenValidity {
|
||||
@inlinable
|
||||
static func tokenValidity(_ token: Substring) -> TokenValidity {
|
||||
#if canImport(Darwin)
|
||||
if #available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, visionOS 26.0, *) {
|
||||
return Self.tokenValidity(token.utf8.span)
|
||||
@@ -270,7 +297,8 @@ extension HTTPField {
|
||||
}
|
||||
|
||||
@available(macOS 26.0, iOS 26.0, watchOS 26.0, tvOS 26.0, visionOS 26.0, *)
|
||||
private static func tokenValidity(_ buffer: borrowing Span<UInt8>) -> TokenValidity {
|
||||
@inlinable
|
||||
static func tokenValidity(_ buffer: borrowing Span<UInt8>) -> TokenValidity {
|
||||
// Checks validity of token based on [RFC 9110](https://www.rfc-editor.org/rfc/rfc9110.html#name-tokens)
|
||||
if buffer.isEmpty {
|
||||
return .invalid
|
||||
@@ -294,12 +322,14 @@ extension HTTPField {
|
||||
return validity
|
||||
}
|
||||
#else
|
||||
private static func tokenValidity(_ token: some StringProtocol) -> TokenValidity {
|
||||
@inlinable
|
||||
static func tokenValidity(_ token: some StringProtocol) -> TokenValidity {
|
||||
Self.tokenValidity(token.utf8)
|
||||
}
|
||||
#endif
|
||||
|
||||
private static func tokenValidity(_ bytes: some Collection<UInt8>) -> TokenValidity {
|
||||
@inlinable
|
||||
static func tokenValidity(_ bytes: some Collection<UInt8>) -> TokenValidity {
|
||||
// Checks validity of token based on [RFC 9110](https://www.rfc-editor.org/rfc/rfc9110.html#name-tokens)
|
||||
if bytes.isEmpty {
|
||||
return .invalid
|
||||
|
||||
Reference in new issue
Block a user