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

Reduce memory allocations #18

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

Conversation

nineinchnick
Copy link
Member

Greatly reduce memory allocations. Unfortunately, CPU usage is only slightly lower.

Summary of changes:

  • use sync.Pool for Values and free them after done drawing, headers are not freed
  • reuse buffers for Scan(), but the row slice of []*Value is not yet reused/pooled
  • cache rowStyles, especially because there's a custom function to avoid calling bytes.Repeat for fillers, fillers' capacity now expands and is reused
  • reuse the default JSON encoder with a buffer

Note that the (recently implemented) TemplateEncoder doesn't free Values at all, because it's sending rows over a channel and it doesn't know when the template executor is done processing them. I tried to leverage the fact that the channel is non-buffered, but couldn't make it work.

Before:

% go test -bench=. -benchmem -memprofile mem.prof -cpuprofile cpu.prof -run Aaa
goos: linux
goarch: amd64
pkg: github.com/xo/tblfmt
cpu: Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz
BenchmarkEncodeFormats/aligned-4         	    108	 10625854 ns/op	2219288 B/op	  52203 allocs/op
BenchmarkEncodeFormats/aligned-batch10-4 	    108	 11112151 ns/op	2205421 B/op	  54066 allocs/op
BenchmarkEncodeFormats/aligned-batch100-4         	    108	 10678294 ns/op	2187920 B/op	  52359 allocs/op
BenchmarkEncodeFormats/json-4                     	    171	  6709710 ns/op	1297701 B/op	  28267 allocs/op
BenchmarkEncodeFormats/csv-4                      	    122	  9492879 ns/op	1779549 B/op	  39533 allocs/op
BenchmarkEncodeFormats/template-asciidoc-4        	     64	 17672476 ns/op	2733557 B/op	  61512 allocs/op
BenchmarkEncodeFormats/template-html-4            	     24	 48334576 ns/op	5860730 B/op	 189299 allocs/op
PASS
ok  	github.com/xo/tblfmt	13.316s
go test -bench=. -benchmem -memprofile mem.prof -cpuprofile cpu.prof -run Aaa  13.39s user 1.40s system 106% cpu 13.818 total

After:

% go test -bench=. -benchmem -memprofile mem.prof -cpuprofile cpu.prof -run Aaa
goos: linux
goarch: amd64
pkg: github.com/xo/tblfmt
cpu: Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz
BenchmarkEncodeFormats/aligned-4         	    103	 10761363 ns/op	1882749 B/op	  36154 allocs/op
BenchmarkEncodeFormats/aligned-batch10-4 	    133	  8782258 ns/op	 608893 B/op	  12403 allocs/op
BenchmarkEncodeFormats/aligned-batch100-4         	    128	  8861259 ns/op	 844083 B/op	  14838 allocs/op
BenchmarkEncodeFormats/json-4                     	    211	  5492458 ns/op	 201296 B/op	   4081 allocs/op
BenchmarkEncodeFormats/csv-4                      	    154	  7600791 ns/op	 366642 B/op	   9617 allocs/op
BenchmarkEncodeFormats/template-asciidoc-4        	     60	 18153671 ns/op	2548155 B/op	  59428 allocs/op
BenchmarkEncodeFormats/template-html-4            	     25	 53138683 ns/op	5673048 B/op	 187153 allocs/op
PASS
ok  	github.com/xo/tblfmt	12.839s
go test -bench=. -benchmem -memprofile mem.prof -cpuprofile cpu.prof -run Aaa  12.74s user 0.93s system 103% cpu 13.168 total

Closes xo/usql#144

@nineinchnick nineinchnick changed the title Buffer Reduce memory allocations Mar 21, 2021
@nineinchnick nineinchnick marked this pull request as draft March 22, 2021 08:22
util.go Outdated Show resolved Hide resolved
encode.go Outdated Show resolved Hide resolved
fmt.go Outdated Show resolved Hide resolved
@kenshaw
Copy link
Member

kenshaw commented Mar 28, 2021

I see that you're working on this. Please hold off on merging this to master for now. I'm almost done with the changes, and this will very likely create conflicts. I'll work with you after the other changes I've made are in master. Apologies for this.

@nineinchnick
Copy link
Member Author

No problem, I don't mind resolving conflicts. As this PR has some significant changes, I wouldn't have merged it without review anyway.

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.

Convert to a shared buffer pool for table output
2 participants