E240 perf: cheaper first call of recommend (importlib.resources and pathlib off the path) by rafatahmed · Pull Request #343 · rafatahmed/Flahax · GitHub
[go: up one dir, main page]

Skip to content

perf: cheaper first call of recommend (importlib.resources and pathlib off the path) - #343

Merged
rafatahmed merged 3 commits into
mainfrom
perf/first-call-recommend
Nov 27, 2026
Merged

rafatahmed merged 3 commits into
mainfrom
perf/first-call-recommend

Conversation

@rafatahmed
Copy link
Copy Markdown
Owner

Closes #342

Two imports sat on the first-use path of recommend only for small jobs:

  • importlib.resources.files (six modules) was imported just to read a packaged JSON file; it pulls in tempfile, shutil, zipfile and the path readers. New flahax/package_data.py opens the same file beside the package (same bytes; text keeps universal newlines).
  • pathlib was imported at the top of inputs.py for one read_json call; it is now imported inside that function.

Evidence

Python 3.13 with compiled bytecode, best of 3:

before after
first use of flahax.recommend 73 ms 49 ms
first recommend(...) call 17 ms 17.5 ms
total 90 ms 67 ms

Steady state is 3.5-4.4 ms, so the first call still costs about 60 ms more. What remains is mostly standard library: json plus re 17 ms, dataclasses plus inspect 18 ms, enum 7 ms, and hashlib 6 ms inside the first call (needed for the identities digest in materials).

  • New tests/test_package_data.py: every packaged data file reads identically through the helper and through importlib.resources; a fresh first recommend imports neither importlib.resources nor pathlib.
  • 215 test modules pass locally; heavy modules run in CI. Results are unchanged (the same files are read).

Correction to #336

I measured #336 in a shell that sets PYTHONDONTWRITEBYTECODE, so changed modules were recompiled on every run. With compiled bytecode the eager import flahax was 145 ms, not 350 ms; it is now 1.2 ms. The change is still real, just smaller than stated there.

@rafatahmed rafatahmed added the enhancement New feature or request label Nov 27, 2026
@rafatahmed rafatahmed self-assigned this Nov 27, 2026
@rafatahmed
rafatahmed marked this pull request as ready for review November 27, 2026 01:41
@codecov
codecov Bot commented Nov 27, 2026 •
Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (3fd4117) to head (de64fa9).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #343   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          149       150    +1     
  Lines        17588     17596    +8     
  Branches      3561      3561           
=========================================
+ Hits         17588     17596    +8     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@rafatahmed
rafatahmed merged commit fbdde7c into main Nov 27, 2026
29 checks passed
@rafatahmed
rafatahmed deleted the perf/first-call-recommend branch November 27, 2026 01:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

perf: first recommend() call costs about 90 ms of imports and one-time setup (importlib.resources alone 36 ms)

1 participant

0