Repository navigation
perf: cheaper first call of recommend (importlib.resources and pathlib off the path) - #343
Merged
Merged
Conversation
…commend 90 to 67 ms (#342)
rafatahmed
marked this pull request as ready for review
November 27, 2026 01:41
Codecov Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #342
Two imports sat on the first-use path of
recommendonly 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. Newflahax/package_data.pyopens the same file beside the package (same bytes; text keeps universal newlines).pathlibwas imported at the top ofinputs.pyfor oneread_jsoncall; it is now imported inside that function.Evidence
Python 3.13 with compiled bytecode, best of 3:
flahax.recommendrecommend(...)callSteady state is 3.5-4.4 ms, so the first call still costs about 60 ms more. What remains is mostly standard library:
jsonplusre17 ms,dataclassesplusinspect18 ms,enum7 ms, andhashlib6 ms inside the first call (needed for the identities digest inmaterials).tests/test_package_data.py: every packaged data file reads identically through the helper and throughimportlib.resources; a fresh firstrecommendimports neitherimportlib.resourcesnorpathlib.Correction to #336
I measured #336 in a shell that sets
PYTHONDONTWRITEBYTECODE, so changed modules were recompiled on every run. With compiled bytecode the eagerimport flahaxwas 145 ms, not 350 ms; it is now 1.2 ms. The change is still real, just smaller than stated there.