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

perf: Reduce template compilation time by ~16% #206

Merged
merged 6 commits into from
Apr 1, 2022

Conversation

willbeason
Copy link
Member

Remove unnecessarily parsing Rego multiple times. Converting to/from the parsed Rego AST is expensive, so removing this results in a significant speedup in compilation times.

Before:

constraint$ GOMAXPROCS=1 go test ./pkg/client --bench=AddTemplate$/^Complex
goos: linux
goarch: amd64
pkg: github.com/open-policy-agent/frameworks/constraint/pkg/client
cpu: Intel(R) Xeon(R) W-2135 CPU @ 3.70GHz
BenchmarkClient_AddTemplate/Complex/1_Templates         	     776	   1503423 ns/op
BenchmarkClient_AddTemplate/Complex/2_Templates         	     396	   3006789 ns/op
BenchmarkClient_AddTemplate/Complex/5_Templates         	     154	   7659658 ns/op
BenchmarkClient_AddTemplate/Complex/10_Templates        	      76	  15719632 ns/op
BenchmarkClient_AddTemplate/Complex/20_Templates        	      36	  32558251 ns/op
BenchmarkClient_AddTemplate/Complex/50_Templates        	      13	  84629914 ns/op
BenchmarkClient_AddTemplate/Complex/100_Templates       	       6	 173179630 ns/op
BenchmarkClient_AddTemplate/Complex/200_Templates       	       3	 355320224 ns/op
PASS
ok  	github.com/open-policy-agent/frameworks/constraint/pkg/client	12.063s
constraint$ GOMAXPROCS=1 go test ./pkg/client --bench=AddTemplate$/^Complex --benchmem
goos: linux
goarch: amd64
pkg: github.com/open-policy-agent/frameworks/constraint/pkg/client
cpu: Intel(R) Xeon(R) W-2135 CPU @ 3.70GHz
BenchmarkClient_AddTemplate/Complex/1_Templates         	     777	   1502605 ns/op	  546829 B/op	   11315 allocs/op
BenchmarkClient_AddTemplate/Complex/2_Templates         	     385	   3006709 ns/op	 1089453 B/op	   22592 allocs/op
BenchmarkClient_AddTemplate/Complex/5_Templates         	     157	   7581477 ns/op	 2718134 B/op	   56424 allocs/op
BenchmarkClient_AddTemplate/Complex/10_Templates        	      76	  15584239 ns/op	 5441096 B/op	  112825 allocs/op
BenchmarkClient_AddTemplate/Complex/20_Templates        	      37	  31969236 ns/op	10882470 B/op	  225619 allocs/op
BenchmarkClient_AddTemplate/Complex/50_Templates        	      13	  82900329 ns/op	27158004 B/op	  563895 allocs/op
BenchmarkClient_AddTemplate/Complex/100_Templates       	       6	 172543024 ns/op	54392817 B/op	 1127905 allocs/op
BenchmarkClient_AddTemplate/Complex/200_Templates       	       3	 361875473 ns/op	108725650 B/op	 2255664 allocs/op

After:

