Files
sdk/tests/lib/isolate/function_send_test.dart
T
Martin Kustermann 430aa20a1f [vm/concurrency] Implement a fast transitive object copy for isolate message passing
We use message passing as comunication mechanism between isolates.
The transitive closure of an object to be sent is currently serialized
into a snapshot form and deserialized on the receiver side. Furthermore
the receiver side will re-hash any linked hashmaps in that graph.

If isolate gropus are enabled we have all isolates in a group work on
the same heap. That removes the need to use an intermediate
serialization format. It also removes the need for an O(n) step on the
receiver side.

This CL implements a fast transitive object copy implementation and
makes use of it a message that is to be passed to another isolate stays
within the same isolate group.

In the common case the object graph will fit into new space. So the
copy algorithm will try to take advantage of it by having a fast path
and a fallback path. Both of them effectively copy the graph in BFS
order.

The algorithm works effectively like a scavenge operation, but instead
of first copying the from-object to the to-space and then re-writing the
object in to-space to forward the pointers (which requires us writing to
the to-space memory twice), we only reserve space for to-objects and
then initialize the to-objects to it's final contents, including
forwarded pointers (i.e. write the to-space object only once).

Compared with a scavenge operation (which stores forwarding pointers in
the objects themselves), we use a [WeakTable] to store them. This is the
only remaining expensive part of the algorithm and could be further
optimized. To avoid relying on iterating the to-space, we'll remember
[from, to] addresses.

=> All of this works inside a [NoSafepointOperationScope] and avoids
   usages of handles as well as write barriers.

While doing the transitive object copy, we'll share any object we can
safely share (canonical objects, strings, sendports, ...) instead of
copying it.

If the fast path fails (due to allocation failure or hitting) we'll
handlify any raw pointers and continue almost the same algorithm in a
safe way, where GC is possible at every object allocation site and
normal barriers are used for any stores of object pointers.

The copy algorithm uses templates to share the copy logic between the
fast and slow case (same copy routines can work on raw pointers as well
as handles).

There's a few special things to take into consideration:

  * If we copy a view on external typed data we need to know the
    external typed data address to compute the inner pointer of the
    view, so we'll eagerly initialize external typed data.

  * All external typed data needs to get a finalizer attached
    (irrespective if the object copy suceeds or not) to ensure the
    `malloc()`ed data is freed again.

  * Transferables will only be transferred on successful transitive
    copies. Also they need to attach finalizers to objects (which
    requires all objects be in handles).

  * We copy linked hashmaps as they are - instead of compressing the
    data by removing deleted entries. We may need to re-hash those
    hashmaps on the receiver side (similar to the snapshot-based copy
    approach) since new object graph will have no identity hash codes
    assigned to them. Though if the hashmaps only has sharable objects
    as keys (very common, e.g. json) there is no need for re-hashing.


It changes the SendPort.* benchmarks as follows:

