testsAndMisc/python_pkg/vscode_optimizer/_plan.py
Krzysztof kuhy Rudnicki 377a352d48
Some checks are pending
Pre-commit checks / pre-commit (push) Waiting to run
Python tests / test (push) Waiting to run
Shell tests / shell-tests (push) Waiting to run
Shell tests / arch-tests (push) Waiting to run
vscode_optimizer: split _optimize.py 498 -> 100 + 5 modules
The last python file over the 250-line cap. Now that it sits at 100% line
and branch coverage, split along its natural seams:

  _types.py     48  the three dataclasses, so nothing imports a sibling
                    merely to name a type (which would have made a cycle)
  _render.py    69  the ANSI palette and the printers
  _config_io.py 104 variant discovery, JSONC parsing, reads and writes
  _hardware.py  106 _run and the four probes
  _plan.py      156 the pure hardware -> proposed-changes functions
  _optimize.py  100 the CLI entry: _apply_variant and main

Definitions were moved by line range rather than retyped. A throwaway
ast.dump identity check against HEAD confirms all 26 top-level definitions
are unchanged - formatting-blind, logic-sensitive.

The first attempt raised NameError: name '_BO' is not defined at runtime;
_BO was used by main() but missing from the _render import list. Rather than
fix that one failure alone, an AST scan over all six modules for names used
but not bound confirmed nothing else was missing.

Test imports now name the owning module; no re-export barrels. One patch
target genuinely moved - the guard-rail test patches _config_io.Path, since
that is the namespace resolving the name when _discover_variants is called
directly rather than through main().

Verified: same 98 tests pass with every module at 100% line and branch;
python3 -m python_pkg.vscode_optimizer --dry-run reproduces the same
18-change plan and writes nothing; repo-wide pytest 1846 passed at 100%;
over-cap files 36 -> 35.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011w2Pj4Ek3ZNMQVYcFHgXDY
2026-08-17 13:15:50 +02:00

157 lines
4.5 KiB
Python

"""Turn detected hardware plus current settings into a list of changes.
Pure functions: nothing here reads the filesystem or shells out. Each
proposal carries the value it replaces, so the plan can be shown as a diff.
"""
from __future__ import annotations
from python_pkg.vscode_optimizer._types import _Hw, _Opt
_RAM_THRESHOLDS = ((28000, 4096), (14000, 2048), (7000, 1024))
_DEFAULT_MEM = 512
_MIB_1024 = 1024
_MIN_THREADS = 4
_SUBMOD_LIMIT = 30
_WATCHER_EX: dict[str, bool] = dict.fromkeys(
[
"**/.git/objects/**",
"**/.git/subtree-cache/**",
"**/node_modules/**",
"**/.venv/**",
"**/venv/**",
"**/__pycache__/**",
"**/build/**",
"**/.mypy_cache/**",
"**/.ruff_cache/**",
"**/.pytest_cache/**",
"**/dist/**",
"**/*.egg-info/**",
],
True,
)
_SEARCH_EX: dict[str, bool] = dict.fromkeys(
[
"**/node_modules",
"**/build",
"**/.venv",
"**/venv",
"**/__pycache__",
"**/.mypy_cache",
"**/.ruff_cache",
"**/.pytest_cache",
"**/dist",
],
True,
)
def _ideal_mem(ram_mb: int) -> int:
for threshold, value in _RAM_THRESHOLDS:
if ram_mb >= threshold:
return value
return _DEFAULT_MEM
def _dict_merge_opt(
cur_settings: dict[str, object],
key: str,
ideal: dict[str, bool],
reason: str,
) -> _Opt | None:
cur = cur_settings.get(key, {})
if not isinstance(cur, dict):
cur = {}
if all(k in cur for k in ideal):
return None
return _Opt(key, {**cur, **ideal}, reason, cur or None)
def _compute_opts(hw: _Hw, cur: dict[str, object]) -> list[_Opt]:
"""Return optimizations based on hardware and current settings."""
opts: list[_Opt] = []
def _p(key: str, val: object, reason: str) -> None:
if cur.get(key) != val:
opts.append(_Opt(key, val, reason, cur.get(key)))
threads = max(_MIN_THREADS, hw.cpu_physical_cores)
_p(
"search.maxThreads",
threads,
f"{hw.cpu_physical_cores} physical cores - use them for workspace search",
)
mem = _ideal_mem(hw.ram_total_mb)
_p(
"files.maxMemoryForLargeFilesMB",
mem,
f"{hw.ram_total_mb // _MIB_1024} GB RAM - allow up to {mem} MB for large files",
)
if hw.gpu_vendor in ("NVIDIA", "AMD"):
_p(
"terminal.integrated.gpuAcceleration",
"on",
f"{hw.gpu_vendor} GPU - enable GPU-rendered terminal",
)
smooth = True
for key in (
"editor.smoothScrolling",
"workbench.list.smoothScrolling",
"terminal.integrated.smoothScrolling",
):
_p(key, smooth, "Smooth scrolling is free with a dedicated GPU")
no = False
_p("search.followSymlinks", no, "Avoid duplicate results and wasted I/O")
for result in (
_dict_merge_opt(
cur,
"files.watcherExclude",
_WATCHER_EX,
"Exclude build/cache dirs from file watcher",
),
_dict_merge_opt(
cur, "search.exclude", _SEARCH_EX, "Exclude build/cache dirs from search"
),
):
if result:
opts.extend([result])
_p("editor.guides.bracketPairs", "active", "Lightweight visual aid")
_p(
"diffEditor.maxComputationTime",
0,
f"Fast CPU ({hw.cpu_model}) - compute diffs fully",
)
_p("editor.minimap.enabled", no, "Minimap consumes GPU/CPU for little benefit")
cur_sub = cur.get("git.detectSubmodulesLimit")
if cur_sub is None or (isinstance(cur_sub, int) and cur_sub < _SUBMOD_LIMIT):
opts.append(
_Opt(
"git.detectSubmodulesLimit",
_SUBMOD_LIMIT,
"Higher limit is fine with fast CPU",
cur_sub,
)
)
return opts
def _gpu_flags(hw: _Hw) -> list[str]:
"""Return Electron flags appropriate for the detected GPU."""
if hw.gpu_vendor in ("NVIDIA", "AMD"):
base = [
"--enable-gpu-rasterization",
"--enable-zero-copy",
"--ignore-gpu-blocklist",
"--enable-features=CanvasOopRasterization",
]
if hw.gpu_vendor == "NVIDIA":
base.append("--enable-features=VaapiVideoDecodeLinuxGL,VaapiVideoEncoder")
return base
if hw.gpu_vendor == "Intel":
return [
"--enable-gpu-rasterization",
"--ignore-gpu-blocklist",
"--enable-features=VaapiVideoDecodeLinuxGL",
]
return []