constraint$ GOMAXPROCS=1 go test ./pkg/client --bench=AddTemplate$/^Complex
goos: linux
goarch: amd64
pkg: github.com/open-policy-agent/frameworks/constraint/pkg/client
cpu: Intel(R) Xeon(R) W-2135 CPU @ 3.70GHz
BenchmarkClient_AddTemplate/Complex/1_Templates         	     910	   1263631 ns/op
BenchmarkClient_AddTemplate/Complex/2_Templates         	     478	   2484350 ns/op
BenchmarkClient_AddTemplate/Complex/5_Templates         	     183	   6491906 ns/op
BenchmarkClient_AddTemplate/Complex/10_Templates        	     100	  13091745 ns/op
BenchmarkClient_AddTemplate/Complex/20_Templates        	      46	  26967734 ns/op
BenchmarkClient_AddTemplate/Complex/50_Templates        	      16	  70383262 ns/op
BenchmarkClient_AddTemplate/Complex/100_Templates       	       7	 144370954 ns/op
BenchmarkClient_AddTemplate/Complex/200_Templates       	       4	 312258998 ns/op
PASS
ok  	github.com/open-policy-agent/frameworks/constraint/pkg/client	12.309s
constraint$ GOMAXPROCS=1 go test ./pkg/client --bench=AddTemplate$/^Complex --benchmem
goos: linux
goarch: amd64
pkg: github.com/open-policy-agent/frameworks/constraint/pkg/client
cpu: Intel(R) Xeon(R) W-2135 CPU @ 3.70GHz
BenchmarkClient_AddTemplate/Complex/1_Templates         	     930	   1278016 ns/op	  455831 B/op	    9200 allocs/op
BenchmarkClient_AddTemplate/Complex/2_Templates         	     466	   2514157 ns/op	  907062 B/op	   18360 allocs/op
BenchmarkClient_AddTemplate/Complex/5_Templates         	     186	   6472272 ns/op	 2263255 B/op	   45847 allocs/op
BenchmarkClient_AddTemplate/Complex/10_Templates        	     100	  13266355 ns/op	 4526045 B/op	   91662 allocs/op
BenchmarkClient_AddTemplate/Complex/20_Templates        	      43	  27157279 ns/op	 9061271 B/op	  183310 allocs/op
BenchmarkClient_AddTemplate/Complex/50_Templates        	      16	  70915101 ns/op	22633622 B/op	  458174 allocs/op
BenchmarkClient_AddTemplate/Complex/100_Templates       	       7	 146138294 ns/op	45285475 B/op	  916346 allocs/op
BenchmarkClient_AddTemplate/Complex/200_Templates       	       4	 303379543 ns/op	90568002 B/op	 1832689 allocs/op

Will Beason added 3 commits March 29, 2022 10:09
Signed-off-by: Will Beason <willbeason@google.com>
Signed-off-by: Will Beason <willbeason@google.com>
Signed-off-by: Will Beason <willbeason@google.com>
@willbeason willbeason self-assigned this Mar 29, 2022
@codecov-commenter
Copy link

codecov-commenter commented Mar 29, 2022

Codecov Report

Merging #206 (2b1fee9) into master (acd7764) will decrease coverage by 0.36%.
The diff coverage is n/a.

@@            Coverage Diff             @@
##           master     #206      +/-   ##
==========================================
- Coverage   46.65%   46.28%   -0.37%     
==========================================
  Files          64       64              
  Lines        2840     2815      -25     
==========================================
- Hits         1325     1303      -22     
  Misses       1295     1295              
+ Partials      220      217       -3     
Flag Coverage Δ
unittests 46.28% <ø> (-0.37%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...eworks/constraint/pkg/regorewriter/regorewriter.go 73.41% <0.00%> (-1.27%) ⬇️
...orks/constraint/pkg/client/drivers/local/driver.go 42.33% <0.00%> (-0.61%) ⬇️
...s/constraint/pkg/client/drivers/local/compilers.go 65.62% <0.00%> (-0.38%) ⬇️
...t/frameworks/constraint/pkg/client/rego_helpers.go
...eworks/constraint/pkg/client/drivers/local/rego.go 50.00% <0.00%> (ø)

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 acd7764...2b1fee9. Read the comment docs.

Signed-off-by: Will Beason <willbeason@google.com>
@willbeason
Copy link
Member Author

Ultimately this is just refactoring - no visible changes to users or consumers of this library.

Signed-off-by: Will Beason <willbeason@google.com>
@willbeason willbeason changed the title perf: Reduce template compilation time by ~20% perf: Reduce template compilation time by ~16% Mar 29, 2022
Copy link
Contributor

@maxsmythe maxsmythe 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
Member

@sozercan sozercan left a comment

Choose a reason for hiding this comment

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

LGTM

Signed-off-by: Will Beason <willbeason@google.com>
@willbeason
Copy link
Member Author

Tested locally via go.work - passes in Gatekeeper.

@willbeason willbeason merged commit b7cc4c7 into open-policy-agent:master Apr 1, 2022
@willbeason willbeason deleted the perf-improvements branch April 1, 2022 16:19
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.

None yet

4 participants