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

Refactor trailer Field for Improved Memory Efficiency and Performance #1928

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

Conversation

ksw2000
Copy link
Contributor

@ksw2000 ksw2000 commented Dec 19, 2024

The original trailer field was represented by []argsKV, where argsKV consisted of two []byte slices and one bool, totaling 56 bytes. However, maintaining each element required only a single 24-byte slice.

After refactoring, there was a significant reduction in memory usage.

I designed two benchmarks to compare the performance between the refactored code and the original implementation.

  • BenchmarkSetTrailerBytes primarily measures the performance of the AddTrailerBytes function.
  • BenchmarkSetTrailerBytesWithReset evaluates the efficiency of reusing memory by repeatedly adding trailers and resetting.
func BenchmarkSetTrailerBytes(b *testing.B) {
	list := make([][]byte, 5)
	for i := range list {
		list[i] = []byte(fmt.Sprintf("trailer-%d", i))
	}
	for i := 0; i < b.N; i++ {
		header := RequestHeader{}
		for j := range list {
			header.AddTrailerBytes(list[j])
		}
	}
}

func BenchmarkSetTrailerBytesWithReset(b *testing.B) {
	list := make([][]byte, 5)
	for i := range list {
		list[i] = []byte(fmt.Sprintf("trailer-%d", i))
	}
	header := RequestHeader{}
	for i := 0; i < b.N; i++ {
		for j := range list {
			header.AddTrailerBytes(list[j])
		}
		header.Reset()
	}
}
goos: linux
goarch: amd64
pkg: github.com/valyala/fasthttp
cpu: Intel(R) Xeon(R) CPU E5-2680 v4 @ 2.40GHz
                           │   old.txt   │           new.txt           │
                           │   sec/op    │   sec/op     vs base        │
SetTrailerBytes-8            966.6n ± 1%   756.6n ± 1%  -21.72% (n=50)
SetTrailerBytesWithReset-8   202.2n ± 0%   176.0n ± 1%  -12.93% (n=50)
geomean                      442.1n        365.0n       -17.44%

                           │   old.txt    │               new.txt                │
                           │     B/op     │    B/op     vs base                  │
SetTrailerBytes-8            944.0 ± 0%     456.0 ± 0%  -51.69% (n=50)
SetTrailerBytesWithReset-8   0.000 ± 0%     0.000 ± 0%        ~ (p=1.000 n=50) ¹
geomean                                 ²               -30.50%                ²
¹ all samples are equal
² summaries must be >0 to compute geomean

                           │   old.txt    │               new.txt               │
                           │  allocs/op   │ allocs/op   vs base                 │
SetTrailerBytes-8            10.00 ± 0%     10.00 ± 0%       ~ (p=1.000 n=50) ¹
SetTrailerBytesWithReset-8   0.000 ± 0%     0.000 ± 0%       ~ (p=1.000 n=50) ¹
geomean                                 ²               +0.00%                ²
¹ all samples are equal
² summaries must be >0 to compute geomean

The refactoring of the trailer field from []argsKV to a single slice significantly reduced memory usage up to 51.69% and improved execution time up to 21.72%.

@ksw2000 ksw2000 marked this pull request as ready for review December 19, 2024 06:00
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