forked from firka/flutter
Speed up first asset load by encoding asset manifest in binary rather than JSON (#113637)
This commit is contained in:
@@ -2,8 +2,11 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:meta/meta.dart';
|
||||
import 'package:package_config/package_config.dart';
|
||||
import 'package:standard_message_codec/standard_message_codec.dart';
|
||||
|
||||
import 'base/context.dart';
|
||||
import 'base/deferred_component.dart';
|
||||
@@ -136,6 +139,9 @@ class ManifestAssetBundle implements AssetBundle {
|
||||
_splitDeferredAssets = splitDeferredAssets,
|
||||
_licenseCollector = LicenseCollector(fileSystem: fileSystem);
|
||||
|
||||
// We assume the main asset is designed for a device pixel ratio of 1.0
|
||||
static const double _defaultResolution = 1.0;
|
||||
|
||||
final Logger _logger;
|
||||
final FileSystem _fileSystem;
|
||||
final LicenseCollector _licenseCollector;
|
||||
@@ -161,7 +167,8 @@ class ManifestAssetBundle implements AssetBundle {
|
||||
|
||||
DateTime? _lastBuildTimestamp;
|
||||
|
||||
static const String _kAssetManifestJson = 'AssetManifest.json';
|
||||
static const String _kAssetManifestBinFileName = 'AssetManifest.bin';
|
||||
static const String _kAssetManifestJsonFileName = 'AssetManifest.json';
|
||||
static const String _kNoticeFile = 'NOTICES';
|
||||
// Comically, this can't be name with the more common .gz file extension
|
||||
// because when it's part of an AAR and brought into another APK via gradle,
|
||||
@@ -229,8 +236,13 @@ class ManifestAssetBundle implements AssetBundle {
|
||||
// device.
|
||||
_lastBuildTimestamp = DateTime.now();
|
||||
if (flutterManifest.isEmpty) {
|
||||
entries[_kAssetManifestJson] = DevFSStringContent('{}');
|
||||
entryKinds[_kAssetManifestJson] = AssetKind.regular;
|
||||
entries[_kAssetManifestJsonFileName] = DevFSStringContent('{}');
|
||||
entryKinds[_kAssetManifestJsonFileName] = AssetKind.regular;
|
||||
final ByteData emptyAssetManifest =
|
||||
const StandardMessageCodec().encodeMessage(<dynamic, dynamic>{})!;
|
||||
entries[_kAssetManifestBinFileName] =
|
||||
DevFSByteContent(emptyAssetManifest.buffer.asUint8List(0, emptyAssetManifest.lengthInBytes));
|
||||
entryKinds[_kAssetManifestBinFileName] = AssetKind.regular;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -426,7 +438,11 @@ class ManifestAssetBundle implements AssetBundle {
|
||||
_wildcardDirectories[uri] ??= _fileSystem.directory(uri);
|
||||
}
|
||||
|
||||
final DevFSStringContent assetManifest = _createAssetManifest(assetVariants, deferredComponentsAssetVariants);
|
||||
final Map<String, List<String>> assetManifest =
|
||||
_createAssetManifest(assetVariants, deferredComponentsAssetVariants);
|
||||
final DevFSStringContent assetManifestJson = DevFSStringContent(json.encode(assetManifest));
|
||||
final DevFSByteContent assetManifestBinary = _createAssetManifestBinary(assetManifest);
|
||||
|
||||
final DevFSStringContent fontManifest = DevFSStringContent(json.encode(fonts));
|
||||
final LicenseResult licenseResult = _licenseCollector.obtainLicenses(packageConfig, additionalLicenseFiles);
|
||||
if (licenseResult.errorMessages.isNotEmpty) {
|
||||
@@ -450,7 +466,8 @@ class ManifestAssetBundle implements AssetBundle {
|
||||
_fileSystem.file('DOES_NOT_EXIST_RERUN_FOR_WILDCARD$suffix').absolute);
|
||||
}
|
||||
|
||||
_setIfChanged(_kAssetManifestJson, assetManifest, AssetKind.regular);
|
||||
_setIfChanged(_kAssetManifestJsonFileName, assetManifestJson, AssetKind.regular);
|
||||
_setIfChanged(_kAssetManifestBinFileName, assetManifestBinary, AssetKind.regular);
|
||||
_setIfChanged(kFontManifestJson, fontManifest, AssetKind.regular);
|
||||
_setLicenseIfChanged(licenseResult.combinedLicenses, targetPlatform);
|
||||
return 0;
|
||||
@@ -459,17 +476,31 @@ class ManifestAssetBundle implements AssetBundle {
|
||||
@override
|
||||
List<File> additionalDependencies = <File>[];
|
||||
|
||||
void _setIfChanged(String key, DevFSStringContent content, AssetKind assetKind) {
|
||||
if (!entries.containsKey(key)) {
|
||||
entries[key] = content;
|
||||
entryKinds[key] = assetKind;
|
||||
void _setIfChanged(String key, DevFSContent content, AssetKind assetKind) {
|
||||
bool areEqual(List<int> o1, List<int> o2) {
|
||||
if (o1.length != o2.length) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int index = 0; index < o1.length; index++) {
|
||||
if (o1[index] != o2[index]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
final DevFSContent? oldContent = entries[key];
|
||||
// In the case that the content is unchanged, we want to avoid an overwrite
|
||||
// as the isModified property may be reset to true,
|
||||
if (oldContent is DevFSByteContent && content is DevFSByteContent &&
|
||||
areEqual(oldContent.bytes, content.bytes)) {
|
||||
return;
|
||||
}
|
||||
final DevFSStringContent? oldContent = entries[key] as DevFSStringContent?;
|
||||
if (oldContent?.string != content.string) {
|
||||
entries[key] = content;
|
||||
entryKinds[key] = assetKind;
|
||||
}
|
||||
|
||||
entries[key] = content;
|
||||
entryKinds[key] = assetKind;
|
||||
}
|
||||
|
||||
void _setLicenseIfChanged(
|
||||
@@ -621,14 +652,14 @@ class ManifestAssetBundle implements AssetBundle {
|
||||
return deferredComponentsAssetVariants;
|
||||
}
|
||||
|
||||
DevFSStringContent _createAssetManifest(
|
||||
Map<String, List<String>> _createAssetManifest(
|
||||
Map<_Asset, List<_Asset>> assetVariants,
|
||||
Map<String, Map<_Asset, List<_Asset>>> deferredComponentsAssetVariants
|
||||
) {
|
||||
final Map<String, List<String>> jsonObject = <String, List<String>>{};
|
||||
final Map<_Asset, List<String>> jsonEntries = <_Asset, List<String>>{};
|
||||
final Map<String, List<String>> manifest = <String, List<String>>{};
|
||||
final Map<_Asset, List<String>> entries = <_Asset, List<String>>{};
|
||||
assetVariants.forEach((_Asset main, List<_Asset> variants) {
|
||||
jsonEntries[main] = <String>[
|
||||
entries[main] = <String>[
|
||||
for (final _Asset variant in variants)
|
||||
variant.entryUri.path,
|
||||
];
|
||||
@@ -636,26 +667,69 @@ class ManifestAssetBundle implements AssetBundle {
|
||||
if (deferredComponentsAssetVariants != null) {
|
||||
for (final Map<_Asset, List<_Asset>> componentAssets in deferredComponentsAssetVariants.values) {
|
||||
componentAssets.forEach((_Asset main, List<_Asset> variants) {
|
||||
jsonEntries[main] = <String>[
|
||||
entries[main] = <String>[
|
||||
for (final _Asset variant in variants)
|
||||
variant.entryUri.path,
|
||||
];
|
||||
});
|
||||
}
|
||||
}
|
||||
final List<_Asset> sortedKeys = jsonEntries.keys.toList()
|
||||
final List<_Asset> sortedKeys = entries.keys.toList()
|
||||
..sort((_Asset left, _Asset right) => left.entryUri.path.compareTo(right.entryUri.path));
|
||||
for (final _Asset main in sortedKeys) {
|
||||
final String decodedEntryPath = Uri.decodeFull(main.entryUri.path);
|
||||
final List<String> rawEntryVariantsPaths = jsonEntries[main]!;
|
||||
final List<String> rawEntryVariantsPaths = entries[main]!;
|
||||
final List<String> decodedEntryVariantPaths = rawEntryVariantsPaths
|
||||
.map((String value) => Uri.decodeFull(value))
|
||||
.toList();
|
||||
jsonObject[decodedEntryPath] = decodedEntryVariantPaths;
|
||||
manifest[decodedEntryPath] = decodedEntryVariantPaths;
|
||||
}
|
||||
return DevFSStringContent(json.encode(jsonObject));
|
||||
return manifest;
|
||||
}
|
||||
|
||||
DevFSByteContent _createAssetManifestBinary(
|
||||
Map<String, List<String>> assetManifest
|
||||
) {
|
||||
double parseScale(String key) {
|
||||
final Uri assetUri = Uri.parse(key);
|
||||
String directoryPath = '';
|
||||
if (assetUri.pathSegments.length > 1) {
|
||||
directoryPath = assetUri.pathSegments[assetUri.pathSegments.length - 2];
|
||||
}
|
||||
|
||||
final Match? match = _extractRatioRegExp.firstMatch(directoryPath);
|
||||
if (match != null && match.groupCount > 0) {
|
||||
return double.parse(match.group(1)!);
|
||||
}
|
||||
return _defaultResolution;
|
||||
}
|
||||
|
||||
final Map<String, dynamic> result = <String, dynamic>{};
|
||||
|
||||
for (final MapEntry<String, dynamic> manifestEntry in assetManifest.entries) {
|
||||
final List<dynamic> resultVariants = <dynamic>[];
|
||||
final List<String> entries = (manifestEntry.value as List<dynamic>).cast<String>();
|
||||
for (final String variant in entries) {
|
||||
if (variant == manifestEntry.key) {
|
||||
// With the newer binary format, don't include the main asset in it's
|
||||
// list of variants. This reduces parsing time at runtime.
|
||||
continue;
|
||||
}
|
||||
final Map<String, dynamic> resultVariant = <String, dynamic>{};
|
||||
final double variantDevicePixelRatio = parseScale(variant);
|
||||
resultVariant['asset'] = variant;
|
||||
resultVariant['dpr'] = variantDevicePixelRatio;
|
||||
resultVariants.add(resultVariant);
|
||||
}
|
||||
result[manifestEntry.key] = resultVariants;
|
||||
}
|
||||
|
||||
final ByteData message = const StandardMessageCodec().encodeMessage(result)!;
|
||||
return DevFSByteContent(message.buffer.asUint8List(0, message.lengthInBytes));
|
||||
}
|
||||
|
||||
static final RegExp _extractRatioRegExp = RegExp(r'/?(\d+(\.\d*)?)x$');
|
||||
|
||||
/// Prefixes family names and asset paths of fonts included from packages with
|
||||
/// 'packages/<package_name>'
|
||||
List<Font> _parsePackageFonts(
|
||||
|
||||
Reference in New Issue
Block a user