Skip to content

fix: Fixed logic error in index_tweak_folder which resulted in parsing all tweaks twice#15

Merged
wojtmic merged 4 commits into
wojtmic:masterfrom
jamakoiv:get_tweaks_rewrite
Mar 16, 2026
Merged

fix: Fixed logic error in index_tweak_folder which resulted in parsing all tweaks twice#15
wojtmic merged 4 commits into
wojtmic:masterfrom
jamakoiv:get_tweaks_rewrite

Conversation

@jamakoiv

Copy link
Copy Markdown
Contributor

Description

The implementation of rejecting same tweaks in index_tweak_folder only considered duplicates inside the same folder,
not possible duplicates from other previously indexed folders. Since the tweak layer and name are constructed from the folder structure and filename, and no known OS filesystem allows two identical filenames in same folder, this resulted in dead code.
The rejection of duplicates only happened after the files had been already read and parsed, which created double the amount of calls to the JSON-parser which is quite slow in python.

Fixes:

  • Rewrote the index_tweak_folder to use Path.rglob -function for getting tweak filenames. rglob handles subdirectories without need for recursive function pattern.
  • Duplicate tweaks are dropped based on the tweak name before parsing. The tweaks found in DIR_USER are prioritized over the ones in SYSTEM and PACKAGE.
  • Refactored reading and parsing the tweak files to separate parse_tweak -function. This makes it easier to use multiprocessing to parse the tweaks if necessary in the future.
  • get_tweak -function now parses only the requested tweak instead of parsing all tweaks and returning just the requested one.
  • Added basic unit-tests. The test tweak-files screw up the line-counts, so i can separate these to another pull-request if you prefer cleaner history.

With the above changes cProfile.run("get_tweaks()") went from

7298618 function calls (6548291 primitive calls) in 2.461 seconds

to

3628019 function calls (3256901 primitive calls) in 1.244 seconds

and cProfile.run("get_tweak('themes.reset')") from

7298619 function calls (6548292 primitive calls) in 2.417 seconds

to

116207 function calls (105288 primitive calls) in 0.048 seconds

API-changes:

  • index_tweak_folder return value changed from dict[str, TweakData] to dict[str, Path]. Only called internally in tweaks.py.
  • get_tweak_names return value changed from set[str] to dict[str, Path]. All code calling get_tweak_names is ok with this.

Type of Change

  • feat: New feature
  • fix: Bug fix
  • docs: Documentation update
  • tweak: New tweak file
  • test: Test additions/changes
  • chore: Build/tooling changes

Checklist

Logic error in the previous implementation resulted in all tweaks being
read and parsed multiple times.

- Replaced recursive logic in index_tweak_folder with using Path.rglob
-function.
- Function index_tweak_folder now returns the tweak name (in pattern
layer.name) and path to the tweak. Tweaks with duplicate names can be
dropped before parsing the JSON.
- Separated reading and parsing of the JSON-files to separate
  parse_tweak -function. This makes it easier to use multiprocessing for
parsing the JSONs if necessary.
- get_tweak -function now parses only the requested tweak instead of all
  tweaks.
- Added basic logging.
@wojtmic

wojtmic commented Mar 16, 2026

Copy link
Copy Markdown
Owner

Thanks for the fix!

@wojtmic wojtmic merged commit 3cd7cd6 into wojtmic:master Mar 16, 2026
2 checks passed
@jamakoiv jamakoiv deleted the get_tweaks_rewrite branch March 17, 2026 21:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants