Skip to content

Commit 5924fb6

Browse files
authored
Add inlinable where potentially usefull (#511)
Allow the compiler to specialize more code by applying `@inlinable` to generic code.
1 parent 8939095 commit 5924fb6

9 files changed

+50
-8
lines changed

Sources/AWSLambdaRuntime/ControlPlaneRequest.swift

+8-1
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,19 @@ enum ControlPlaneResponse: Hashable {
2828
case error(ErrorResponse)
2929
}
3030

31-
package struct InvocationMetadata: Hashable {
31+
@usableFromInline
32+
package struct InvocationMetadata: Hashable, Sendable {
33+
@usableFromInline
3234
package let requestID: String
35+
@usableFromInline
3336
package let deadlineInMillisSinceEpoch: Int64
37+
@usableFromInline
3438
package let invokedFunctionARN: String
39+
@usableFromInline
3540
package let traceID: String
41+
@usableFromInline
3642
package let clientContext: String?
43+
@usableFromInline
3744
package let cognitoIdentity: String?
3845

3946
package init(headers: HTTPHeaders) throws(LambdaRuntimeError) {

Sources/AWSLambdaRuntime/Lambda+LocalServer.swift

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ extension Lambda {
4545
/// - body: Code to run within the context of the mock server. Typically this would be a Lambda.run function call.
4646
///
4747
/// - note: This API is designed strictly for local testing and is behind a DEBUG flag
48+
@usableFromInline
4849
static func withLocalServer(
4950
invocationEndpoint: String? = nil,
5051
_ body: sending @escaping () async throws -> Void

Sources/AWSLambdaRuntime/Lambda.swift

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import ucrt
3030
#endif
3131

3232
public enum Lambda {
33+
@inlinable
3334
package static func runLoop<RuntimeClient: LambdaRuntimeClientProtocol, Handler>(
3435
runtimeClient: RuntimeClient,
3536
handler: Handler,

Sources/AWSLambdaRuntime/LambdaContext.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public struct LambdaContext: CustomDebugStringConvertible, Sendable {
8888
self.storage.logger
8989
}
9090

91-
init(
91+
public init(
9292
requestID: String,
9393
traceID: String,
9494
invokedFunctionARN: String,

Sources/AWSLambdaRuntime/LambdaRuntime.swift

+4
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ import Foundation
2727
// sadly crashes the compiler today.
2828
public final class LambdaRuntime<Handler>: @unchecked Sendable where Handler: StreamingLambdaHandler {
2929
// TODO: We want to change this to Mutex as soon as this doesn't crash the Swift compiler on Linux anymore
30+
@usableFromInline
3031
let handlerMutex: NIOLockedValueBox<Handler?>
32+
@usableFromInline
3133
let logger: Logger
34+
@usableFromInline
3235
let eventLoop: EventLoop
3336

3437
public init(
@@ -48,6 +51,7 @@ public final class LambdaRuntime<Handler>: @unchecked Sendable where Handler: St
4851
self.logger.debug("LambdaRuntime initialized")
4952
}
5053

54+
@inlinable
5155
public func run() async throws {
5256
let handler = self.handlerMutex.withLockedValue { handler in
5357
let result = handler

Sources/AWSLambdaRuntime/LambdaRuntimeClient.swift

+22-4
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,47 @@ import NIOCore
1717
import NIOHTTP1
1818
import NIOPosix
1919

20+
@usableFromInline
2021
final actor LambdaRuntimeClient: LambdaRuntimeClientProtocol {
22+
@usableFromInline
2123
nonisolated let unownedExecutor: UnownedSerialExecutor
2224

23-
struct Configuration {
25+
@usableFromInline
26+
struct Configuration: Sendable {
2427
var ip: String
2528
var port: Int
29+
30+
@usableFromInline
31+
init(ip: String, port: Int) {
32+
self.ip = ip
33+
self.port = port
34+
}
2635
}
2736

28-
struct Writer: LambdaRuntimeClientResponseStreamWriter {
37+
@usableFromInline
38+
struct Writer: LambdaRuntimeClientResponseStreamWriter, Sendable {
2939
private var runtimeClient: LambdaRuntimeClient
3040

3141
fileprivate init(runtimeClient: LambdaRuntimeClient) {
3242
self.runtimeClient = runtimeClient
3343
}
3444

45+
@usableFromInline
3546
func write(_ buffer: NIOCore.ByteBuffer) async throws {
3647
try await self.runtimeClient.write(buffer)
3748
}
3849

50+
@usableFromInline
3951
func finish() async throws {
4052
try await self.runtimeClient.writeAndFinish(nil)
4153
}
4254

55+
@usableFromInline
4356
func writeAndFinish(_ buffer: NIOCore.ByteBuffer) async throws {
4457
try await self.runtimeClient.writeAndFinish(buffer)
4558
}
4659

60+
@usableFromInline
4761
func reportError(_ error: any Error) async throws {
4862
try await self.runtimeClient.reportError(error)
4963
}
@@ -90,6 +104,7 @@ final actor LambdaRuntimeClient: LambdaRuntimeClientProtocol {
90104
// being fully closed before we can return from it.
91105
private var closingConnections: [any Channel] = []
92106

107+
@inlinable
93108
static func withRuntimeClient<Result>(
94109
configuration: Configuration,
95110
eventLoop: any EventLoop,
@@ -110,14 +125,16 @@ final actor LambdaRuntimeClient: LambdaRuntimeClientProtocol {
110125
return try result.get()
111126
}
112127

113-
private init(configuration: Configuration, eventLoop: any EventLoop, logger: Logger) {
128+
@usableFromInline
129+
init(configuration: Configuration, eventLoop: any EventLoop, logger: Logger) {
114130
self.unownedExecutor = eventLoop.executor.asUnownedSerialExecutor()
115131
self.configuration = configuration
116132
self.eventLoop = eventLoop
117133
self.logger = logger
118134
}
119135

120-
private func close() async {
136+
@usableFromInline
137+
func close() async {
121138
self.logger.trace("Close lambda runtime client")
122139

123140
guard case .notClosing = self.closingState else {
@@ -144,6 +161,7 @@ final actor LambdaRuntimeClient: LambdaRuntimeClientProtocol {
144161
}
145162
}
146163

164+
@usableFromInline
147165
func nextInvocation() async throws -> (Invocation, Writer) {
148166
try await withTaskCancellationHandler {
149167
switch self.lambdaState {

Sources/AWSLambdaRuntime/LambdaRuntimeClientProtocol.swift

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,26 @@
1414

1515
import NIOCore
1616

17+
@usableFromInline
1718
package protocol LambdaRuntimeClientResponseStreamWriter: LambdaResponseStreamWriter {
1819
func write(_ buffer: ByteBuffer) async throws
1920
func finish() async throws
2021
func writeAndFinish(_ buffer: ByteBuffer) async throws
2122
func reportError(_ error: any Error) async throws
2223
}
2324

25+
@usableFromInline
2426
package protocol LambdaRuntimeClientProtocol {
2527
associatedtype Writer: LambdaRuntimeClientResponseStreamWriter
2628

2729
func nextInvocation() async throws -> (Invocation, Writer)
2830
}
2931

30-
package struct Invocation {
32+
@usableFromInline
33+
package struct Invocation: Sendable {
34+
@usableFromInline
3135
package var metadata: InvocationMetadata
36+
@usableFromInline
3237
package var event: ByteBuffer
3338

3439
package init(metadata: InvocationMetadata, event: ByteBuffer) {

Sources/AWSLambdaRuntime/LambdaRuntimeError.swift

+6-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
//
1313
//===----------------------------------------------------------------------===//
1414

15+
@usableFromInline
1516
package struct LambdaRuntimeError: Error {
16-
package enum Code {
17+
@usableFromInline
18+
package enum Code: Sendable {
1719
case closingRuntimeClient
1820

1921
case connectionToControlPlaneLost
@@ -34,12 +36,15 @@ package struct LambdaRuntimeError: Error {
3436
case invalidPort
3537
}
3638

39+
@usableFromInline
3740
package init(code: Code, underlying: (any Error)? = nil) {
3841
self.code = code
3942
self.underlying = underlying
4043
}
4144

45+
@usableFromInline
4246
package var code: Code
47+
@usableFromInline
4348
package var underlying: (any Error)?
4449

4550
}

Sources/AWSLambdaRuntime/Utils.swift

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ enum Signal: Int32 {
5959
}
6060

6161
extension DispatchWallTime {
62+
@usableFromInline
6263
init(millisSinceEpoch: Int64) {
6364
let nanoSinceEpoch = UInt64(millisSinceEpoch) * 1_000_000
6465
let seconds = UInt64(nanoSinceEpoch / 1_000_000_000)

0 commit comments

Comments
 (0)