From 6ea64cbb714059f8bd020200c10d6f1fcc891b54 Mon Sep 17 00:00:00 2001 From: Darren Chan <74682667+chandarrengoog@users.noreply.github.com> Date: Fri, 18 Jun 2021 19:35:06 -0400 Subject: [PATCH] [build] Speed up incremental & no-op builds. (flutter/engine#26815) Per gn documentation, the output timestamp can remain the same if all outputs remain the same, resulting in large time savings since dependent targets don't need to be rebuilt. See: https://gn.googlesource.com/gn/+/HEAD/docs/reference.md#target-declarations-action_declare-a-target-that-runs-a-script-a-single-time-outputs Currently, `copy_debug_symbols.py` updates timestamps unconditionally, resulting in slower incremental and no-op builds. We change its current behavior to not update the timestamps if the debug symbols will not change. Local testing indicates around a `20s` improvement for no-op and incremental builds: - Control: http://go/paste/4800062489100288 - Experiment: http://go/paste/5727928869453824 See https://fxbug.dev/79001. CC: @naudzghebre --- .../tools/fuchsia/copy_debug_symbols.py | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/engine/src/flutter/tools/fuchsia/copy_debug_symbols.py b/engine/src/flutter/tools/fuchsia/copy_debug_symbols.py index 71a647e73a..2724b079cf 100755 --- a/engine/src/flutter/tools/fuchsia/copy_debug_symbols.py +++ b/engine/src/flutter/tools/fuchsia/copy_debug_symbols.py @@ -12,6 +12,7 @@ import argparse import errno +import hashlib import json import os import re @@ -21,6 +22,18 @@ import sys import time +def HashFile(filepath): + """Calculates the hash of a file without reading it all in memory at once.""" + digest = hashlib.sha1() + with open(filepath, 'rb') as f: + while True: + chunk = f.read(1024*1024) + if not chunk: + break + digest.update(chunk) + return digest.hexdigest() + + def Touch(fname): with open(fname, 'a'): os.utime(fname, None) @@ -111,9 +124,14 @@ def main(): dbg_file_name = '%s%s' % (parts['exec_name'], dbg_suffix) dbg_file_path = os.path.join(dbg_prefix_base, dbg_file_name) + # If the debug file hasn't changed, don't rewrite the debug and completion + # file, speeding up incremental builds. + if os.path.exists(dbg_file_path) and HashFile(args.exec_path) == HashFile(dbg_file_path): + return 0 + shutil.copyfile(args.exec_path, dbg_file_path) - # Note this needs to be in sync with debug_symbols.gni + # Note this needs to be in sync with fuchsia_debug_symbols.gni completion_file = os.path.join(args.dest, '.%s_dbg_success' % args.exec_name) Touch(completion_file)