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

Side by Side Diff: runtime/bin/builtin.dart

Issue 1154173006: - Avoid using a streamed receive port for single messages. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Address review comments. Created 5 years, 6 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 unified diff | Download patch
« no previous file with comments | « no previous file | runtime/bin/vmservice/loader.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library builtin; 5 library builtin;
6 // NOTE: Do not import 'dart:io' in builtin. 6 // NOTE: Do not import 'dart:io' in builtin.
7 import 'dart:collection';
7 import 'dart:isolate'; 8 import 'dart:isolate';
8 import 'dart:typed_data'; 9 import 'dart:typed_data';
9 10
10 // The root library (aka the script) is imported into this library. The 11 // The root library (aka the script) is imported into this library. The
11 // standalone embedder uses this to lookup the main entrypoint in the 12 // standalone embedder uses this to lookup the main entrypoint in the
12 // root library's namespace. 13 // root library's namespace.
13 Function _getMainClosure() => main; 14 Function _getMainClosure() => main;
14 15
15 16
16 // 'print' implementation. 17 // 'print' implementation.
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 const Dart_kImportTag = 0; 53 const Dart_kImportTag = 0;
53 const Dart_kSourceTag = 1; 54 const Dart_kSourceTag = 1;
54 const Dart_kCanonicalizeUrl = 2; 55 const Dart_kCanonicalizeUrl = 2;
55 56
56 // Embedder sets this to true if the --trace-loading flag was passed on the 57 // Embedder sets this to true if the --trace-loading flag was passed on the
57 // command line. 58 // command line.
58 bool _traceLoading = false; 59 bool _traceLoading = false;
59 60
60 // A port for communicating with the service isolate for I/O. 61 // A port for communicating with the service isolate for I/O.
61 SendPort _loadPort; 62 SendPort _loadPort;
62 // Maintain a number of outstanding load requests. Current loading request is 63 // The receive port for a load request. Multiple sources can be fetched in
63 // finished once there are no outstanding requests. 64 // a single load request.
64 int _numOutstandingLoadRequests = 0; 65 RawReceivePort _receivePort;
66 SendPort _sendPort;
67 // A request id valid only for the current load cycle (while the number of
68 // outstanding load requests is greater than 0). Can be reset when loading is
69 // completed.
70 int _reqId = 0;
71 // An unordered hash map mapping from request id to a particular load request.
72 // Once there are no outstanding load requests the current load has finished.
73 HashMap _reqMap = new HashMap();
65 74
66 // The current working directory when the embedder was launched. 75 // The current working directory when the embedder was launched.
67 Uri _workingDirectory; 76 Uri _workingDirectory;
68 // The URI that the root script was loaded from. Remembered so that 77 // The URI that the root script was loaded from. Remembered so that
69 // package imports can be resolved relative to it. The root script is the basis 78 // package imports can be resolved relative to it. The root script is the basis
70 // for the root library in the VM. 79 // for the root library in the VM.
71 Uri _rootScript; 80 Uri _rootScript;
72 // The directory to look in to resolve "package:" scheme URIs. By detault it is 81 // The directory to look in to resolve "package:" scheme URIs. By detault it is
73 // the 'packages' directory right next to the script. 82 // the 'packages' directory right next to the script.
74 Uri _packageRoot = _rootScript.resolve('packages/'); 83 Uri _packageRoot = _rootScript.resolve('packages/');
75 84
76 // Special handling for Windows paths so that they are compatible with URI 85 // Special handling for Windows paths so that they are compatible with URI
77 // handling. 86 // handling.
78 // Embedder sets this to true if we are running on Windows. 87 // Embedder sets this to true if we are running on Windows.
79 bool _isWindows = false; 88 bool _isWindows = false;
80 89
81 90
82 // A class wrapping the load error message in an Error object. 91 // A class wrapping the load error message in an Error object.
83 class LoadError extends Error { 92 class _LoadError extends Error {
84 final String message; 93 final String message;
85 LoadError(this.message); 94 _LoadError(this.message);
86 95
87 String toString() => 'Load Error: $message'; 96 String toString() => 'Load Error: $message';
88 } 97 }
89 98
99 // Class collecting all of the information about a particular load request.
100 class _LoadRequest {
101 final int _id;
102 final int _tag;
103 final String _uri;
104 final String _libraryUri;
105
106 _LoadRequest(this._id, this._tag, this._uri, this._libraryUri);
107 }
108
90 109
91 // Native calls provided by the embedder. 110 // Native calls provided by the embedder.
92 void _signalDoneLoading() native "Builtin_DoneLoading"; 111 void _signalDoneLoading() native "Builtin_DoneLoading";
93 void _loadScriptCallback(int tag, String uri, String libraryUri, Uint8List data) 112 void _loadScriptCallback(int tag, String uri, String libraryUri, Uint8List data)
94 native "Builtin_LoadSource"; 113 native "Builtin_LoadSource";
95 void _asyncLoadErrorCallback(uri, libraryUri, error) 114 void _asyncLoadErrorCallback(uri, libraryUri, error)
96 native "Builtin_AsyncLoadError"; 115 native "Builtin_AsyncLoadError";
97 116
98 117
99 _sanitizeWindowsPath(path) { 118 _sanitizeWindowsPath(path) {
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 // this location. 241 // this location.
223 _rootScript = scriptUri; 242 _rootScript = scriptUri;
224 243
225 if (_traceLoading) { 244 if (_traceLoading) {
226 _print('# Resolved entry point to: $_rootScript'); 245 _print('# Resolved entry point to: $_rootScript');
227 } 246 }
228 return scriptUri; 247 return scriptUri;
229 } 248 }
230 249
231 250
232 void _finishLoadRequest(String uri) { 251 void _finishLoadRequest(_LoadRequest req) {
233 assert(_numOutstandingLoadRequests > 0); 252 // Now that we are done with loading remove the request from the map.
234 _numOutstandingLoadRequests--; 253 var tmp = _reqMap.remove(req._id);
254 assert(tmp == req);
235 if (_traceLoading) { 255 if (_traceLoading) {
236 _print("Loading of $uri finished, " 256 _print("Loading of ${req._uri} finished, "
237 "${_numOutstandingLoadRequests} requests remaining"); 257 "${_reqMap.length} requests remaining");
238 } 258 }
239 if (_numOutstandingLoadRequests == 0) { 259
260 if (_reqMap.isEmpty) {
261 if (_traceLoading) {
262 _print("Closing loading port.");
263 }
264 _receivePort.close();
265 _receivePort = null;
266 _sendPort = null;
267 _reqId = 0;
240 _signalDoneLoading(); 268 _signalDoneLoading();
241 } 269 }
242 } 270 }
243 271
244 272
245 void _startLoadRequest(String uri, Uri resourceUri) { 273 void _handleLoaderReply(msg) {
246 assert(_numOutstandingLoadRequests >= 0); 274 int id = msg[0];
247 _numOutstandingLoadRequests++; 275 var dataOrError = msg[1];
248 if (_traceLoading) { 276 assert((id >= 0) && (id < _reqId));
249 _print("Loading of $resourceUri for $uri started, " 277 var req = _reqMap[id];
250 "${_numOutstandingLoadRequests} requests outstanding"); 278 try {
279 if (dataOrError is Uint8List) {
280 _loadScript(req, dataOrError);
281 } else {
282 assert(dataOrError is String);
283 var error = new _LoadError(dataOrError.toString());
284 _asyncLoadError(req, error);
285 }
286 } catch(e, s) {
287 // Wrap inside a _LoadError unless we are already propagating a
288 // previous _LoadError.
289 var error = (e is _LoadError) ? e : new _LoadError(e.toString());
290 assert(req != null);
291 _asyncLoadError(req, error);
251 } 292 }
252 } 293 }
253 294
254 295
255 void _loadScript(int tag, String uri, String libraryUri, Uint8List data) { 296 void _startLoadRequest(int tag,
256 // TODO: Currently a compilation error while loading the script is 297 String uri,
257 // fatal for the isolate. _loadScriptCallback() does not return and 298 String libraryUri,
258 // the _numOutstandingLoadRequests counter remains out of sync. 299 Uri resourceUri) {
259 _loadScriptCallback(tag, uri, libraryUri, data); 300 if (_reqMap.isEmpty) {
260 _finishLoadRequest(uri); 301 if (_traceLoading) {
302 _print("Initializing load port.");
303 }
304 assert(_receivePort == null);
305 assert(_sendPort == null);
306 _receivePort = new RawReceivePort(_handleLoaderReply);
307 _sendPort = _receivePort.sendPort;
308 }
309 // Register the load request and send it to the VM service isolate.
310 var curId = _reqId++;
311
312 assert(_reqMap[curId] == null);
313 _reqMap[curId] = new _LoadRequest(curId, tag, uri, libraryUri);
314
315 var msg = new List(3);
316 msg[0] = _sendPort;
317 msg[1] = curId;
318 msg[2] = resourceUri.toString();
319 _loadPort.send(msg);
320
321 if (_traceLoading) {
322 _print("Loading of $resourceUri for $uri started with id: $curId, "
323 "${_reqMap.length} requests outstanding");
324 }
261 } 325 }
262 326
263 327
264 void _asyncLoadError(int tag, String uri, String libraryUri, LoadError error) { 328 void _loadScript(_LoadRequest req, Uint8List data) {
265 if (_traceLoading) { 329 // TODO: Currently a compilation error while loading the script is
266 _print("_asyncLoadError($uri), error: $error"); 330 // fatal for the isolate. _loadScriptCallback() does not return and
267 } 331 // the number of requests remains out of sync.
268 if (tag == Dart_kImportTag) { 332 _loadScriptCallback(req._tag, req._uri, req._libraryUri, data);
269 // When importing a library, the libraryUri is the imported 333 _finishLoadRequest(req);
270 // uri.
271 libraryUri = uri;
272 }
273 _asyncLoadErrorCallback(uri, libraryUri, error);
274 _finishLoadRequest(uri);
275 } 334 }
276 335
277 336
337 void _asyncLoadError(_LoadRequest req, _LoadError error) {
338 if (_traceLoading) {
339 _print("_asyncLoadError(${req._uri}), error: $error");
340 }
341 if (req._tag == Dart_kImportTag) {
342 // When importing a library, the libraryUri is the imported
343 // uri.
344 req._libraryUri = req._uri;
345 }
346 _asyncLoadErrorCallback(req._uri, req._libraryUri, error);
347 _finishLoadRequest(req);
348 }
349
350
278 _loadDataFromLoadPort(int tag, 351 _loadDataFromLoadPort(int tag,
279 String uri, 352 String uri,
280 String libraryUri, 353 String libraryUri,
281 Uri resourceUri) { 354 Uri resourceUri) {
282 var receivePort = new ReceivePort();
283 receivePort.first.then((dataOrError) {
284 receivePort.close();
285 if (dataOrError is Uint8List) {
286 _loadScript(tag, uri, libraryUri, dataOrError);
287 } else {
288 assert(dataOrError is String);
289 var error = new LoadError(dataOrError.toString());
290 _asyncLoadError(tag, uri, libraryUri, error);
291 }
292 }).catchError((e) {
293 receivePort.close();
294 // Wrap inside a LoadError unless we are already propagating a previously
295 // seen LoadError.
296 var error = (e is LoadError) ? e : new LoadError(e.toString);
297 _asyncLoadError(tag, uri, libraryUri, error);
298 });
299
300 try { 355 try {
301 var msg = [receivePort.sendPort, resourceUri.toString()]; 356 _startLoadRequest(tag, uri, libraryUri, resourceUri);
302 _loadPort.send(msg);
303 _startLoadRequest(uri, resourceUri);
304 } catch (e) { 357 } catch (e) {
305 if (_traceLoading) { 358 if (_traceLoading) {
306 _print("Exception when communicating with service isolate: $e"); 359 _print("Exception when communicating with service isolate: $e");
307 } 360 }
308 // Wrap inside a LoadError unless we are already propagating a previously 361 // Wrap inside a _LoadError unless we are already propagating a previously
309 // seen LoadError. 362 // seen _LoadError.
310 var error = (e is LoadError) ? e : new LoadError(e.toString); 363 var error = (e is _LoadError) ? e : new _LoadError(e.toString());
311 _asyncLoadError(tag, uri, libraryUri, error); 364 _asyncLoadError(tag, uri, libraryUri, error);
312 receivePort.close();
313 } 365 }
314 } 366 }
315 367
316 368
317 // Embedder Entrypoint: 369 // Embedder Entrypoint:
318 // Asynchronously loads script data through a http[s] or file uri. 370 // Asynchronously loads script data through a http[s] or file uri.
319 _loadDataAsync(int tag, String uri, String libraryUri) { 371 _loadDataAsync(int tag, String uri, String libraryUri) {
320 var resourceUri; 372 var resourceUri;
321 if (tag == Dart_kScriptTag) { 373 if (tag == Dart_kScriptTag) {
322 resourceUri = _resolveScriptUri(uri); 374 resourceUri = _resolveScriptUri(uri);
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
453 } else { 505 } else {
454 name = userUri.substring(index + 1); 506 name = userUri.substring(index + 1);
455 path = userUri.substring(0, index + 1); 507 path = userUri.substring(0, index + 1);
456 } 508 }
457 509
458 path = _filePathFromUri(path); 510 path = _filePathFromUri(path);
459 var filename = _platformExtensionFileName(name); 511 var filename = _platformExtensionFileName(name);
460 512
461 return [path, filename, name]; 513 return [path, filename, name];
462 } 514 }
OLDNEW
« no previous file with comments | « no previous file | runtime/bin/vmservice/loader.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698