OLD | NEW |
---|---|
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 import 'dart:async'; | 5 import 'dart:async'; |
6 import 'dart:io'; | 6 import 'dart:io'; |
7 import 'dart:typed_data'; | |
7 | 8 |
8 import 'package:front_end/file_system.dart'; | 9 import 'package:front_end/file_system.dart'; |
9 import 'package:front_end/incremental_kernel_generator.dart'; | 10 import 'package:front_end/incremental_kernel_generator.dart'; |
10 import 'package:front_end/incremental_resolved_ast_generator.dart'; | 11 import 'package:front_end/incremental_resolved_ast_generator.dart'; |
11 import 'package:front_end/src/base/api_signature.dart'; | 12 import 'package:front_end/src/base/api_signature.dart'; |
12 import 'package:front_end/src/base/performace_logger.dart'; | 13 import 'package:front_end/src/base/performace_logger.dart'; |
13 import 'package:front_end/src/base/processed_options.dart'; | 14 import 'package:front_end/src/base/processed_options.dart'; |
14 import 'package:front_end/src/fasta/dill/dill_library_builder.dart'; | 15 import 'package:front_end/src/fasta/dill/dill_library_builder.dart'; |
15 import 'package:front_end/src/fasta/dill/dill_target.dart'; | 16 import 'package:front_end/src/fasta/dill/dill_target.dart'; |
16 import 'package:front_end/src/fasta/kernel/kernel_target.dart'; | 17 import 'package:front_end/src/fasta/kernel/kernel_target.dart'; |
(...skipping 21 matching lines...) Expand all Loading... | |
38 } | 39 } |
39 | 40 |
40 /// Implementation of [IncrementalKernelGenerator]. | 41 /// Implementation of [IncrementalKernelGenerator]. |
41 /// | 42 /// |
42 /// TODO(scheglov) Update the documentation. | 43 /// TODO(scheglov) Update the documentation. |
43 /// | 44 /// |
44 /// Theory of operation: an instance of [IncrementalResolvedAstGenerator] is | 45 /// Theory of operation: an instance of [IncrementalResolvedAstGenerator] is |
45 /// used to obtain resolved ASTs, and these are fed into kernel code generation | 46 /// used to obtain resolved ASTs, and these are fed into kernel code generation |
46 /// logic. | 47 /// logic. |
47 class IncrementalKernelGeneratorImpl implements IncrementalKernelGenerator { | 48 class IncrementalKernelGeneratorImpl implements IncrementalKernelGenerator { |
49 /// The version of data format, should be incremented on every format change. | |
50 static const int DATA_VERSION = 1; | |
51 | |
48 /// The compiler options, such as the [FileSystem], the SDK dill location, | 52 /// The compiler options, such as the [FileSystem], the SDK dill location, |
49 /// etc. | 53 /// etc. |
50 final ProcessedOptions _options; | 54 final ProcessedOptions _options; |
51 | 55 |
52 /// The object that knows how to resolve "package:" and "dart:" URIs. | 56 /// The object that knows how to resolve "package:" and "dart:" URIs. |
53 final TranslateUri _uriTranslator; | 57 final TranslateUri _uriTranslator; |
54 | 58 |
55 /// The logger to report compilation progress. | 59 /// The logger to report compilation progress. |
56 final PerformanceLog _logger; | 60 final PerformanceLog _logger; |
57 | 61 |
58 /// The current file system state. | 62 /// The current file system state. |
59 final FileSystemState _fsState; | 63 final FileSystemState _fsState; |
60 | 64 |
61 /// The byte storage to get and put serialized data. | 65 /// The byte storage to get and put serialized data. |
62 final ByteStore _byteStore; | 66 final ByteStore _byteStore; |
63 | 67 |
64 /// The URI of the program entry point. | 68 /// The URI of the program entry point. |
65 final Uri _entryPoint; | 69 final Uri _entryPoint; |
66 | 70 |
71 /// The salt to mix into all hashes used as keys for serialized data. | |
72 final Uint8List _salt = new Uint8List(16); | |
73 | |
67 /// Latest compilation signatures produced by [computeDelta] for libraries. | 74 /// Latest compilation signatures produced by [computeDelta] for libraries. |
68 final Map<Uri, String> _uriToLatestSignature = {}; | 75 final Map<Uri, String> _latestSignature = {}; |
69 | 76 |
70 /// The set of absolute file URIs that were reported through [invalidate] | 77 /// The set of absolute file URIs that were reported through [invalidate] |
71 /// and not checked for actual changes yet. | 78 /// and not checked for actual changes yet. |
72 final Set<Uri> _invalidatedFiles = new Set<Uri>(); | 79 final Set<Uri> _invalidatedFiles = new Set<Uri>(); |
73 | 80 |
74 IncrementalKernelGeneratorImpl( | 81 IncrementalKernelGeneratorImpl( |
75 this._options, this._uriTranslator, this._entryPoint) | 82 this._options, this._uriTranslator, this._entryPoint) |
76 : _logger = _options.logger, | 83 : _logger = _options.logger, |
77 _fsState = new FileSystemState(_options.fileSystem, _uriTranslator), | 84 _fsState = new FileSystemState(_options.fileSystem, _uriTranslator), |
78 _byteStore = _options.byteStore; | 85 _byteStore = _options.byteStore { |
86 _fillSalt(); | |
87 } | |
79 | 88 |
80 @override | 89 @override |
81 Future<DeltaProgram> computeDelta( | 90 Future<DeltaProgram> computeDelta( |
82 {Future<Null> watch(Uri uri, bool used)}) async { | 91 {Future<Null> watch(Uri uri, bool used)}) async { |
83 return await _logger.runAsync('Compute delta', () async { | 92 return await _logger.runAsync('Compute delta', () async { |
84 await _refreshInvalidatedFiles(); | 93 await _refreshInvalidatedFiles(); |
85 | 94 |
86 // Ensure that the graph starting at the entry point is ready. | 95 // Ensure that the graph starting at the entry point is ready. |
87 FileState entryLibrary = await _fsState.getFile(_entryPoint); | 96 FileState entryLibrary = await _fsState.getFile(_entryPoint); |
88 | 97 |
(...skipping 15 matching lines...) Expand all Loading... | |
104 results.add(result); | 113 results.add(result); |
105 } | 114 } |
106 }); | 115 }); |
107 | 116 |
108 Program program = new Program(nameRoot: nameRoot); | 117 Program program = new Program(nameRoot: nameRoot); |
109 | 118 |
110 // Add affected libraries (with different signatures). | 119 // Add affected libraries (with different signatures). |
111 for (_LibraryCycleResult result in results) { | 120 for (_LibraryCycleResult result in results) { |
112 for (Library library in result.kernelLibraries) { | 121 for (Library library in result.kernelLibraries) { |
113 Uri uri = library.importUri; | 122 Uri uri = library.importUri; |
114 if (_uriToLatestSignature[uri] != result.signature) { | 123 if (_latestSignature[uri] != result.signature) { |
115 _uriToLatestSignature[uri] = result.signature; | 124 _latestSignature[uri] = result.signature; |
116 program.libraries.add(library); | 125 program.libraries.add(library); |
117 } | 126 } |
118 } | 127 } |
119 } | 128 } |
120 | 129 |
121 // TODO(scheglov) Add libraries which import changed libraries. | 130 // TODO(scheglov) Add libraries which import changed libraries. |
122 | 131 |
123 return new DeltaProgram(program); | 132 return new DeltaProgram(program); |
124 }); | 133 }); |
125 } | 134 } |
126 | 135 |
127 @override | 136 @override |
128 void invalidate(Uri uri) { | 137 void invalidate(Uri uri) { |
129 _invalidatedFiles.add(uri); | 138 _invalidatedFiles.add(uri); |
130 } | 139 } |
131 | 140 |
132 @override | 141 @override |
133 void invalidateAll() => unimplemented(); | 142 void invalidateAll() => unimplemented(); |
134 | 143 |
135 /// Ensure that [dillTarget] includes the [cycle] libraries. It already | 144 /// Ensure that [dillTarget] includes the [cycle] libraries. It already |
136 /// contains all the libraries that sorted before the given [cycle] in | 145 /// contains all the libraries that sorted before the given [cycle] in |
137 /// topological order. Return the result with the cycle libraries. | 146 /// topological order. Return the result with the cycle libraries. |
138 Future<_LibraryCycleResult> _compileCycle( | 147 Future<_LibraryCycleResult> _compileCycle( |
139 CanonicalName nameRoot, DillTarget dillTarget, LibraryCycle cycle) async { | 148 CanonicalName nameRoot, DillTarget dillTarget, LibraryCycle cycle) async { |
140 return _logger.runAsync('Compile cycle $cycle', () async { | 149 return _logger.runAsync('Compile cycle $cycle', () async { |
141 String signature; | 150 String signature; |
142 { | 151 { |
143 var signatureBuilder = new ApiSignature(); | 152 var signatureBuilder = new ApiSignature(); |
144 // TODO(scheglov) add salt | 153 signatureBuilder.addBytes(_salt); |
145 // signature.addUint32List(_fsState._salt); | |
146 Set<FileState> transitiveFiles = cycle.libraries | 154 Set<FileState> transitiveFiles = cycle.libraries |
147 .map((library) => library.transitiveFiles) | 155 .map((library) => library.transitiveFiles) |
148 .expand((files) => files) | 156 .expand((files) => files) |
149 .toSet(); | 157 .toSet(); |
150 signatureBuilder.addInt(transitiveFiles.length); | 158 signatureBuilder.addInt(transitiveFiles.length); |
151 for (var file in transitiveFiles) { | 159 for (var file in transitiveFiles) { |
152 signatureBuilder.addString(file.uri.toString()); | 160 signatureBuilder.addString(file.uri.toString()); |
153 // TODO(scheglov) use API signature | 161 // TODO(scheglov) use API signature |
154 signatureBuilder.addBytes(file.contentHash); | 162 signatureBuilder.addBytes(file.contentHash); |
155 } | 163 } |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
246 } | 254 } |
247 }); | 255 }); |
248 } else { | 256 } else { |
249 // TODO(scheglov) How to handle this? | 257 // TODO(scheglov) How to handle this? |
250 } | 258 } |
251 } | 259 } |
252 } | 260 } |
253 } while (wasChanged); | 261 } while (wasChanged); |
254 } | 262 } |
255 | 263 |
264 /// Fill [_salt] with data. | |
265 void _fillSalt() { | |
266 var saltBuilder = new ApiSignature(); | |
267 saltBuilder.addInt(DATA_VERSION); | |
268 saltBuilder.addBool(_options.strongMode); | |
269 var saltBytes = saltBuilder.toByteList(); | |
270 _salt.setRange(0, 16, saltBytes); | |
Paul Berry
2017/05/17 22:25:06
This doesn't seem safe. If saltBytes has fewer th
scheglov
2017/05/17 22:38:09
Hm... I think toByteList() returns exactly 16 byte
Paul Berry
2017/05/18 02:57:15
Oops, you're right. I forgot about that.
| |
271 } | |
272 | |
256 /// Refresh all the invalidated files and update dependencies. | 273 /// Refresh all the invalidated files and update dependencies. |
257 Future<Null> _refreshInvalidatedFiles() async { | 274 Future<Null> _refreshInvalidatedFiles() async { |
258 await _logger.runAsync('Refresh invalidated files', () async { | 275 await _logger.runAsync('Refresh invalidated files', () async { |
259 for (var fileUri in _invalidatedFiles) { | 276 for (var fileUri in _invalidatedFiles) { |
260 var file = await _fsState.getFile(fileUri); | 277 var file = await _fsState.getFile(fileUri); |
261 await file.refresh(); | 278 await file.refresh(); |
262 } | 279 } |
263 _invalidatedFiles.clear(); | 280 _invalidatedFiles.clear(); |
264 }); | 281 }); |
265 } | 282 } |
(...skipping 18 matching lines...) Expand all Loading... | |
284 /// TODO(scheglov) Use API signatures. | 301 /// TODO(scheglov) Use API signatures. |
285 /// TODO(scheglov) Or use tree shaking and compute signatures of outlines. | 302 /// TODO(scheglov) Or use tree shaking and compute signatures of outlines. |
286 final String signature; | 303 final String signature; |
287 | 304 |
288 /// Kernel libraries for libraries in the [cycle]. Bodies of dependencies | 305 /// Kernel libraries for libraries in the [cycle]. Bodies of dependencies |
289 /// are not included, but but references to those dependencies are included. | 306 /// are not included, but but references to those dependencies are included. |
290 final List<Library> kernelLibraries; | 307 final List<Library> kernelLibraries; |
291 | 308 |
292 _LibraryCycleResult(this.cycle, this.signature, this.kernelLibraries); | 309 _LibraryCycleResult(this.cycle, this.signature, this.kernelLibraries); |
293 } | 310 } |
OLD | NEW |