Replace almost all public static lets with computed vars (#3229)

Motivation:

Public static lets can serve a bunch of roles, but one of them is to
store simple constants: integers, and other trivial types, for example.
This is a nice pattern and for internal and private static lets it works
well, but for public ones it produces some inefficient code.

In particular, it has two downsides. First, it allocates storage for
that value. We don't actually need to allocate a few hundred extra
megabytes for the various integers we want to store.

Secondly, it forces calling code to access the address and call the
dispatch_once code in order to get hold of the value. For trivial types
we don't need that cost: they can just know what the value is directly.

Inlinable computed vars avoid all of these costs: they have no size
overhead for storage, and they are visible to all clients so their
values can be directly assembled.

While I'm here, I added a bunch of other inlinable annotations for a few
trivial data types I stumbled onto.

Modifications:

- Added loads of inlinables. Like, loads.
- Swapped many static lets to static vars.

Result:

Better codegen, smaller memory footprints, more attributes.
This commit is contained in:
Cory Benfield authored and GitHub committed 2025-05-02 16:08:10 +01:00
1 parent ea8703616e
commit abe4f1bcb6
14 files changed
+517 -156

No files matched your search

+25 -7
View File
@@ -691,15 +691,17 @@ public struct HTTPVersion: Equatable, Sendable {
///
/// - Parameter major: The major version number.
/// - Parameter minor: The minor version number.
@inlinable
public init(major: Int, minor: Int) {
self._major = UInt16(major)
self._minor = UInt16(minor)
}
private var _minor: UInt16
private var _major: UInt16
@usableFromInline var _minor: UInt16
@usableFromInline var _major: UInt16
/// The major version number.
@inlinable
public var major: Int {
get {
Int(self._major)
@@ -710,6 +712,7 @@ public struct HTTPVersion: Equatable, Sendable {
}
/// The minor version number.
@inlinable
public var minor: Int {
get {
Int(self._minor)
@@ -720,19 +723,34 @@ public struct HTTPVersion: Equatable, Sendable {
}
/// HTTP/3
public static let http3 = HTTPVersion(major: 3, minor: 0)
@inlinable
public static var http3: HTTPVersion {
HTTPVersion(major: 3, minor: 0)
}
/// HTTP/2
public static let http2 = HTTPVersion(major: 2, minor: 0)
@inlinable
public static var http2: HTTPVersion {
HTTPVersion(major: 2, minor: 0)
}
/// HTTP/1.1
public static let http1_1 = HTTPVersion(major: 1, minor: 1)
@inlinable
public static var http1_1: HTTPVersion {
HTTPVersion(major: 1, minor: 1)
}
/// HTTP/1.0
public static let http1_0 = HTTPVersion(major: 1, minor: 0)
@inlinable
public static var http1_0: HTTPVersion {
HTTPVersion(major: 1, minor: 0)
}
/// HTTP/0.9 (not supported by SwiftNIO)
public static let http0_9 = HTTPVersion(major: 0, minor: 9)
@inlinable
public static var http0_9: HTTPVersion {
HTTPVersion(major: 0, minor: 9)
}
}
extension HTTPParserError: CustomDebugStringConvertible {