Skip to content

Commit f0b83bf

Browse files
lrhnCommit Queue
authored andcommitted
Move VM-specific logic out of schedule_microtask.dart.
Adds trivial hooks that other platforms can inline for no extra overhead, and the VM can specialize to do whatever extra checking they need. Avoids adding unnecessary code to other platforms. Tested: Refactoringm covered by existing test. Change-Id: I3816f27548fd883fd98eba0428be8d58e27da12e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/437481 Reviewed-by: Martin Kustermann <[email protected]> Commit-Queue: Lasse Nielsen <[email protected]> Reviewed-by: Stephen Adams <[email protected]> Reviewed-by: Slava Egorov <[email protected]>
1 parent f4e7c3c commit f0b83bf

File tree

2 files changed

+62
-40
lines changed

2 files changed

+62
-40
lines changed

sdk/lib/_internal/vm/lib/schedule_microtask_patch.dart

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,65 @@
44

55
part of "async_patch.dart";
66

7-
@patch
7+
@pragma("vm:entry-point", !const bool.fromEnvironment("dart.vm.product"))
88
abstract final class _MicrotaskMirrorQueue {
9-
@patch
9+
// This will be set to true by the native runtime's
10+
// `DartUtils::PrepareAsyncLibrary` when the CLI flag `--profile-microtasks`
11+
// is set.
12+
@pragma("vm:entry-point", !const bool.fromEnvironment("dart.vm.product"))
13+
static bool _shouldProfileMicrotasks = false;
14+
1015
@pragma("vm:external-name", "MicrotaskMirrorQueue_onScheduleAsyncCallback")
1116
external static void _onScheduleAsyncCallback();
1217

13-
@patch
1418
@pragma(
1519
"vm:external-name",
1620
"MicrotaskMirrorQueue_onSchedulePriorityAsyncCallback",
1721
)
1822
external static void _onSchedulePriorityAsyncCallback();
1923

20-
@patch
2124
@pragma("vm:external-name", "MicrotaskMirrorQueue_onAsyncCallbackComplete")
2225
external static void _onAsyncCallbackComplete(int startTime, int endTime);
2326
}
2427

28+
@patch
29+
void _beforeScheduleMicrotaskCallback() {
30+
if (!const bool.fromEnvironment("dart.vm.product") &&
31+
_MicrotaskMirrorQueue._shouldProfileMicrotasks) {
32+
_MicrotaskMirrorQueue._onScheduleAsyncCallback();
33+
}
34+
}
35+
36+
@patch
37+
void _beforeSchedulePriorityCallback() {
38+
if (!const bool.fromEnvironment("dart.vm.product") &&
39+
_MicrotaskMirrorQueue._shouldProfileMicrotasks) {
40+
_MicrotaskMirrorQueue._onSchedulePriorityAsyncCallback();
41+
}
42+
}
43+
44+
@patch
45+
void Function() _microtaskEntryCallback(_AsyncCallbackEntry entry) {
46+
if (const bool.fromEnvironment("dart.vm.product") ||
47+
!_MicrotaskMirrorQueue._shouldProfileMicrotasks) {
48+
return entry.callback;
49+
} else {
50+
@pragma("vm:invisible")
51+
void timedCallback() {
52+
final callbackStartTime = Timeline.now;
53+
(entry.callback)();
54+
final callbackEndTime = Timeline.now;
55+
_MicrotaskMirrorQueue._onAsyncCallbackComplete(
56+
callbackStartTime,
57+
callbackEndTime,
58+
);
59+
}
60+
61+
;
62+
return timedCallback;
63+
}
64+
}
65+
2566
@patch
2667
class _AsyncRun {
2768
@patch

sdk/lib/async/schedule_microtask.dart

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,7 @@ void _microtaskLoop() {
3737
var next = entry.next;
3838
_nextCallback = next;
3939
if (next == null) _lastCallback = null;
40-
if (const bool.fromEnvironment("dart.vm.product") ||
41-
!_MicrotaskMirrorQueue._shouldProfileMicrotasks) {
42-
(entry.callback)();
43-
} else {
44-
final callbackStartTime = Timeline.now;
45-
(entry.callback)();
46-
final callbackEndTime = Timeline.now;
47-
_MicrotaskMirrorQueue._onAsyncCallbackComplete(
48-
callbackStartTime,
49-
callbackEndTime,
50-
);
51-
}
40+
_microtaskEntryCallback(entry)();
5241
}
5342
}
5443

@@ -73,10 +62,7 @@ void _startMicrotaskLoop() {
7362
/// microtasks, but as part of the current system event.
7463
void _scheduleAsyncCallback(_AsyncCallback callback) {
7564
_AsyncCallbackEntry newEntry = _AsyncCallbackEntry(callback);
76-
if (!const bool.fromEnvironment("dart.vm.product") &&
77-
_MicrotaskMirrorQueue._shouldProfileMicrotasks) {
78-
_MicrotaskMirrorQueue._onScheduleAsyncCallback();
79-
}
65+
_beforeScheduleMicrotaskCallback();
8066
_AsyncCallbackEntry? lastCallback = _lastCallback;
8167
if (lastCallback == null) {
8268
_nextCallback = _lastCallback = newEntry;
@@ -102,10 +88,7 @@ void _schedulePriorityAsyncCallback(_AsyncCallback callback) {
10288
return;
10389
}
10490
_AsyncCallbackEntry entry = _AsyncCallbackEntry(callback);
105-
if (!const bool.fromEnvironment("dart.vm.product") &&
106-
_MicrotaskMirrorQueue._shouldProfileMicrotasks) {
107-
_MicrotaskMirrorQueue._onSchedulePriorityAsyncCallback();
108-
}
91+
_beforeSchedulePriorityCallback();
10992
_AsyncCallbackEntry? lastPriorityCallback = _lastPriorityCallback;
11093
if (lastPriorityCallback == null) {
11194
entry.next = _nextCallback;
@@ -121,6 +104,20 @@ void _schedulePriorityAsyncCallback(_AsyncCallback callback) {
121104
}
122105
}
123106

107+
// Overridden by VM.
108+
@pragma("dart2js:prefer-inline")
109+
@pragma("dart2wasm:prefer-inline")
110+
void _beforeSchedulePriorityCallback() {}
111+
112+
@pragma("dart2js:prefer-inline")
113+
@pragma("dart2wasm:prefer-inline")
114+
void _beforeScheduleMicrotaskCallback() {}
115+
116+
@pragma("dart2js:prefer-inline")
117+
@pragma("dart2wasm:prefer-inline")
118+
void Function() _microtaskEntryCallback(_AsyncCallbackEntry entry) =>
119+
entry.callback;
120+
124121
/// Runs a function asynchronously.
125122
///
126123
/// Callbacks registered through this function are always executed in order and
@@ -167,22 +164,6 @@ void scheduleMicrotask(void Function() callback) {
167164
Zone.current.scheduleMicrotask(Zone.current.bindCallbackGuarded(callback));
168165
}
169166

170-
@pragma("vm:entry-point", !const bool.fromEnvironment("dart.vm.product"))
171-
abstract final class _MicrotaskMirrorQueue {
172-
// This will be set to true by the native runtime's
173-
// `DartUtils::PrepareAsyncLibrary` when the CLI flag `--profile-microtasks`
174-
// is set.
175-
@pragma("vm:entry-point", !const bool.fromEnvironment("dart.vm.product"))
176-
static bool _shouldProfileMicrotasks = false;
177-
178-
// The following methods are only implemented for the native runtime. Those
179-
// implementations are in
180-
// `sdk/lib/_internal/vm/lib/schedule_microtask_patch.dart`.
181-
static void _onScheduleAsyncCallback() {}
182-
static void _onSchedulePriorityAsyncCallback() {}
183-
static void _onAsyncCallbackComplete(int startTime, int endTime) {}
184-
}
185-
186167
class _AsyncRun {
187168
/// Schedule the given callback before any other event in the event-loop.
188169
external static void _scheduleImmediate(void Function() callback);

0 commit comments

Comments
 (0)