mirror of
https://github.com/apple/swift-nio
synced 2026-11-28 05:14:23 +00:00
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.
135 lines
2.8 KiB
Swift
135 lines
2.8 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// MARK: _UInt24
|
|
|
|
/// A 24-bit unsigned integer value type.
|
|
@usableFromInline
|
|
struct _UInt24: Sendable {
|
|
@usableFromInline var _backing: (UInt16, UInt8)
|
|
|
|
@inlinable
|
|
init(_ value: UInt32) {
|
|
assert(value & 0xff_00_00_00 == 0, "value \(value) too large for _UInt24")
|
|
self._backing = IntegerBitPacking.unpackUInt16UInt8(value)
|
|
}
|
|
|
|
@inlinable
|
|
static var bitWidth: Int {
|
|
24
|
|
}
|
|
|
|
@inlinable
|
|
static var max: _UInt24 {
|
|
.init((UInt32(1) << 24) - 1)
|
|
}
|
|
|
|
@inlinable
|
|
static var min: _UInt24 {
|
|
.init(0)
|
|
}
|
|
}
|
|
|
|
extension UInt32 {
|
|
@inlinable
|
|
init(_ value: _UInt24) {
|
|
self = IntegerBitPacking.packUInt16UInt8(value._backing.0, value._backing.1)
|
|
}
|
|
}
|
|
|
|
extension Int {
|
|
@inlinable
|
|
init(_ value: _UInt24) {
|
|
self = Int(UInt32(value))
|
|
}
|
|
}
|
|
|
|
extension _UInt24: Equatable {
|
|
@inlinable
|
|
public static func == (lhs: _UInt24, rhs: _UInt24) -> Bool {
|
|
lhs._backing == rhs._backing
|
|
}
|
|
}
|
|
|
|
extension _UInt24: CustomStringConvertible {
|
|
@usableFromInline
|
|
var description: String {
|
|
UInt32(self).description
|
|
}
|
|
}
|
|
|
|
// MARK: _UInt56
|
|
|
|
/// A 56-bit unsigned integer value type.
|
|
struct _UInt56: Sendable {
|
|
@usableFromInline var _backing: (UInt32, UInt16, UInt8)
|
|
|
|
@inlinable init(_ value: UInt64) {
|
|
self._backing = IntegerBitPacking.unpackUInt32UInt16UInt8(value)
|
|
}
|
|
|
|
@inlinable
|
|
static var bitWidth: Int {
|
|
56
|
|
}
|
|
|
|
@inlinable
|
|
static var _initializeUInt64: UInt64 {
|
|
(1 << 56) - 1
|
|
}
|
|
@inlinable
|
|
static var max: _UInt56 {
|
|
.init(_initializeUInt64)
|
|
}
|
|
@inlinable
|
|
static var min: _UInt56 {
|
|
.init(0)
|
|
}
|
|
}
|
|
|
|
extension _UInt56 {
|
|
init(_ value: Int) {
|
|
self.init(UInt64(value))
|
|
}
|
|
}
|
|
|
|
extension UInt64 {
|
|
init(_ value: _UInt56) {
|
|
self = IntegerBitPacking.packUInt32UInt16UInt8(
|
|
value._backing.0,
|
|
value._backing.1,
|
|
value._backing.2
|
|
)
|
|
}
|
|
}
|
|
|
|
extension Int {
|
|
init(_ value: _UInt56) {
|
|
self = Int(UInt64(value))
|
|
}
|
|
}
|
|
|
|
extension _UInt56: Equatable {
|
|
@inlinable
|
|
public static func == (lhs: _UInt56, rhs: _UInt56) -> Bool {
|
|
lhs._backing == rhs._backing
|
|
}
|
|
}
|
|
|
|
extension _UInt56: CustomStringConvertible {
|
|
var description: String {
|
|
UInt64(self).description
|
|
}
|
|
}
|