```
Benchmark                                     |              default |                        IG |                  IG + FOC
----------------------------------------------------------------------------------------------------------------------------
SendPort.Send.Nop(RunTimeRaw):                |        0.25 us (1 x) |       0.26 us    (0.96 x) |       0.25 us    (1.00 x)
SendPort.Send.Json.400B(RunTimeRaw):          |        4.15 us (1 x) |       1.45 us    (2.86 x) |       1.05 us    (3.95 x)
SendPort.Send.Json.5KB(RunTimeRaw):           |       82.16 us (1 x) |      27.17 us    (3.02 x) |      18.32 us    (4.48 x)
SendPort.Send.Json.50KB(RunTimeRaw):          |      784.70 us (1 x) |     242.10 us    (3.24 x) |     165.50 us    (4.74 x)
SendPort.Send.Json.500KB(RunTimeRaw):         |     8510.4  us (1 x) |    3083.80 us    (2.76 x) |    2311.29 us    (3.68 x)
SendPort.Send.Json.5MB(RunTimeRaw):           |   122381.33 us (1 x) |   62959.40 us    (1.94 x) |   55492.10 us    (2.21 x)
SendPort.Send.BinaryTree.2(RunTimeRaw):       |        1.91 us (1 x) |       0.92 us    (2.08 x) |       0.72 us    (2.65 x)
SendPort.Send.BinaryTree.4(RunTimeRaw):       |        6.32 us (1 x) |       2.70 us    (2.34 x) |       2.10 us    (3.01 x)
SendPort.Send.BinaryTree.6(RunTimeRaw):       |       25.24 us (1 x) |      10.47 us    (2.41 x) |       8.61 us    (2.93 x)
SendPort.Send.BinaryTree.8(RunTimeRaw):       |      104.08 us (1 x) |      41.08 us    (2.53 x) |      33.51 us    (3.11 x)
SendPort.Send.BinaryTree.10(RunTimeRaw):      |      373.39 us (1 x) |     174.11 us    (2.14 x) |     134.75 us    (2.77 x)
SendPort.Send.BinaryTree.12(RunTimeRaw):      |     1588.64 us (1 x) |     893.18 us    (1.78 x) |     532.05 us    (2.99 x)
SendPort.Send.BinaryTree.14(RunTimeRaw):      |     6849.55 us (1 x) |    3705.19 us    (1.85 x) |    2507.90 us    (2.73 x)
SendPort.Receive.Nop(RunTimeRaw):             |        0.67 us (1 x) |       0.69 us    (0.97 x) |       0.68 us    (0.99 x)
SendPort.Receive.Json.400B(RunTimeRaw):       |        4.37 us (1 x) |       0.78 us    (5.60 x) |       0.77 us    (5.68 x)
SendPort.Receive.Json.5KB(RunTimeRaw):        |       45.67 us (1 x) |       0.90 us   (50.74 x) |       0.87 us   (52.49 x)
SendPort.Receive.Json.50KB(RunTimeRaw):       |      498.81 us (1 x) |       1.24 us  (402.27 x) |       1.06 us  (470.58 x)
SendPort.Receive.Json.500KB(RunTimeRaw):      |     5366.02 us (1 x) |       4.22 us (1271.57 x) |       4.65 us (1153.98 x)
SendPort.Receive.Json.5MB(RunTimeRaw):        |   101050.88 us (1 x) |      20.81 us (4855.88 x) |      21.0  us (4811.95 x)
SendPort.Receive.BinaryTree.2(RunTimeRaw):    |        3.91 us (1 x) |       0.76 us    (5.14 x) |       0.74 us    (5.28 x)
SendPort.Receive.BinaryTree.4(RunTimeRaw):    |        9.90 us (1 x) |       0.79 us   (12.53 x) |       0.76 us   (13.03 x)
SendPort.Receive.BinaryTree.6(RunTimeRaw):    |       33.09 us (1 x) |       0.87 us   (38.03 x) |       0.84 us   (39.39 x)
SendPort.Receive.BinaryTree.8(RunTimeRaw):    |      126.77 us (1 x) |       0.92 us  (137.79 x) |       0.88 us  (144.06 x)
SendPort.Receive.BinaryTree.10(RunTimeRaw):   |      533.09 us (1 x) |       0.94 us  (567.12 x) |       0.92 us  (579.45 x)
SendPort.Receive.BinaryTree.12(RunTimeRaw):   |     2223.23 us (1 x) |       3.03 us  (733.74 x) |       3.04 us  (731.33 x)
SendPort.Receive.BinaryTree.14(RunTimeRaw):   |     8945.66 us (1 x) |       4.03 us (2219.77 x) |       4.30 us (2080.39 x)
```

Issue https://github.com/dart-lang/sdk/issues/36097

TEST=vm/dart{,_2}/isolates/fast_object_copy{,2}_test

Change-Id: I835c59dab573d365b8a4b9d7c5359a6ea8d8b0a7
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/203776
Commit-Queue: Martin Kustermann <kustermann@google.com>
Reviewed-by: Ryan Macnak <rmacnak@google.com>
Reviewed-by: Slava Egorov <vegorov@google.com>
Reviewed-by: Alexander Aprelev <aam@google.com>
2021-07-13 19:04:20 +00:00

224 lines
6.5 KiB
Dart

// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// VMOptions=--enable-isolate-groups --experimental-enable-isolate-groups-jit --no-enable-fast-object-copy
// VMOptions=--enable-isolate-groups --experimental-enable-isolate-groups-jit --enable-fast-object-copy
// VMOptions=--no-enable-isolate-groups
import "dart:isolate";
import "dart:async";
import "package:expect/expect.dart";
import "package:async_helper/async_helper.dart";
void toplevel(port, message) {
port.send("toplevel:$message");
}
Function createFuncToplevel() => (p, m) {
p.send(m);
};
class C {
Function initializer;
Function body;
C()
: initializer = ((p, m) {
throw "initializer";
}),
body = ((p, m) {
throw "body";
}) {}
static void staticFunc(port, message) {
port.send("static:$message");
}
static Function createFuncStatic() => (p, m) {
throw "static expr";
};
void instanceMethod(p, m) {
throw "instanceMethod";
}
Function createFuncMember() => (p, m) {
throw "instance expr";
};
void call(n, p) {
throw "C";
}
}
class Callable {
void call(p, m) {
p.send(["callable", m]);
}
}
void main() {
asyncStart();
// Sendables are top-level functions and static functions only.
testSendable("toplevel", toplevel);
testSendable("static", C.staticFunc);
// Unsendables are any closure - instance methods or function expression.
var c = new C();
testUnsendable("instance method", c.instanceMethod);
testUnsendable("static context expression", createFuncToplevel());
testUnsendable("static context expression", C.createFuncStatic());
testUnsendable("initializer context expression", c.initializer);
testUnsendable("constructor context expression", c.body);
testUnsendable("instance method context expression", c.createFuncMember());
// The result of `toplevel.call` and `staticFunc.call` may or may not be
// identical to `toplevel` and `staticFunc` respectively. If they are not
// equal, they may or may not be considered toplevel/static functions anyway,
// and therefore sendable. The VM and dart2js currently disagree on whether
// `toplevel` and `toplevel.call` are identical, both allow them to be sent.
// If this is ever specified to something else, use:
// testUnsendable("toplevel.call", toplevel.call);
// testUnsendable("static.call", C.staticFunc.call);
// instead.
// These two tests should be considered canaries for accidental behavior
// change rather than requirements.
testSendable("toplevel", toplevel.call);
testSendable("static", C.staticFunc.call);
// Callable objects are sendable if general objects are (VM yes, dart2js no).
// It's unspecified whether arbitrary objects can be sent. If it is specified,
// add a test that `new Callable()` is either sendable or unsendable.
// The call method of a callable object is a closure holding the object,
// not a top-level or static function, so it should be blocked, just as
// a normal method.
testUnsendable("callable object", new Callable().call);
asyncEnd();
return;
}
// Create a receive port that expects exactly one message.
// Pass the message to `callback` and return the sendPort.
SendPort singleMessagePort(callback) {
var p;
p = new RawReceivePort((v) {
p.close();
callback(v);
});
return p.sendPort;
}
// A singleMessagePort that expects the message to be a specific value.
SendPort expectMessagePort(message) {
asyncStart();
return singleMessagePort((v) {
Expect.equals(message, v);
asyncEnd();
});
}
void testSendable(name, func) {
// Function as spawn message.
Isolate.spawn(callFunc, [func, expectMessagePort("$name:spawn"), "spawn"]);
// Send function to same isolate.
var reply = expectMessagePort("$name:direct");
singleMessagePort(callFunc).send([func, reply, "direct"]);
// Send function to other isolate, call it there.
reply = expectMessagePort("$name:other isolate");
callPort().then((p) {
p.send([func, reply, "other isolate"]);
});
// Round-trip function trough other isolate.
echoPort((roundtripFunc) {
Expect.identical(func, roundtripFunc, "$name:send through isolate");
}).then((port) {
port.send(func);
});
}
// Creates a new isolate and a pair of ports that expect a single message
// to be sent to the other isolate and back to the callback function.
Future<SendPort> echoPort(callback(value)) {
final completer = new Completer<SendPort>();
SendPort replyPort = singleMessagePort(callback);
late RawReceivePort initPort;
initPort = new RawReceivePort((p) {
completer.complete(p);
initPort.close();
});
return Isolate.spawn(_echo, [replyPort, initPort.sendPort])
.then((isolate) => completer.future);
}
void _echo(msg) {
var replyPort = msg[0];
late RawReceivePort requestPort;
requestPort = new RawReceivePort((msg) {
replyPort.send(msg);
requestPort.close(); // Single echo only.
});
msg[1].send(requestPort.sendPort);
}
// Creates other isolate that waits for a single message, `msg`, on the returned
// port, and executes it as `msg[0](msg[1],msg[2])` in the other isolate.
Future<SendPort> callPort() {
final completer = new Completer<SendPort>();
SendPort initPort = singleMessagePort(completer.complete);
return Isolate.spawn(_call, initPort).then((_) => completer.future);
}
void _call(initPort) {
initPort.send(singleMessagePort(callFunc));
}
void testUnsendable(name, func) {
asyncStart();
Isolate.spawn(nop, func).then<void>((v) => throw "allowed spawn direct?",
onError: (e, s) {
asyncEnd();
});
asyncStart();
Isolate.spawn(nop, [func]).then<void>((v) => throw "allowed spawn wrapped?",
onError: (e, s) {
asyncEnd();
});
asyncStart();
var noReply = new RawReceivePort((_) {
throw "Unexpected message: $_";
});
Expect.throws(() {
noReply.sendPort.send(func);
}, (_) => true, "send direct");
Expect.throws(() {
noReply.sendPort.send([func]);
}, (_) => true, "send wrapped");
scheduleMicrotask(() {
noReply.close();
asyncEnd();
});
// Try sending through other isolate.
asyncStart();
echoPort((v) {
Expect.equals(0, v);
}).then((p) {
try {
p.send(func);
} finally {
p.send(0); // Closes echo port.
}
}).then<void>((p) => throw "unreachable 2", onError: (e, s) {
asyncEnd();
});
}
void nop(_) {}
void callFunc(message) {
message[0](message[1], message[2]);
}