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

Revert #5842 #6128

Open
MrAlias opened this issue Sep 19, 2024 · 2 comments
Open

Revert #5842 #6128

MrAlias opened this issue Sep 19, 2024 · 2 comments
Labels
help wanted Extra attention is needed

Comments

@MrAlias
Copy link
Contributor

MrAlias commented Sep 19, 2024

May I ask why the change, approved in #5664, was reverted? Vararg slice was allocated once and used 2/multiple times. Now it is allocated on each Add() call. There is/was even a comment, explaining the "why".

See https://go.dev/ref/spec#Passing_arguments_to_..._parameters

See https://fs.blog/chestertons-fence/

var sink []string

func abc(vals ...string) {
	sink = vals
}

func BenchmarkFuncCall(b *testing.B) {
	b.ReportAllocs()
	b.ResetTimer()
	arg := []string{"a", "b", "c"}
	for i := 0; i < b.N; i++ {
		abc(arg...)
	}
}

gives me

BenchmarkFuncCall-10    	1000000000	         0.9481 ns/op	       0 B/op	       0 allocs/op
var sink []string

func abc(vals ...string) {
	sink = vals
}

func BenchmarkFuncCall(b *testing.B) {
	b.ReportAllocs()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		abc("a", "b", "c")
	}
}

gives me

BenchmarkFuncCall-10    	42442630	        26.69 ns/op	      48 B/op	       1 allocs/op

Put the fence back, please 😁

Originally posted by @ash2k in #5842 (comment)

@XSAM
Copy link
Member

XSAM commented Sep 20, 2024

Should we use a more explainable fence for the comment? Something like Allocate slice once to avoid allocation for the variadic argument.

@MrAlias
Copy link
Contributor Author

MrAlias commented Sep 20, 2024

SGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants