Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Replace SystemStateReader.read() with lru_cache #105

Merged
merged 10 commits into from
May 21, 2024

Conversation

Melkor333
Copy link
Contributor

@Melkor333 Melkor333 commented Apr 12, 2024

Removes the very time intensive SystemStateReader.read() function and use functions that directly return the variables.
Solve the caching Problem with lru_cache, which is cleared with ...clear_cache() whenever a respective Command is run.
Since the Command already has a provides function with known_dependency_types which pretty much exactly resembles the state variables that should be cached, I wrote a small Command.clear_caches() function which clears caches whenever a Command has been run.

I added some provides definitions so that it should work consistently with the cache resetting.

I also split it up into various "easier to understand" feature-separate commits - they should even run through the tests individually :)
You can also read the commit notes to follow my thought process.

FWIW I don't really understand the tests all too well and am not 100% sure I didn't accidentally "break" a test. I'm also not sure if more tests would be necessary to properly test the caching... But it looks very much like my changes are correct.

Copy link
Member

@winged winged left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like where this is going! Just dropping a few hints on how to cleanup things

pyaptly/cli.py Show resolved Hide resolved
pyaptly/snapshot.py Outdated Show resolved Hide resolved
pyaptly/state_reader.py Outdated Show resolved Hide resolved
pyaptly/state_reader.py Outdated Show resolved Hide resolved
In preparation for LRU caching, don't make the reader store the
variables itself. Instead, the functions return the read state
directly.
The now defunct read() function is removed separately
The FunctionCommand was required to reset the
reader, which is not necessary anymore.
These provides are not necessary for dependecy resolving.
But upcoming caching will reuse the provides and therefore require
consistent use of it.
Add an LRU cache to the state_reader so state is only reread when an
actual changing command has been executed.
After command execution the cache of the relevant types is cleared.
@Melkor333
Copy link
Contributor Author

BTW. I did a very quick check to see if the tests are faster....

# With changes
real    0m44.300s
user    0m15.974s
sys     0m11.979s

# Without changes
real    1m37.832s
user    0m34.823s
sys     0m28.877s

But the tests are still requiring internet (the ubuntu keyservers AFAIU) and I only let it run a single time. But if the tests themselves only take half the time, I can imagine a great speed up for complex setups. We'll see...

@Melkor333 Melkor333 marked this pull request as ready for review April 19, 2024 20:18
pyaptly/state_reader.py Outdated Show resolved Hide resolved
pyaptly/state_reader.py Outdated Show resolved Hide resolved
pyaptly/command.py Show resolved Hide resolved
Mirrors are updated ad-hoc. No dependency-management is required.
Therefore no Command is generated and the `mirror` dependency-type
doesn't exist.
Copy link

@rhizoome rhizoome left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!!!!

@rhizoome rhizoome merged commit db67240 into main May 21, 2024
1 check passed
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.

3 participants