From 3c3f492ff024b4875696b11db73a7370545442fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Rogalski?= Date: Wed, 29 Sep 2021 23:47:19 +0200 Subject: [PATCH 1/3] Performance improvements in __main__.py Do not recompute invariant values in bisect loop. --- src/darker/__main__.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/darker/__main__.py b/src/darker/__main__.py index 22f4b15ee..cf768279e 100644 --- a/src/darker/__main__.py +++ b/src/darker/__main__.py @@ -125,6 +125,21 @@ def _reformat_single_file( # pylint: disable=too-many-arguments,too-many-locals src = git_root / path_in_repo edited_linenums_differ = EditedLinenumsDiffer(git_root, revrange) + # 4. run black + formatted = run_black(rev2_isorted, black_config) + logger.debug("Read %s lines from edited file %s", len(rev2_isorted.lines), src) + logger.debug("Black reformat resulted in %s lines", len(formatted.lines)) + + # 5. get the diff between the edited and reformatted file + opcodes = diff_and_get_opcodes(rev2_isorted, formatted) + + # 6. convert the diff into chunks + black_chunks = list(opcodes_to_chunks(opcodes, rev2_isorted, formatted)) + + # Exit early if nothing to do + if not black_chunks: + return rev2_isorted + max_context_lines = len(rev2_isorted.lines) minimum_context_lines = BinarySearch(0, max_context_lines + 1) last_successful_reformat = None @@ -146,17 +161,6 @@ def _reformat_single_file( # pylint: disable=too-many-arguments,too-many-locals last_successful_reformat = rev2_isorted break - # 4. run black - formatted = run_black(rev2_isorted, black_config) - logger.debug("Read %s lines from edited file %s", len(rev2_isorted.lines), src) - logger.debug("Black reformat resulted in %s lines", len(formatted.lines)) - - # 5. get the diff between the edited and reformatted file - opcodes = diff_and_get_opcodes(rev2_isorted, formatted) - - # 6. convert the diff into chunks - black_chunks = list(opcodes_to_chunks(opcodes, rev2_isorted, formatted)) - # 7. choose reformatted content chosen = TextDocument.from_lines( choose_lines(black_chunks, edited_linenums), From ed4ded08bbb928533b5ad9f0e15e22138199539e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Rogalski?= Date: Thu, 30 Sep 2021 22:58:48 +0200 Subject: [PATCH 2/3] Fix code style issue --- src/darker/__main__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/darker/__main__.py b/src/darker/__main__.py index cf768279e..0b0f2cf05 100644 --- a/src/darker/__main__.py +++ b/src/darker/__main__.py @@ -132,14 +132,14 @@ def _reformat_single_file( # pylint: disable=too-many-arguments,too-many-locals # 5. get the diff between the edited and reformatted file opcodes = diff_and_get_opcodes(rev2_isorted, formatted) - + # 6. convert the diff into chunks black_chunks = list(opcodes_to_chunks(opcodes, rev2_isorted, formatted)) # Exit early if nothing to do if not black_chunks: return rev2_isorted - + max_context_lines = len(rev2_isorted.lines) minimum_context_lines = BinarySearch(0, max_context_lines + 1) last_successful_reformat = None From 2a89d06447ec1e205ec21c153ad4a6207a05f9f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Rogalski?= Date: Thu, 30 Sep 2021 23:03:57 +0200 Subject: [PATCH 3/3] Update Changelog --- CHANGES.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.rst b/CHANGES.rst index 91c27e328..50641fb04 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -15,6 +15,7 @@ Fixed - ``/foo $ darker --diff /bar/my-repo`` now works: the current working directory can be in a different part of the directory hierarchy - An incompatible ``isort`` version now causes a short user-friendly error message +- Improve bisect performance by not recomputing invariant data within bisect loop 1.3.0_ - 2021-09-04