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

getSnippetHash: Use regexp instead of parsing whole AST #759

Merged
merged 2 commits into from
Sep 19, 2022

Conversation

julienduchesne
Copy link
Member

@julienduchesne julienduchesne commented Sep 16, 2022

When calculating the env hash to use as cache keys, we don't need an exact list of imports
Therefore, to improve performance, we can replace the current strategy of using the AST by a regexp strategy.
This new strategy can lead to false positives (e.g. a string containing import 'foo') but this is not an issue for the hashing function.
All that matters is that the hash is consistent and accounts for all files (no false negatives)

On actual huge real-life environments, I have seen improvements of up to 95% (from 2s to 100ms)

When calculating the env hash to use as cache keys, we don't need an exact list of imports
Therefore, to improve performance, we can replace the current strategy of using the AST by a regexp strategy.
This new strategy can lead to false positives (e.g. a string containing import 'foo') but this is not an issue for the hashing function. All that matters is that the hash is consistent and accounts for all files (no false negatives)
@github-actions
Copy link

github-actions bot commented Sep 16, 2022

Benchstat (compared to main):

name              old time/op    new time/op    delta
GetSnippetHash-2    40.7ms ± 4%    20.4ms ± 5%  -49.88%  (p=0.000 n=10+10)

name              old alloc/op   new alloc/op   delta
GetSnippetHash-2    14.1MB ± 2%     3.9MB ± 0%  -72.74%  (p=0.000 n=10+10)

name              old allocs/op  new allocs/op  delta
GetSnippetHash-2      153k ± 1%       32k ± 0%  -78.94%  (p=0.000 n=10+10)

@julienduchesne julienduchesne marked this pull request as ready for review September 16, 2022 20:02
Copy link
Member

@Duologic Duologic left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@inkel inkel left a comment

Choose a reason for hiding this comment

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

Left a simple question. Overall LGTM. The performance improvements are amazing! I'm pretty sure the speed up is because of the reduction in the number of allocations, though I could be wrong.

pkg/jsonnet/imports.go Outdated Show resolved Hide resolved
@julienduchesne
Copy link
Member Author

Left a simple question. Overall LGTM. The performance improvements are amazing! I'm pretty sure the speed up is because of the reduction in the number of allocations, though I could be wrong.

Yeah, the issue with the AST is that you have to parse all of it and traverse all nodes

@julienduchesne julienduchesne merged commit 0aba526 into main Sep 19, 2022
@julienduchesne julienduchesne deleted the julienduchesne/get-snippet-hash branch September 19, 2022 12:14
julienduchesne added a commit that referenced this pull request Sep 27, 2022
Made a stupid mistake in the previous PR: #759

This fixes it and adds another benchmark test to ensure it doesn't happen again.
I also removed the Github Actions benchmark test, as it's not really useful, anytime we change the tests, we'll get erroneous results which will be annoying.
Instead, I added the benchmark tests to the Drone run, we can compare whenever we want.
julienduchesne added a commit that referenced this pull request Sep 28, 2022
* Fix `getSnippetHash` not considering all files
Made a stupid mistake in the previous PR: #759

This fixes it and adds another benchmark test to ensure it doesn't happen again.
I also removed the Github Actions benchmark test, as it's not really useful, anytime we change the tests, we'll get erroneous results which will be annoying.
Instead, I added the benchmark tests to the Drone run, we can compare whenever we want.

* linting

* Add changelog, will release straight away
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