Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(406)

Unified Diff: mojo/dart/packages/mojom/lib/src/generate.dart

Issue 1970573003: Dart: Faster 'gen' command for mojom.dart tool (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/dart/packages/mojom/lib/src/generate.dart
diff --git a/mojo/dart/packages/mojom/lib/src/generate.dart b/mojo/dart/packages/mojom/lib/src/generate.dart
index 46fc0140aeb6e472a99eda7ec83326a3d4562ab6..331963a130455b217fdbbd86fd9cbeeae1504765 100644
--- a/mojo/dart/packages/mojom/lib/src/generate.dart
+++ b/mojo/dart/packages/mojom/lib/src/generate.dart
@@ -20,6 +20,55 @@ part 'mojom_finder.dart';
const String mojoTestPackage = '_mojo_for_test_only';
+// A class for running as many generation tasks in parallel as there are cores
+// on the machine. Supposing `tasks` is a list of functions of type
+// `Future f()`, used as:
+// var runner = new _GenerationTaskRunner(tasks);
+// await runner.run();
+// No result is returned.
+class _GenerationTaskRunner {
+ List<Function> _tasks;
+ List<Future> _futures;
+ Completer _completer;
+ int _numCpus;
+ int _nextTaskIndex;
+ int _futuresOutstanding;
+
+ _GenerationTaskRunner(this._tasks) {
+ _numCpus = Platform.numberOfProcessors;
+ _futures = new List<Future>();
+ _completer = new Completer();
+ _nextTaskIndex = 0;
+ _futuresOutstanding = 0;
+ }
+
+ Future run() {
+ while ((_nextTaskIndex < _numCpus) && (_nextTaskIndex < _tasks.length)) {
+ int idx = _nextTaskIndex;
+ _futures.add(_tasks[_nextTaskIndex]().then((_) {
+ _addNewTask(idx);
+ }));
+ _nextTaskIndex++;
+ _futuresOutstanding++;
+ }
+ return _completer.future;
+ }
+
+ void _addNewTask(int idx) {
+ if (_nextTaskIndex < _tasks.length) {
+ _futures[idx] = _tasks[_nextTaskIndex]().then((_) {
+ _addNewTask(idx);
+ });
+ _nextTaskIndex++;
+ } else {
+ _futuresOutstanding--;
+ if (_futuresOutstanding == 0) {
+ _completer.complete(null);
+ }
+ }
+ }
+}
+
class MojomGenerator {
static dev.Counter _genMs;
final bool _errorOnDuplicate;
@@ -71,10 +120,14 @@ class MojomGenerator {
// newer than the oldest .mojom.dart file, then regenerate.
if (_force || (mojomDartCount < mojomCount) ||
_shouldRegenerate(newestMojomTime, oldestMojomDartTime)) {
+ var tasks = new List();
for (File mojom in info.mojomFiles) {
- await _generateForMojom(
- mojom, info.importDir, info.packageDir, info.name);
+ tasks.add(() => _generateForMojom(
+ mojom, info.importDir, info.packageDir, info.name));
}
+ var runner = new _GenerationTaskRunner(tasks);
+ await runner.run();
+
// Delete any .mojom.dart files that are still older than mojomTime.
await _deleteOldMojomDart(info.packageDir, newestMojomTime);
}
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698