Unverified Commit b47da6c2 authored by Stanislav Baranov's avatar Stanislav Baranov Committed by GitHub

Add support for binary compression of dynamic patches by the flutter tool. (#27754)

This reduces the typical dynamic patch size by more than 10x.

Also see https://github.com/flutter/engine/pull/7777 for decompression support in the runtime.
parent 8101862b
......@@ -10,6 +10,7 @@ import 'package:meta/meta.dart';
import '../android/android_sdk.dart';
import '../application_package.dart';
import '../artifacts.dart';
import '../base/bsdiff.dart';
import '../base/common.dart';
import '../base/file_system.dart';
import '../base/logger.dart';
......@@ -498,6 +499,7 @@ Future<void> _buildGradleProjectV2(
throwToolExit('Error: Could not find baseline package ${baselineApkFile.path}.');
printStatus('Found baseline package ${baselineApkFile.path}.');
printStatus('Creating dynamic patch...');
final Archive newApk = ZipDecoder().decodeBytes(apkFile.readAsBytesSync());
final Archive oldApk = ZipDecoder().decodeBytes(baselineApkFile.readAsBytesSync());
......@@ -519,7 +521,14 @@ Future<void> _buildGradleProjectV2(
throwToolExit("Error: Dynamic patching doesn't support changes to ${newFile.name}.");
final String name = fs.path.relative(newFile.name, from: 'assets/');
update.addFile(ArchiveFile(name, newFile.content.length, newFile.content));
if (name.contains('_snapshot_')) {
final List<int> diff = bsdiff(oldFile.content, newFile.content);
final int ratio = 100 * diff.length ~/ newFile.content.length;
printStatus('Deflated $name by ${ratio == 0 ? 99 : 100 - ratio}%');
update.addFile(ArchiveFile(name + '.bzdiff40', diff.length, diff));
} else {
update.addFile(ArchiveFile(name, newFile.content.length, newFile.content));
}
}
File updateFile;
......@@ -567,7 +576,8 @@ Future<void> _buildGradleProjectV2(
updateFile.parent.createSync(recursive: true);
updateFile.writeAsBytesSync(ZipEncoder().encode(update), flush: true);
printStatus('Created dynamic patch ${updateFile.path}.');
final String patchSize = getSizeAsMB(updateFile.lengthSync());
printStatus('Created dynamic patch ${updateFile.path} ($patchSize).');
}
} else {
final File bundleFile = _findBundleFile(project, buildInfo);
......
This diff is collapsed.
// Copyright 2017 The Chromium Authors. All rights reserved.
// 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:flutter_tools/src/base/bsdiff.dart';
import '../src/common.dart';
void main() {
group('Main', () {
test('generates diff', () {
final Uint8List a = Uint8List.fromList('Hello'.runes.toList());
final Uint8List b = Uint8List.fromList('World'.runes.toList());
final Uint8List c = Uint8List.fromList(bsdiff(a, b));
expect(bspatch(a, c), equals(b));
});
});
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment