@@ -28,7 +28,9 @@ import 'package:kernel/target/targets.dart' show targets, TargetFlags;
2828import 'package:path/path.dart' as path;
2929import 'package:usage/uuid/uuid.dart' ;
3030
31- import 'package:vm/binary_cache.dart' show BinaryCache, Range;
31+ import 'package:vm/metadata/binary_cache.dart'
32+ show BinaryCacheMetadataRepository;
33+
3234import 'package:vm/bytecode/gen_bytecode.dart'
3335 show generateBytecode, createFreshComponentWithBytecode;
3436import 'package:vm/bytecode/options.dart' show BytecodeOptions;
@@ -281,8 +283,6 @@ class FrontendCompiler implements CompilerInterface {
281283 String _kernelBinaryFilenameIncremental;
282284 String _kernelBinaryFilenameFull;
283285 String _initializeFromDill;
284- String _bytecodeDillPath;
285- String _bytecodeCachePath;
286286
287287 Set <Uri > previouslyReportedDependencies = Set <Uri >();
288288
@@ -404,14 +404,12 @@ class FrontendCompiler implements CompilerInterface {
404404 ];
405405 }
406406
407- _bytecodeCachePath = _initializeFromDill + ".cache" ;
408- if (compilerOptions.bytecode) {
407+ if (compilerOptions.bytecode && _initializeFromDill != null ) {
409408 // If we are generating bytecode, put bytecode only (not AST) in
410409 // [_kernelBinaryFilename], which the user of this tool will eventually
411410 // feed to Flutter engine or flutter_tester. Use a separate file to cache
412411 // the AST result to initialize the incremental compiler for the next
413412 // invocation of this tool.
414- _bytecodeDillPath = _initializeFromDill;
415413 _initializeFromDill += ".ast" ;
416414 }
417415
@@ -558,38 +556,25 @@ class FrontendCompiler implements CompilerInterface {
558556 {bool filterExternal: false ,
559557 IncrementalSerializer incrementalSerializer}) async {
560558 final Component component = results.component;
559+ // Remove the cache that came either from this function or from
560+ // initializing from a kernel file.
561+ component.metadata.remove (BinaryCacheMetadataRepository .repositoryTag);
561562
562563 if (_compilerOptions.bytecode) {
563- BinaryCache inputCache;
564- if (_options['incremental' ]) {
565- inputCache = new BinaryCache ();
566- await inputCache.read (_bytecodeCachePath, _bytecodeDillPath);
567- }
568- BinaryCache outputCache = new BinaryCache ();
569-
570564 {
571565 // Generate bytecode as the output proper.
572566 final IOSink sink = File (filename).openWrite ();
573- int offset = 0 ;
574567 await runWithFrontEndCompilerContext (
575568 _mainSource, _compilerOptions, component, () async {
576569 if (_options['incremental' ]) {
577570 // When loading a single kernel buffer with multiple sub-components,
578571 // the VM expects 'main' to be the first sub-component.
579572 await forEachPackage (component,
580573 (String package, List <Library > libraries) async {
581- offset = _writePackage (
582- results,
583- package,
584- libraries,
585- sink,
586- offset,
587- inputCache,
588- outputCache,
589- incrementalSerializer? .invalidatedUris);
574+ _writePackage (results, package, libraries, sink);
590575 }, mainFirst: true );
591576 } else {
592- _writePackage (results, 'main' , component.libraries, sink, 0 );
577+ _writePackage (results, 'main' , component.libraries, sink);
593578 }
594579 });
595580 await sink.close ();
@@ -600,6 +585,15 @@ class FrontendCompiler implements CompilerInterface {
600585 // of [filename] so that a later invocation of frontend_server will the
601586 // same arguments will use this to initialize its incremental kernel
602587 // compiler.
588+ final repository = BinaryCacheMetadataRepository ();
589+ component.addMetadataRepository (repository);
590+ for (var lib in component.libraries) {
591+ var bytes = BinaryCacheMetadataRepository .lookup (lib);
592+ if (bytes != null ) {
593+ repository.mapping[lib] = bytes;
594+ }
595+ }
596+
603597 final file = new File (_initializeFromDill);
604598 await file.create (recursive: true );
605599 final IOSink sink = file.openWrite ();
@@ -613,18 +607,9 @@ class FrontendCompiler implements CompilerInterface {
613607
614608 sortComponent (component);
615609
616- if (incrementalSerializer != null ) {
617- incrementalSerializer.writePackagesToSinkAndTrimComponent (
618- component, sink);
619- } else if (unsafePackageSerialization == true ) {
620- writePackagesToSinkAndTrimComponent (component, sink);
621- }
622-
623610 printer.writeComponentFile (component);
624611 await sink.close ();
625612 }
626-
627- await outputCache.write (_bytecodeCachePath);
628613 } else {
629614 // Generate AST as the output proper.
630615 final IOSink sink = File (filename).openWrite ();
@@ -647,9 +632,6 @@ class FrontendCompiler implements CompilerInterface {
647632
648633 printer.writeComponentFile (component);
649634 await sink.close ();
650-
651- // Reset cache if not generating bytecode.
652- await new File (_bytecodeCachePath).writeAsString ('' );
653635 }
654636
655637 if (_options['split-output-by-packages' ]) {
@@ -732,33 +714,18 @@ class FrontendCompiler implements CompilerInterface {
732714 }
733715 }
734716
735- int _writePackage (KernelCompilationResults result, String package,
736- List <Library > libraries, IOSink sink, int offset,
737- [BinaryCache inputCache,
738- BinaryCache outputCache,
739- Set <Uri > invalidatedUris]) {
717+ void _writePackage (KernelCompilationResults result, String package,
718+ List <Library > libraries, IOSink sink) {
740719 final canCache = libraries.isNotEmpty &&
741720 _compilerOptions.bytecode &&
742721 errors.isEmpty &&
743722 package != "main" ;
744723
745- if (canCache && invalidatedUris != null ) {
746- final range = inputCache? .get (package);
747- if (range != null ) {
748- bool invalidated = false ;
749- for (var lib in libraries) {
750- if (invalidatedUris.contains (lib.fileUri)) {
751- invalidated = true ;
752- break ;
753- }
754- }
755-
756- if (! invalidated) {
757- final cachedBytes = inputCache.getBytes (range);
758- sink.add (cachedBytes);
759- outputCache.add (package, Range (offset, cachedBytes.length));
760- return offset + cachedBytes.length;
761- }
724+ if (canCache) {
725+ var cachedBytes = BinaryCacheMetadataRepository .lookup (libraries.first);
726+ if (cachedBytes != null ) {
727+ sink.add (cachedBytes);
728+ return ;
762729 }
763730 }
764731
@@ -783,9 +750,8 @@ class FrontendCompiler implements CompilerInterface {
783750 final bytes = byteSink.builder.takeBytes ();
784751 sink.add (bytes);
785752 if (canCache) {
786- outputCache. add (package, Range (offset, bytes.length) );
753+ BinaryCacheMetadataRepository . insert (libraries.first, bytes);
787754 }
788- return offset + bytes.length;
789755 }
790756
791757 @override
0 commit comments