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: use buf in Args instead of bytebufferpool #1931

Merged
merged 1 commit into from
Jan 2, 2025

Conversation

ksw2000
Copy link
Contributor

@ksw2000 ksw2000 commented Jan 2, 2025

Resolve #1856

Since Args already contains a buf field, and multiple functions are designed to operate based on it, using the buf in Args results in a shorter process compared to using bytebufferpool. Additionally, based on the usage scenario, the number of calls to SetUint is unlikely to exceed 10, or even 100. Under such circumstances, using buf is significantly more efficient than bytebufferpool.

Benchmark

Call SetUint multiple times with the same key to compare the performance of the AppendUint step.

func BenchmarkSetUint1(b *testing.B) {
	benchmarkSetUint(b, 1)
}

func BenchmarkSetUint10(b *testing.B) {
	benchmarkSetUint(b, 10)
}

func BenchmarkSetUint100(b *testing.B) {
	benchmarkSetUint(b, 100)
}

func benchmarkSetUint(b *testing.B, n int) {
	data := make([]int, n)

	for i := range data {
		data[i] = i
	}

	args := AcquireArgs()
	for i := 0; i < b.N; i++ {
		for j := range data {
			args.SetUint("key", data[j])
		}
		args.Reset()
	}
	ReleaseArgs(args)
}

Result

goos: linux
goarch: amd64
pkg: github.com/valyala/fasthttp
cpu: AMD EPYC 7763 64-Core Processor                
             │  old.txt    │              new.txt                │
             │   sec/op    │   sec/op     vs base                │
SetUint1-4     34.16n ± 3%   17.74n ± 1%  -48.06% (p=0.000 n=20)
SetUint10-4    336.0n ± 1%   169.0n ± 3%  -49.68% (p=0.000 n=20)
SetUint100-4   3.288µ ± 1%   1.640µ ± 1%  -50.13% (p=0.000 n=20)
geomean        335.4n        170.1n       -49.30%
…             │   old.txt    │              new.txt                │
             │  allocs/op   │ allocs/op   vs base                 │
SetUint1-4     0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=20) ¹
SetUint10-4    0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=20) ¹
SetUint100-4   0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=20) ¹
geomean                   ²               +0.00%                ²
¹ all samples are equal
² summaries must be >0 to compute geomean

The results show that the implementation using buf is approximately 50% faster than using bytebufferpool.

@ksw2000 ksw2000 changed the title refactor: use buf in Args instead of bytebufferpool perf: use buf in Args instead of bytebufferpool Jan 2, 2025
@ksw2000 ksw2000 marked this pull request as ready for review January 2, 2025 02:55
@ksw2000 ksw2000 marked this pull request as draft January 2, 2025 04:11
@ksw2000 ksw2000 marked this pull request as ready for review January 2, 2025 05:41
@erikdubbelboer erikdubbelboer merged commit 2dfdfd8 into valyala:master Jan 2, 2025
14 of 15 checks passed
@erikdubbelboer
Copy link
Collaborator

Thanks!

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.

Deprecate usage of valyala/bytebufferpool
2 participants