Skip to content

Commit c1f830b

Browse files
xedinktoso
authored andcommitted
[Distributed] Drop optionality from result buffer in _executeDistributedTarget
`RawPointer?` is lowered into a two arguments since it's a struct, to make it easy let's just allocate an empty pointer for `Void` result.
1 parent c83c2c3 commit c1f830b

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

stdlib/public/Distributed/DistributedActorSystem.swift

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -222,18 +222,20 @@ extension DistributedActorSystem {
222222

223223
// Decode the return type
224224
func allocateReturnTypeBuffer<R>(_: R.Type) -> UnsafeRawPointer? {
225-
if R.self == Void.self {
226-
return nil
227-
}
228225
return UnsafeRawPointer(UnsafeMutablePointer<R>.allocate(capacity: 1))
229226
}
230227
guard let returnType: Any.Type = _getReturnTypeInfo(mangledMethodName: mangledTargetName) else {
231228
throw ExecuteDistributedTargetError(
232229
message: "Failed to decode distributed target return type")
233230
}
234-
let resultBuffer = _openExistential(returnType, do: allocateReturnTypeBuffer)
231+
232+
guard let resultBuffer = _openExistential(returnType, do: allocateReturnTypeBuffer) else {
233+
throw ExecuteDistributedTargetError(
234+
message: "Failed to allocate buffer for distributed target return type")
235+
}
236+
235237
func destroyReturnTypeBuffer<R>(_: R.Type) {
236-
resultBuffer?.assumingMemoryBound(to: R.self).deallocate()
238+
resultBuffer.assumingMemoryBound(to: R.self).deallocate()
237239
}
238240
defer {
239241
_openExistential(returnType, do: destroyReturnTypeBuffer)
@@ -280,16 +282,11 @@ extension DistributedActorSystem {
280282
on: actor,
281283
mangledTargetName, UInt(mangledTargetName.count),
282284
argumentBuffer: hargs.buffer._rawValue,
283-
resultBuffer: resultBuffer?._rawValue
285+
resultBuffer: resultBuffer._rawValue
284286
)
285287

286-
// Get the result out of the buffer and invoke onReturn with the right type
287-
guard let resultBuffer = resultBuffer else {
288-
try await handler.onReturn(value: ())
289-
return
290-
}
291-
func onReturn<R>(_: R.Type) async throws {
292-
try await handler.onReturn/*<R>*/(value: resultBuffer)
288+
func onReturn<R>(_ resultTy: R.Type) async throws {
289+
try await handler.onReturn/*<R>*/(value: resultBuffer.load(as: resultTy))
293290
}
294291

295292
try await _openExistential(returnType, do: onReturn)
@@ -305,7 +302,7 @@ func _executeDistributedTarget(
305302
on actor: AnyObject, // DistributedActor
306303
_ targetName: UnsafePointer<UInt8>, _ targetNameLength: UInt,
307304
argumentBuffer: Builtin.RawPointer, // HeterogeneousBuffer of arguments
308-
resultBuffer: Builtin.RawPointer?
305+
resultBuffer: Builtin.RawPointer
309306
) async throws
310307

311308
// ==== ----------------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)