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

Minimize allocations in strutil #19

Merged
merged 2 commits into from
Nov 22, 2021
Merged

Minimize allocations in strutil #19

merged 2 commits into from
Nov 22, 2021

Conversation

sethvargo
Copy link
Contributor

I noticed some pretty quick-and-easy wins for performance in the strutil package.

  1. Use struct{} instead of bool for map membership. The empty struct does not allocate and consumes no memory, so it's far more efficient than a boolean:

    package main
    
    import (
    	"fmt"
    	"unsafe"
    )
    
    func main() {
    	x := [100]struct{}{}
    	y := [100]bool{}
    
    	fmt.Printf("%v\n", unsafe.Sizeof(x)) // 0
    	fmt.Printf("%v\n", unsafe.Sizeof(y)) // 100
    }
  2. Pre-allocate slices and maps when we know the maximum possible size.

  3. Remove an unnecessary test dependency and rename the tests to match Go's expected test format name.

Benchmarks

Before:

BenchmarkRemoveDuplicates-16          	       2	 756790682 ns/op	106821536 B/op	   38192 allocs/op
BenchmarkRemoveDuplicatesStable-16    	       6	 183990038 ns/op	58343824 B/op	       4 allocs/op
BenchmarkEquivalentSlices-16          	       1	1666107197 ns/op	358676792 B/op	   76680 allocs/op

After:

BenchmarkRemoveDuplicates-16          	       3	 477817660 ns/op	56117237 B/op	       7 allocs/op
BenchmarkRemoveDuplicatesStable-16    	       6	 200917214 ns/op	56115224 B/op	       3 allocs/op
BenchmarkEquivalentSlices-16          	       1	1009316412 ns/op	112230496 B/op	       8 allocs/op

@sethvargo
Copy link
Contributor Author

Pokey @jefferai @calvn

@jefferai
Copy link
Member

I'm on vacation :-)

@jefferai jefferai merged commit f5abcc3 into hashicorp:main Nov 22, 2021
@jefferai
Copy link
Member

Thanks!

@sethvargo sethvargo deleted the sethvargo/alloc branch November 22, 2021 20:13
@sethvargo
Copy link
Contributor Author

Thank you 😄

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.

None yet

2 participants