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: add caching to koanf config to fix performance regression #1042

Merged

Conversation

David-Wobrock
Copy link
Contributor

@David-Wobrock David-Wobrock commented Dec 17, 2022

Fix #1033 of a performance regression in v0.40.0.
It seems linked to a perpetual config reloading, leading to a lot of JSON marshalling being CPU intensive.
Adding back in-memory caching, as we used to have for Viper configuration.

Related issue(s)

Fixes #1033

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 the approval (please contact
    security@ory.sh) 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

@David-Wobrock David-Wobrock force-pushed the issue-1033-config-performance-regression branch from 9e2ece5 to cc501c9 Compare December 17, 2022 09:44
@David-Wobrock David-Wobrock force-pushed the issue-1033-config-performance-regression branch from cc501c9 to 75a7089 Compare December 17, 2022 10:09
@codecov
Copy link

codecov bot commented Dec 17, 2022

Codecov Report

Merging #1042 (77d5b4e) into master (878089d) will increase coverage by 0.46%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master    #1042      +/-   ##
==========================================
+ Coverage   77.74%   78.20%   +0.46%     
==========================================
  Files          83       83              
  Lines        3841     4006     +165     
==========================================
+ Hits         2986     3133     +147     
- Misses        578      594      +16     
- Partials      277      279       +2     
Impacted Files Coverage Δ
driver/configuration/provider_koanf.go 87.34% <100.00%> (+1.29%) ⬆️
driver/registry_memory.go 89.82% <0.00%> (-3.93%) ⬇️
internal/cloudstorage/useragent.go 100.00% <0.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@David-Wobrock David-Wobrock marked this pull request as ready for review December 17, 2022 10:19
@David-Wobrock David-Wobrock force-pushed the issue-1033-config-performance-regression branch 2 times, most recently from 5a25bba to 4ce1fe6 Compare January 4, 2023 07:51
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.

Thank you very much! I think there's an improvement to this that addresses the problem in a more targeted way and without sideffects :)

driver/configuration/provider_koanf.go Outdated Show resolved Hide resolved
driver/configuration/provider_koanf.go Outdated Show resolved Hide resolved
@@ -306,7 +346,11 @@ func (v *KoanfProvider) PipelineConfig(prefix, id string, override json.RawMessa
return errors.WithStack(err)
}
Copy link
Member

Choose a reason for hiding this comment

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

The culprit of the high CPU usage is in this method. My recommendation would be to cache the result of the validation (only when successful) and the cache key sha256(prefix + "|" + id + "|" + marshalled). That way we will respect hot reloading (because we use the config values) and still save a lot of CPU cycles!

@David-Wobrock David-Wobrock force-pushed the issue-1033-config-performance-regression branch 6 times, most recently from 49075d7 to d251a7d Compare January 28, 2023 16:02
@David-Wobrock
Copy link
Contributor Author

Hey @aeneasr
Thanks for the review! Good catch, we can reduce the caching range, and avoid side-effects 👍 I wasn't aware of the hot-reloading part.

I pushed edits to the PR.

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, looking much better! just a commend on how to improve the ristretto config

driver/configuration/provider_koanf.go Outdated Show resolved Hide resolved
@David-Wobrock David-Wobrock force-pushed the issue-1033-config-performance-regression branch from d251a7d to 77d5b4e Compare January 30, 2023 11:09
@David-Wobrock
Copy link
Contributor Author

Thanks for the configuration @aeneasr 👍
I updated the commit with the suggestion :)

@aeneasr aeneasr merged commit e7fb605 into ory:master Jan 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.

Performance/CPU usage regression in v0.40.0
2 participants