Skip to content

Commit eca7e3e

Browse files
committed
Add @inlinable and @_transparent for cross-module optimization
Applied performance annotations: - @inlinable on hot paths (Binary64/Binary32 bytes() and value() methods) - @_transparent on thin wrappers (Double/Float extension methods) Performance impact: - Round-trip 10K doubles: 7.20ms → 5.21ms (28% faster) - All API paths: 175.88µs → 125.75µs (28% faster) - Alternating endianness: 131.92µs → 72.42µs (45% faster) Combined with deserialization optimization: - Total improvement: 26.39ms → 5.21ms (5.1x faster overall) The @inlinable attribute allows cross-module inlining when using this library, while @_transparent forces inlining of trivial wrappers for zero-cost abstractions.
1 parent cbbea3b commit eca7e3e

File tree

4 files changed

+14
-0
lines changed

4 files changed

+14
-0
lines changed

Sources/IEEE_754/Double+IEEE_754.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ extension Double {
4646
/// let value = Double(bytes: data)
4747
/// let value = Double(bytes: data, endianness: .big)
4848
/// ```
49+
@_transparent
4950
public init?(bytes: [UInt8], endianness: [UInt8].Endianness = .little) {
5051
guard let value = IEEE_754.Binary64.value(from: bytes, endianness: endianness) else {
5152
return nil
@@ -72,6 +73,7 @@ extension Double {
7273
/// let bytes = value.bytes()
7374
/// let bytes = value.bytes(endianness: .big)
7475
/// ```
76+
@_transparent
7577
public func bytes(endianness: [UInt8].Endianness = .little) -> [UInt8] {
7678
IEEE_754.Binary64.bytes(from: self, endianness: endianness)
7779
}
@@ -94,6 +96,7 @@ extension Double {
9496
/// let value = Double.ieee754(bytes)
9597
/// let value = Double.ieee754(bytes, endianness: .big)
9698
/// ```
99+
@_transparent
97100
public static func ieee754(
98101
_ bytes: [UInt8],
99102
endianness: [UInt8].Endianness = .little
@@ -117,6 +120,7 @@ extension Double.IEEE754 {
117120
/// let bytes = (3.14159).ieee754.bytes()
118121
/// let bytes = (3.14159).ieee754.bytes(endianness: .big)
119122
/// ```
123+
@_transparent
120124
public func bytes(endianness: [UInt8].Endianness = .little) -> [UInt8] {
121125
IEEE_754.Binary64.bytes(from: double, endianness: endianness)
122126
}
@@ -129,6 +133,7 @@ extension Double.IEEE754 {
129133
/// ```swift
130134
/// let pattern = (3.14159).ieee754.bitPattern
131135
/// ```
136+
@_transparent
132137
public var bitPattern: UInt64 {
133138
double.bitPattern
134139
}

Sources/IEEE_754/Float+IEEE_754.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ extension Float {
4646
/// let value = Float(bytes: data)
4747
/// let value = Float(bytes: data, endianness: .big)
4848
/// ```
49+
@_transparent
4950
public init?(bytes: [UInt8], endianness: [UInt8].Endianness = .little) {
5051
guard let value = IEEE_754.Binary32.value(from: bytes, endianness: endianness) else {
5152
return nil
@@ -72,6 +73,7 @@ extension Float {
7273
/// let bytes = value.bytes()
7374
/// let bytes = value.bytes(endianness: .big)
7475
/// ```
76+
@_transparent
7577
public func bytes(endianness: [UInt8].Endianness = .little) -> [UInt8] {
7678
IEEE_754.Binary32.bytes(from: self, endianness: endianness)
7779
}
@@ -94,6 +96,7 @@ extension Float {
9496
/// let value = Float.ieee754(bytes)
9597
/// let value = Float.ieee754(bytes, endianness: .big)
9698
/// ```
99+
@_transparent
97100
public static func ieee754(
98101
_ bytes: [UInt8],
99102
endianness: [UInt8].Endianness = .little
@@ -117,6 +120,7 @@ extension Float.IEEE754 {
117120
/// let bytes = Float(3.14).ieee754.bytes()
118121
/// let bytes = Float(3.14).ieee754.bytes(endianness: .big)
119122
/// ```
123+
@_transparent
120124
public func bytes(endianness: [UInt8].Endianness = .little) -> [UInt8] {
121125
IEEE_754.Binary32.bytes(from: float, endianness: endianness)
122126
}
@@ -129,6 +133,7 @@ extension Float.IEEE754 {
129133
/// ```swift
130134
/// let pattern = Float(3.14).ieee754.bitPattern
131135
/// ```
136+
@_transparent
132137
public var bitPattern: UInt32 {
133138
float.bitPattern
134139
}

Sources/IEEE_754/IEEE_754.Binary32.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ extension IEEE_754.Binary32 {
8585
/// let bytes = IEEE_754.Binary32.bytes(from: Float(3.14))
8686
/// let bytes = IEEE_754.Binary32.bytes(from: Float(3.14), endianness: .big)
8787
/// ```
88+
@inlinable
8889
public static func bytes(
8990
from value: Float,
9091
endianness: [UInt8].Endianness = .little
@@ -112,6 +113,7 @@ extension IEEE_754.Binary32 {
112113
/// let value = IEEE_754.Binary32.value(from: bytes)
113114
/// let value = IEEE_754.Binary32.value(from: bytes, endianness: .big)
114115
/// ```
116+
@inlinable
115117
public static func value(
116118
from bytes: [UInt8],
117119
endianness: [UInt8].Endianness = .little

Sources/IEEE_754/IEEE_754.Binary64.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ extension IEEE_754.Binary64 {
8585
/// let bytes = IEEE_754.Binary64.bytes(from: 3.14159)
8686
/// let bytes = IEEE_754.Binary64.bytes(from: 3.14159, endianness: .big)
8787
/// ```
88+
@inlinable
8889
public static func bytes(
8990
from value: Double,
9091
endianness: [UInt8].Endianness = .little
@@ -112,6 +113,7 @@ extension IEEE_754.Binary64 {
112113
/// let value = IEEE_754.Binary64.value(from: bytes)
113114
/// let value = IEEE_754.Binary64.value(from: bytes, endianness: .big)
114115
/// ```
116+
@inlinable
115117
public static func value(
116118
from bytes: [UInt8],
117119
endianness: [UInt8].Endianness = .little

0 commit comments

Comments
 (0)