From 518250eafd63021e178571c9204efafb9f98b329 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Fri, 18 Sep 2026 04:19:16 +0000 Subject: [PATCH] perf: memoize _get_default_github using @lru_cache Cache the default GitHub username computation in scripts/init.py using @lru_cache(maxsize=1) to eliminate redundant git config and git remote inspection calls. --- scripts/init.py | 2 ++ tests/test_init.py | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/scripts/init.py b/scripts/init.py index c4c8cae..06a0290 100644 --- a/scripts/init.py +++ b/scripts/init.py @@ -1,6 +1,7 @@ import os import re import shutil +from functools import lru_cache from pathlib import Path from subprocess import CalledProcessError, TimeoutExpired, check_output @@ -64,6 +65,7 @@ def _get_git_config(key: str) -> str: return "" +@lru_cache(maxsize=1) def _get_default_github() -> str: # Try git config first username = _get_git_config("github.user") or _get_git_config("user.name") diff --git a/tests/test_init.py b/tests/test_init.py index 977f56a..1329110 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -67,12 +67,16 @@ def mock_check_output(args, **kwargs): def test_get_default_github(monkeypatch): + # Clear lru_cache on _get_default_github before testing + init._get_default_github.cache_clear() + # Case 1: valid username in git config monkeypatch.setattr(init, "_git_config_loaded", True) monkeypatch.setattr(init, "_git_config_cache", {"github.user": "jane-doe"}) assert init._get_default_github() == "jane-doe" # Case 2: invalid username in git config, fallback to remote URL (HTTPS format) + init._get_default_github.cache_clear() monkeypatch.setattr(init, "_git_config_cache", {}) def mock_check_output(args, **kwargs): @@ -84,6 +88,8 @@ def mock_check_output(args, **kwargs): assert init._get_default_github() == "some-org" # Case 3: invalid username in git config, fallback to remote URL (SSH format) + init._get_default_github.cache_clear() + def mock_check_output_ssh(args, **kwargs): if "remote" in args and "get-url" in args: return "git@github.com:ssh-user/some-repo.git\n"