Skip to content

Commit cbf4eb2

Browse files
authored
[Distributed] A couple of improvements to HeterogeneousBuffer and test-cases (#40848)
* [Distributed] NFC: Support encoding/decoding of substitutions by `FakeInvocation` * [Distributed] Fix a bug in heterogeneous buffer iterator `next()` did not advance offset by the size of the returned element * [Distributed] NFC: Adjust `echo` test to handle two arguments
1 parent 70c6b9d commit cbf4eb2

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

stdlib/public/Distributed/HeterogeneousBuffer.swift

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,20 @@ struct HeterogeneousBuffer: Sequence {
7373
return nil
7474
}
7575

76-
func nextOffset<T>(_: T.Type) -> Int {
77-
return MemoryLayout<T>.nextAlignedOffset(offset)
76+
func alignedOffsetAndSize<T>(_: T.Type) -> (offset: Int, size: Int) {
77+
return (MemoryLayout<T>.nextAlignedOffset(offset), MemoryLayout<T>.size)
78+
}
79+
80+
// Retrieve aligned offset and element size info
81+
let info = _openExistential(hbuf.types[i], do: alignedOffsetAndSize)
82+
// Adjust current offset
83+
offset = info.offset
84+
85+
defer {
86+
// Advance offset by the size of the current element
87+
offset += info.size
88+
i += 1
7889
}
79-
offset = _openExistential(hbuf.types[i], do: nextOffset)
80-
i += 1
8190

8291
return hbuf.buffer.advanced(by: offset)
8392
}

test/Distributed/Runtime/distributed_actor_remoteCall.swift

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ distributed actor Greeter {
5151
.init(q: "question", a: 42, b: 1, c: 2.0, d: "Lorum ipsum")
5252
}
5353

54-
distributed func echo(name: String) -> String {
55-
return "Echo: \(name)"
54+
distributed func echo(name: String, age: Int) -> String {
55+
return "Echo: name: \(name), age: \(age)"
5656
}
5757

5858
distributed func enumResult() -> E {
@@ -117,12 +117,13 @@ struct FakeActorSystem: DistributedActorSystem {
117117
struct FakeInvocation: DistributedTargetInvocationEncoder, DistributedTargetInvocationDecoder {
118118
typealias SerializationRequirement = Codable
119119

120+
var substitutions: [Any.Type] = []
120121
var arguments: [Any] = []
121122
var returnType: Any.Type? = nil
122123
var errorType: Any.Type? = nil
123124

124125
mutating func recordGenericSubstitution<T>(_ type: T.Type) throws {
125-
fatalError("NOT IMPLEMENTED: \(#function)")
126+
substitutions.append(type)
126127
}
127128
mutating func recordArgument<Argument: SerializationRequirement>(_ argument: Argument) throws {
128129
arguments.append(argument)
@@ -138,7 +139,7 @@ struct FakeInvocation: DistributedTargetInvocationEncoder, DistributedTargetInvo
138139
// === Receiving / decoding -------------------------------------------------
139140

140141
func decodeGenericSubstitutions() throws -> [Any.Type] {
141-
[]
142+
return substitutions
142143
}
143144

144145
var argumentIndex: Int = 0
@@ -156,7 +157,7 @@ struct FakeInvocation: DistributedTargetInvocationEncoder, DistributedTargetInvo
156157
}
157158

158159
print(" > decode argument: \(argument)")
159-
pointer.pointee = argument
160+
pointer.initialize(to: argument)
160161
argumentIndex += 1
161162
}
162163

@@ -190,8 +191,7 @@ let helloName = "$s4main7GreeterC5helloSSyFTE"
190191
let answerName = "$s4main7GreeterC6answerSiyFTE"
191192
let largeResultName = "$s4main7GreeterC11largeResultAA11LargeStructVyFTE"
192193
let enumResultName = "$s4main7GreeterC10enumResultAA1EOyFTE"
193-
194-
let echoName = "$s4main7GreeterC4echo4nameS2S_tFTE"
194+
let echoName = "$s4main7GreeterC4echo4name3ageS2S_SitFTE"
195195

196196
func test() async throws {
197197
let system = FakeActorSystem()
@@ -243,14 +243,15 @@ func test() async throws {
243243

244244
var echoInvocation = system.makeInvocationEncoder()
245245
try echoInvocation.recordArgument("Caplin")
246+
try echoInvocation.recordArgument(42)
246247
try echoInvocation.doneRecording()
247248
try await system.executeDistributedTarget(
248249
on: local,
249250
mangledTargetName: echoName,
250251
invocationDecoder: &echoInvocation,
251252
handler: FakeResultHandler()
252253
)
253-
// CHECK: RETURN: Echo: Caplin
254+
// CHECK: RETURN: Echo: name: Caplin, age: 42
254255

255256
print("done")
256257
// CHECK-NEXT: done

0 commit comments

Comments
 (0)