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

[chore] primitive slice, avoid allocation if enough space #6061

Merged
merged 1 commit into from
Sep 13, 2022

Conversation

bogdandrutu
Copy link
Member

Signed-off-by: Bogdan [email protected]

@bogdandrutu bogdandrutu requested review from a team and mx-psi September 13, 2022 18:27
@bogdandrutu
Copy link
Member Author

I created some benchmarks to prove that append is as fast as copy, so log them here for others to see them:

func BenchmarkArrayAppend(b *testing.B) {
	arr := []int64{1, 2, 3, 5, 6, 7, 8}
	b.ReportAllocs()
	for n := 0; n < b.N; n++ {
		cpArr := getArray(n)
		arr = arr[:0]
		arr = append(arr, cpArr...)
		if len(arr) != len(cpArr) {
			b.Fail()
		}
	}
}

func BenchmarkArrayCopy(b *testing.B) {
	arr := []int64{1, 2, 3, 5, 6, 7, 8}
	b.ReportAllocs()
	for n := 0; n < b.N; n++ {
		cpArr := getArray(n)
		if cap(arr) >= len(cpArr) {
			arr = arr[:len(cpArr)]
		} else {
			arr = make([]int64, len(cpArr))
		}
		copy(arr, cpArr)
		if len(arr) != len(cpArr) {
			b.Fail()
		}
	}
}

func BenchmarkArrayAppendNil(b *testing.B) {
	b.ReportAllocs()
	for n := 0; n < b.N; n++ {
		arr := []int64(nil)
		cpArr := getArray(n)
		arr = append(arr, cpArr...)
		if len(arr) != len(cpArr) {
			b.Fail()
		}
	}
}

func BenchmarkArrayCopyNil(b *testing.B) {
	b.ReportAllocs()
	for n := 0; n < b.N; n++ {
		arr := []int64(nil)
		cpArr := getArray(n)
		if cap(arr) >= len(cpArr) {
			arr = arr[:len(cpArr)]
		} else {
			arr = make([]int64, len(cpArr))
		}
		copy(arr, cpArr)
		if len(arr) != len(cpArr) {
			b.Fail()
		}
	}
}

func BenchmarkArrayAppendShort(b *testing.B) {
	ini := []int64{1, 2, 3}
	b.ReportAllocs()
	for n := 0; n < b.N; n++ {
		arr := ini
		cpArr := getArray(n)
		arr = arr[:0]
		arr = append(arr, cpArr...)
		if len(arr) != len(cpArr) {
			b.Fail()
		}
	}
}

func BenchmarkArrayCopyShort(b *testing.B) {
	ini := []int64{1, 2, 3}
	b.ReportAllocs()
	for n := 0; n < b.N; n++ {
		arr := ini
		cpArr := getArray(n)
		if cap(arr) >= len(cpArr) {
			arr = arr[:len(cpArr)]
		} else {
			arr = make([]int64, len(cpArr))
		}
		copy(arr, cpArr)
		if len(arr) != len(cpArr) {
			b.Fail()
		}
	}
}

var (
	arrLen6 = []int64{6, 5, 4, 3, 2, 1}
	arrLen7 = []int64{7, 6, 5, 4, 4, 3, 2, 1}
)

func getArray(iter int) []int64 {
	if iter%2 == 0 {
		return arrLen6
	}
	return arrLen7
}

Results:

goos: darwin
goarch: amd64
pkg: go.opentelemetry.io/collector/pdata/pcommon
cpu: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
BenchmarkArrayAppend
BenchmarkArrayAppend-16         	315533996	         3.492 ns/op	       0 B/op	       0 allocs/op
BenchmarkArrayCopy
BenchmarkArrayCopy-16           	308300316	         3.868 ns/op	       0 B/op	       0 allocs/op
BenchmarkArrayAppendNil
BenchmarkArrayAppendNil-16      	37775329	        29.94 ns/op	      56 B/op	       1 allocs/op
BenchmarkArrayCopyNil
BenchmarkArrayCopyNil-16        	40174813	        29.75 ns/op	      56 B/op	       1 allocs/op
BenchmarkArrayAppendShort
BenchmarkArrayAppendShort-16    	38323144	        31.07 ns/op	      56 B/op	       1 allocs/op
BenchmarkArrayCopyShort
BenchmarkArrayCopyShort-16      	43295260	        35.39 ns/op	      56 B/op	       1 allocs/op
PASS

@codecov
Copy link

codecov bot commented Sep 13, 2022

Codecov Report

Base: 91.78% // Head: 91.77% // Decreases project coverage by -0.00% ⚠️

Coverage data is based on head (6c65e10) compared to base (58c8e09).
Patch coverage: 90.47% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6061      +/-   ##
==========================================
- Coverage   91.78%   91.77%   -0.01%     
==========================================
  Files         216      216              
  Lines       13395    13383      -12     
==========================================
- Hits        12294    12282      -12     
  Misses        878      878              
  Partials      223      223              
Impacted Files Coverage Δ
pdata/pcommon/common.go 94.48% <33.33%> (ø)
pdata/pcommon/generated_primitive_slice.go 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@bogdandrutu bogdandrutu merged commit 98c787a into open-telemetry:main Sep 13, 2022
@bogdandrutu bogdandrutu deleted the copyinplace branch September 13, 2022 18:40
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