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

optimisation: pre-allocate, and fewer slices during summarisation #3002

Merged
merged 1 commit into from
Dec 25, 2017

Conversation

rade
Copy link
Member

@rade rade commented Dec 25, 2017

Pre-allocating slices really pays dividends when we create loads of them and they don't grow very large.

Also, a bunch of code in templates/tables used slices as Maybes, i.e. returning nil or a single-element slice, which is horrendously inefficient.

@rade rade requested a review from bboreham December 25, 2017 10:05
@rade
Copy link
Member Author

rade commented Dec 25, 2017

Benchmarks from running

go test -tags 'netgo unsafe' -run=x -cpu=2 -benchmem -bench='Summarize' --bench-report-path=/tmp/dev-on-dev-report.json

master

BenchmarkSummarizeHosts-2         	   10000	    158150 ns/op	   49524 B/op	     394 allocs/op
BenchmarkSummarizeControllers-2   	    2000	   1061920 ns/op	  348425 B/op	    3447 allocs/op
BenchmarkSummarizePods-2          	     500	   2471461 ns/op	  828257 B/op	    7548 allocs/op
BenchmarkSummarizeContainers-2    	     100	  30202501 ns/op	11220388 B/op	   84016 allocs/op
BenchmarkSummarizeProcesses-2     	     500	   2716148 ns/op	  881743 B/op	    7866 allocs/op

branch

BenchmarkSummarizeHosts-2         	   20000	     90850 ns/op	   25909 B/op	     205 allocs/op
BenchmarkSummarizeControllers-2   	    3000	    594095 ns/op	  199850 B/op	    2009 allocs/op
BenchmarkSummarizePods-2          	     500	   2076888 ns/op	  579624 B/op	    5468 allocs/op
BenchmarkSummarizeContainers-2    	     100	  27914949 ns/op	 9750272 B/op	   73029 allocs/op
BenchmarkSummarizeProcesses-2     	    1000	   2232948 ns/op	  556408 B/op	    4990 allocs/op

Pre-allocating slices really pays dividends when we create loads of
them and they don't grow very large.

We take special care to return nil rather than 0-length slices. This
a) saves further on allocation, and b) is required for some crude
tests to pass that match on nil rather than length.

Also, a bunch of code in templates/tables used slices as Maybe's,
i.e. returning nil or a single-element slice, which is horrendously
inefficient. Eliminating these saves a lot of allocations.
@rade rade force-pushed the summarize-preallocate branch from f75781e to 3de0468 Compare December 25, 2017 12:37
@rade rade merged commit 50e7010 into master Dec 25, 2017
@rade
Copy link
Member Author

rade commented Dec 25, 2017

I've merged this since it's all very straightforward.

@rade rade deleted the summarize-preallocate branch December 25, 2017 13:14
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.

1 participant