Skip to content

Conversation

ksw2000
Copy link
Contributor

@ksw2000 ksw2000 commented Jan 28, 2025

The original delAllArgs method is stable but requires more copying operations to maintain the order of elements.

  • Renamed the original delAllArgs method to delAllArgsStable to maintain stable behavior.
  • Added a new delAllArgs method for non-stable functionality, improving runtime efficiency.

Depending on the specific use case:

  • Use delAllArgsStable when stability (order preservation) is required.
  • Use delAllArgs when stability is not necessary.

In the case of handling headers in h.h, the order of headers is not important. Refer to net/http header.go, where the Del function calls textproto.MIMEHeader.Del. This function is implemented using a map, and the map delete operation is inherently unstable.

Benchmark compare delAllArgsStable and delAllArgs

func BenchmarkArgsDel(b *testing.B) {
	for i := 0; i < b.N; i++ {
		b.StopTimer()
		var a []argsKV
		for j := 1; j < 10; j++ {
			a = appendArg(a, fmt.Sprintf("item%d", j), "val", argsHasValue)
		}
		for j := 1; j < 10; j++ {
			a = appendArg(a, fmt.Sprintf("item%d", j), "val", argsHasValue)
		}
		b.StartTimer()
		for j := 1; j < 10; j++ {
			a = delAllArgs(a, fmt.Sprintf("item%d", j))
		}
	}
}

func BenchmarkArgsDelStable(b *testing.B) {
	for i := 0; i < b.N; i++ {
		b.StopTimer()
		var a []argsKV
		for j := 1; j < 10; j++ {
			a = appendArg(a, fmt.Sprintf("item%d", j), "val", argsHasValue)
		}
		for j := 1; j < 10; j++ {
			a = appendArg(a, fmt.Sprintf("item%d", j), "val", argsHasValue)
		}
		b.StartTimer()
		for j := 1; j < 10; j++ {
			a = delAllArgsStable(a, fmt.Sprintf("item%d", j))
		}
	}
}

Benchmark result:

$ go test --run=^$ --bench BenchmarkArgsDel -benchtime=100000x
goos: linux
goarch: amd64
pkg: github.com/valyala/fasthttp
cpu: AMD EPYC 7763 64-Core Processor                
BenchmarkArgsDel-4                100000              2072 ns/op
BenchmarkArgsDelStable-4          100000              2370 ns/op
PASS
ok      github.com/valyala/fasthttp     8.312s

- Renamed the original `delAllArgs` method to `delAllArgsStable` to maintain stable behavior.
- Added a new `delAllArgs` method for non-stable functionality, improving runtime efficiency.
@ksw2000 ksw2000 marked this pull request as ready for review January 28, 2025 07:33
@ksw2000 ksw2000 changed the title Improve: split delAllArgs into delAllArgs and delAllArgsStable perf: split delAllArgs into delAllArgs and delAllArgsStable Feb 18, 2025
@ksw2000
Copy link
Contributor Author

ksw2000 commented Feb 18, 2025

@erikdubbelboer It's been 3 weeks, what do you think?

@erikdubbelboer erikdubbelboer merged commit b59f47e into valyala:master Feb 19, 2025
15 checks passed
@erikdubbelboer
Copy link
Collaborator

Thanks I somehow missed it.

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