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

fix: avoid excessive memory allocations #2389

Merged
merged 6 commits into from
Apr 15, 2022

Conversation

harnash
Copy link
Contributor

@harnash harnash commented Apr 11, 2022

This PR changes internal storage for HIBP API call results to use ristretto cache instead of plain map to avoid excessive memory allocations when handling substantial load (each checked hash is stored indefinitely in memory).

Related issue(s)

Checklist

  • I have read the contributing guidelines.
  • I have referenced an issue containing the design document if my change
    introduces a new feature.
  • I am following the
    contributing code guidelines.
  • I have read the security policy.
  • I confirm that this pull request does not address a security
    vulnerability. If this pull request addresses a security. vulnerability, I
    confirm that I got green light (please contact
    [email protected]) from the maintainers to push
    the changes.
  • I have added tests that prove my fix is effective or that my feature
    works.
  • I have added or changed the documentation.

Further Comments

This patch was running for a few days on our cluster and it looks like memory usage is not increasing and stabilised around 70MB which is great :-)

@harnash harnash force-pushed the avoid_excessive_memory_allocations branch from 49cf236 to c55e16d Compare April 11, 2022 20:40
Copy link
Member

@aeneasr aeneasr left a comment

Choose a reason for hiding this comment

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

Awesome, thank you for your contribution! This looks pretty good and I have some ideas how to improve it further :)

cache, err := ristretto.NewCache(&ristretto.Config{
NumCounters: 10 * 10000,
MaxCost: 60 * 10000, // BCrypt hash size is 60 bytes
BufferItems: 64,
Copy link
Member

Choose a reason for hiding this comment

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

The cost in ristretto is set is kinda weird. For example, it is probably required to ignore the internal cost and use a custom cost function:

https://github.com/dgraph-io/ristretto/blob/8e850b710d6df0383c375ec6a7beae4ce48fc8d5/cache.go#L137

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alright. I've added ignoreInternalCost=true but I'm not sure if custom cost function is needed at this point.

@harnash harnash force-pushed the avoid_excessive_memory_allocations branch from c55e16d to a012ef2 Compare April 12, 2022 05:53
@codecov
Copy link

codecov bot commented Apr 12, 2022

Codecov Report

Merging #2389 (1f2c9e1) into master (1fbabc2) will decrease coverage by 0.03%.
The diff coverage is 80.00%.

@@            Coverage Diff             @@
##           master    #2389      +/-   ##
==========================================
- Coverage   76.60%   76.56%   -0.04%     
==========================================
  Files         315      315              
  Lines       17328    17326       -2     
==========================================
- Hits        13274    13266       -8     
- Misses       3120     3124       +4     
- Partials      934      936       +2     
Impacted Files Coverage Δ
driver/registry_default.go 86.33% <50.00%> (ø)
selfservice/strategy/password/validator.go 92.50% <87.50%> (-0.19%) ⬇️
persistence/sql/persister_courier.go 85.00% <0.00%> (-3.34%) ⬇️
session/test/persistence.go 98.61% <0.00%> (-1.39%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1fbabc2...1f2c9e1. Read the comment docs.

@aeneasr aeneasr merged commit ee2d410 into ory:master Apr 15, 2022
@harnash harnash deleted the avoid_excessive_memory_allocations branch April 19, 2022 07:53
peturgeorgievv pushed a commit to senteca/kratos-fork that referenced this pull request Jun 30, 2023
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