From aaa277f4cbf48b1f4693f0b2d278ecd238c2402f Mon Sep 17 00:00:00 2001 From: johnnybeckett <58772359+johnnybeckett@users.noreply.github.com> Date: Sun, 30 Apr 2023 03:18:53 +0100 Subject: [PATCH] Speedup gita ll by using a threadpool (#238) Each line of 'll' output is generated from running git commands, by using a thread pool the commands for each line can be ran in parallel. Co-authored-by: John Beckett --- gita/utils.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/gita/utils.py b/gita/utils.py index bf063ab..76aebce 100644 --- a/gita/utils.py +++ b/gita/utils.py @@ -9,6 +9,8 @@ from pathlib import Path from typing import List, Dict, Coroutine, Union, Iterator, Tuple from collections import Counter, defaultdict +from concurrent.futures import ThreadPoolExecutor +import multiprocessing from . import info from . import common @@ -428,17 +430,20 @@ def describe(repos: Dict[str, Dict[str, str]], no_colors: bool = False) -> str: Return the status of all repos """ if repos: - name_width = max(len(n) for n in repos) + 1 - funcs = info.get_info_funcs() - - get_repo_status = info.get_repo_status - if get_repo_status in funcs and no_colors: - idx = funcs.index(get_repo_status) - funcs[idx] = partial(get_repo_status, no_colors=True) - - for name in sorted(repos): - info_items = " ".join(f(repos[name]) for f in funcs) - yield f"{name:<{name_width}}{info_items}" + name_width = len(max(repos, key=len)) + 1 + funcs = info.get_info_funcs() + + get_repo_status = info.get_repo_status + if get_repo_status in funcs and no_colors: + idx = funcs.index(get_repo_status) + funcs[idx] = partial(get_repo_status, no_colors=True) + + num_threads = min(multiprocessing.cpu_count(), len(repos)) + with ThreadPoolExecutor(max_workers=num_threads) as executor: + for line in executor.map( + lambda repo: f'{repo:<{name_width}}{" ".join(f(repos[repo]) for f in funcs)}', + sorted(repos)): + yield line def get_cmds_from_files() -> Dict[str, Dict[str, str]]: