-
Notifications
You must be signed in to change notification settings - Fork 251
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
Make SecureSecurityPolicyConfig significantly faster #506
Conversation
Previously this class had a large number of instance variables being used as storage, however they were almost entirely being accessed in a hash-like way using send and instance_variable_set, both of which are slower than just using a hash. By just using a hash we make this simpler and faster. We can also keep the hash sparse rather than using `nil` as a "not set" sentinel value.
These were only used in one place. We can avoid complexity by not defining them.
For some reason Rubocop doesn't like the case equality operator (it's good) so we can just use a case statement I guess.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @jhawthorn, thanks so much for this PR. We were able to test and verify this change in the github project, feel free to merge. We have another change we'd like to add so you don't need to cut a release, our team will cut the release once the change is added. In favor of making the smallest possible change, our team will cut a separate release for this PR once you have merged the PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have been seeing this gem a lot in profiles. Must of this slowness seems to come from overuse of instance variables in
DynamicConfig
and attempting to use them basically as a hash (which we can do much faster with a hash 😅)The first commit of these is the most important, but the other two also significantly speed things up.
There is definitely more improvement available here, we seem to be overly cautious in duplicating arrays, and we also seem to convert unnecessarily between hashes and the config object, but I think this is the best place to start.
Benchmark:
Before:
After:
to_h
is 10x faster,append
is 2x faster, and .value (which was not the target of these optimizations but I didn't want to see it regress) is slightly faster