Skip to content

Commit 770fd7a

Browse files
committed
[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 d68153c commit 770fd7a

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)
@@ -252,16 +254,11 @@ extension DistributedActorSystem {
252254
on: actor,
253255
mangledTargetName, UInt(mangledTargetName.count),
254256
argumentBuffer: hargs.buffer._rawValue,
255-
resultBuffer: resultBuffer?._rawValue
257+
resultBuffer: resultBuffer._rawValue
256258
)
257259

258-
// Get the result out of the buffer and invoke onReturn with the right type
259-
guard let resultBuffer = resultBuffer else {
260-
try await handler.onReturn(value: ())
261-
return
262-
}
263-
func onReturn<R>(_: R.Type) async throws {
264-
try await handler.onReturn/*<R>*/(value: resultBuffer)
260+
func onReturn<R>(_ resultTy: R.Type) async throws {
261+
try await handler.onReturn/*<R>*/(value: resultBuffer.load(as: resultTy))
265262
}
266263

267264
try await _openExistential(returnType, do: onReturn)
@@ -277,7 +274,7 @@ func _executeDistributedTarget(
277274
on actor: AnyObject, // DistributedActor
278275
_ targetName: UnsafePointer<UInt8>, _ targetNameLength: UInt,
279276
argumentBuffer: Builtin.RawPointer, // HeterogeneousBuffer of arguments
280-
resultBuffer: Builtin.RawPointer?
277+
resultBuffer: Builtin.RawPointer
281278
) async throws
282279

283280
// ==== ----------------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)