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

NewHTTP: No allocations #12

Merged
merged 2 commits into from
Jan 18, 2024

Conversation

mattayes
Copy link

@mattayes mattayes commented Oct 16, 2023

I noticed NewHTTP() is allocating 240B/call. These allocations are:

  1. Creating a new *bytes.Buffer (48B). We used this to get the request/response body size (never read the values) and it's eligible for GC at the end of the function.
  2. Returning a *HTTPPayload (192B)

Falling in the spirit of Zap (zero allocations per op), this PR removes allocations for HTTP:

  • Use io.Discard instead of allocating a buffer.
  • Return HTTPPayload instead of *HTTPPayload.
  • Accept HTTPPayload in HTTP().

This

Running tool: /usr/local/bin/go test -benchmem -run=^$ -coverprofile=/var/folders/sq/bxg3__pn49l78dyg9vgwl9c80000gn/T/vscode-goatP83c/go-code-cover -covermode atomic -bench ^(BenchmarkNewHTTP|BenchmarkNewHTTP_NoBodyAlloc|BenchmarkNewHTTP_NoAllocs)$ go.ajitem.com/zapdriver -race -v

goos: darwin
goarch: amd64
pkg: go.ajitem.com/zapdriver
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
BenchmarkNewHTTP
BenchmarkNewHTTP-12			  914928	      1200 ns/op	     240 B/op	       2 allocs/op
BenchmarkNewHTTP_NoAlloc
BenchmarkNewHTTP_NoAlloc-12          	 1000000	      1102 ns/op	       0 B/op	       0 allocs/op
PASS
coverage: 14.7% of statements
ok  	go.ajitem.com/zapdriver	4.709s
func BenchmarkNewHTTP(b *testing.B) {
	b.ReportAllocs()
	b.RunParallel(func(b *testing.PB) {
		for b.Next() {
			p := NewHTTP(nil, nil)
			_ = HTTP(p)
		}
	}
}

I also made two unrelated (linter) changes:

  • Ran go mod tidy
  • Replaced ioutil with io
  • Made an ignored error explicit.

@mattayes mattayes changed the title Make HTTPPayload and associated methods zero-allocation NewHTTP: Use io.Discard, reduce allocations Oct 16, 2023
@mattayes mattayes changed the title NewHTTP: Use io.Discard, reduce allocations NewHTTP: Use io.Discard, reduce allocations/op 20% Oct 16, 2023
@mattayes mattayes changed the title NewHTTP: Use io.Discard, reduce allocations/op 20% NewHTTP: Use io.Discard, reduce allocations/op by 20% Oct 16, 2023
@mattayes mattayes changed the title NewHTTP: Use io.Discard, reduce allocations/op by 20% NewHTTP: No allocations Oct 17, 2023
@mattayes
Copy link
Author

@asahasrabuddhe thoughts?

@mattayes
Copy link
Author

@asahasrabuddhe bump

@mattayes
Copy link
Author

mattayes commented Nov 2, 2023

@asahasrabuddhe are you no longer maintaining the project?

@asahasrabuddhe
Copy link
Owner

@mattayes this looks great

@asahasrabuddhe asahasrabuddhe merged commit 1861acc into asahasrabuddhe:master Jan 18, 2024
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.

2 participants