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

improvement: Use LRU instead of maps on defaultStore #780

Closed

Conversation

ajnavarro
Copy link
Contributor

Description

Changing actual cache implementation to use an LRU cache improves resource usage and avoids exhaustion.

@ajnavarro ajnavarro requested a review from a team as a code owner April 26, 2023 08:38
@tbruyelle
Copy link
Contributor

tbruyelle commented Apr 26, 2023

Can you explain why maps are exhausting plz ?

EDIT: forget that, I didn't notice it was for caching.

@moul
Copy link
Member

moul commented Apr 26, 2023

@ajnavarro, thank you, can you provide some metrics, i.e., benchmark before/after?

Warn: new dependency, to be reviewed.

cc @peter7891 good example of candidate for the performance management framework.

@moul moul requested review from peter7891 and moul April 26, 2023 08:50
go.mod Outdated Show resolved Hide resolved
"github.com/gnolang/gno/tm2/pkg/amino"
"github.com/gnolang/gno/tm2/pkg/std"
"github.com/gnolang/gno/tm2/pkg/store"
)

const iavlCacheSize = 1024 * 1024 // TODO increase and parameterize.
const objectCacheSize = 100 // TODO parameterize.
const typeCacheSize = 1000 // TODO parameterize.
const nodeCacheSize = 10000 // TODO parameterize.
Copy link
Contributor

Choose a reason for hiding this comment

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

How did you evaluate those values ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Executing a stress test using supernova. Ideally, we should define a total max memory available for store cache.

Changing actual cache implementation to use an LRU cache improves resource usage and avoids exhaustion.

Signed-off-by: Antonio Navarro Perez <antnavper@gmail.com>
@ajnavarro ajnavarro force-pushed the improvement/lru-cache-on-store branch from d56846a to 7c9a8df Compare April 26, 2023 10:52
@ajnavarro
Copy link
Contributor Author

@ajnavarro, thank you, can you provide some metrics, i.e., benchmark before/after?

Warn: new dependency, to be reviewed.

cc @peter7891 good example of candidate for the performance management framework.

I can create some benchmarks measuring the total memory used.

@ajnavarro
Copy link
Contributor Author

@moul @tbruyelle I think I found a bug. After limiting the number of objects allowed to be on memory, some tests started to fail saying that a package is missing from the store (it should be there if it was on the cache before). That might be considered a bug. WDYT?

@tbruyelle
Copy link
Contributor

@moul @tbruyelle I think I found a bug. After limiting the number of objects allowed to be on memory, some tests started to fail saying that a package is missing from the store (it should be there if it was on the cache before). That might be considered a bug. WDYT?

Sure, what is the error exactly ?

@ajnavarro
Copy link
Contributor Author

ajnavarro commented Apr 26, 2023

I found the problem. The store interface has a SetCachePackage method that is only intended to temporally store the package. Before that change, it was stored in memory with no limits, adding the possibility of an OOM error. With this change, we are evicting less used objects, so after 100 packages are stored in memory we remove the less used ones, causing tests to fail because the temporal package was evicted.

IMHO a better solution would be to store these temporal packages on a persistent storage that can be deleted eventually.

@tbruyelle
Copy link
Contributor

OK so to be sure I understand well, some packages (I assume the ones added via SetCachePackage()) aren't retrievable from the store via loadObjectSafe() right ?

@ajnavarro
Copy link
Contributor Author

Hmm, loadObjectSafe() was never able to obtain packages stored using the SetCachePackage() method.

The problem with the actual change is that SetCachePackage is using an in-memory cache that was not expecting to be evicted in any case, but with that change adding an LRU cache we are discarding less used packages from memory.

GetPackage is looking for packages from memory and from other two sources if they are present: baseStore and pkgGetter. With this change, we are removing some packages from memory, so it is not able to find them.

@tbruyelle
Copy link
Contributor

OK that's what I understood, the packages that are manually put in cache via SetCachePackage, are not retrievable from the store or from the pkgGetter. Maybe the solution is to understand why they aren't retrievable from these sources, and then make sure they will be (via addionnal code in SetCachePackage maybe?).

@zivkovicmilos zivkovicmilos marked this pull request as draft April 27, 2023 11:46
@ajnavarro
Copy link
Contributor Author

I asked @zivkovicmilos to make this PR a draft because I'll need more time to think about throwaway packages.

@ajnavarro
Copy link
Contributor Author

ajnavarro commented May 9, 2023

Closing in favor of #812 to try to define the best solution.

@ajnavarro ajnavarro closed this May 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants