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

+39 -9
View File
@@ -16,20 +16,45 @@ import NIOCore
/// An operation code for a websocket frame.
public struct WebSocketOpcode: Sendable {
fileprivate let networkRepresentation: UInt8
@usableFromInline
let networkRepresentation: UInt8
public static let continuation = WebSocketOpcode(rawValue: 0x0)
public static let text = WebSocketOpcode(rawValue: 0x1)
public static let binary = WebSocketOpcode(rawValue: 0x2)
public static let connectionClose = WebSocketOpcode(rawValue: 0x8)
public static let ping = WebSocketOpcode(rawValue: 0x9)
public static let pong = WebSocketOpcode(rawValue: 0xA)
@inlinable
public static var continuation: WebSocketOpcode {
WebSocketOpcode(rawValue: 0x0)
}
@inlinable
public static var text: WebSocketOpcode {
WebSocketOpcode(rawValue: 0x1)
}
@inlinable
public static var binary: WebSocketOpcode {
WebSocketOpcode(rawValue: 0x2)
}
@inlinable
public static var connectionClose: WebSocketOpcode {
WebSocketOpcode(rawValue: 0x8)
}
@inlinable
public static var ping: WebSocketOpcode {
WebSocketOpcode(rawValue: 0x9)
}
@inlinable
public static var pong: WebSocketOpcode {
WebSocketOpcode(rawValue: 0xA)
}
/// Create an opcode from the encoded representation.
///
/// - Parameters
/// - encoded: The encoded representation of the opcode as an 8-bit integer.
/// Must be no more than 4 bits large.
@inlinable
public init?(encodedWebSocketOpcode encoded: UInt8) {
guard encoded < 0x10 else {
return nil
@@ -40,13 +65,15 @@ public struct WebSocketOpcode: Sendable {
/// Create an opcode directly with no validation.
///
/// Used only to create the static lets on this structure.
private init(rawValue: UInt8) {
/// Used only to create the static vars on this structure.
@inlinable
init(rawValue: UInt8) {
self.networkRepresentation = rawValue
}
/// Whether the opcode is in the control range: that is, if the
/// high bit of the opcode nibble is `1`.
@inlinable
public var isControlOpcode: Bool {
self.networkRepresentation & 0x8 == 0x8
}
@@ -57,6 +84,7 @@ extension WebSocketOpcode: Equatable {}
extension WebSocketOpcode: Hashable {}
extension WebSocketOpcode: CaseIterable {
@inlinable
public static var allCases: [WebSocketOpcode] {
get { (0..<0x10).map { WebSocketOpcode(rawValue: $0) } }
@@ -96,6 +124,7 @@ extension UInt8 {
///
/// - Parameters:
/// - opcode: The `WebSocketOpcode`.
@inlinable
public init(webSocketOpcode opcode: WebSocketOpcode) {
precondition(opcode.networkRepresentation < 0x10)
self = opcode.networkRepresentation
@@ -107,6 +136,7 @@ extension Int {
///
/// - Parameters:
/// - opcode: The `WebSocketOpcode`.
@inlinable
public init(webSocketOpcode opcode: WebSocketOpcode) {
precondition(opcode.networkRepresentation < 0x10)
self = Int(opcode.networkRepresentation)