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

pdatagen - Add MoveTo method for struct types #4240

Merged
merged 6 commits into from
Oct 27, 2021

Conversation

trasc
Copy link
Contributor

@trasc trasc commented Oct 21, 2021

Add a new move method in the generated pdata struct types. Changed the batchprocessor to use MoveTo during splits.

Benchmarks:

Before:

go test -run=none -bench=Split  -benchtime=1000x -benchmem
goos: linux
goarch: amd64
pkg: go.opentelemetry.io/collector/processor/batchprocessor
cpu: Intel(R) Core(TM) i7-9850H CPU @ 2.60GHz
BenchmarkSplitLogs-12               1000              4339 ns/op            3200 B/op         53 allocs/op
BenchmarkSplitMetrics-12            1000             12308 ns/op            5056 B/op        109 allocs/op
BenchmarkSplitTraces-12             1000              4599 ns/op            4736 B/op         69 allocs/op
PASS
ok      go.opentelemetry.io/collector/processor/batchprocessor  0.408s

After:

go test -run=none -bench=Split  -benchtime=1000x -benchmem
goos: linux
goarch: amd64
pkg: go.opentelemetry.io/collector/processor/batchprocessor
cpu: Intel(R) Core(TM) i7-9850H CPU @ 2.60GHz
BenchmarkSplitLogs-12               1000              2719 ns/op            1968 B/op         27 allocs/op
BenchmarkSplitMetrics-12            1000              7505 ns/op            1456 B/op         27 allocs/op
BenchmarkSplitTraces-12             1000              2091 ns/op            2736 B/op         27 allocs/op
PASS
ok      go.opentelemetry.io/collector/processor/batchprocessor  0.397s

@trasc trasc requested review from a team and tigrannajaryan October 21, 2021 13:16
@trasc trasc changed the title pdatagen - Add MoveTo method for struct tyes pdatagen - Add MoveTo method for struct types Oct 21, 2021
Copy link
Member

@bogdandrutu bogdandrutu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a copy not a move. You don't move memory instead you just copy it. This can be done as an optimization for CopyTo I believe

@bogdandrutu
Copy link
Member

The only difference is that you empty the src

@bogdandrutu
Copy link
Member

Completely wrong, this does not do a full copy, so definitely is a speed up. I have a concern here, the fact that we leave an "empty" message in the source slice. I think you want this to be used this in the ForEach so will not be a problem.

@trasc
Copy link
Contributor Author

trasc commented Oct 21, 2021

Completely wrong, this does not do a full copy, so definitely is a speed up. I have a concern here, the fact that we leave an "empty" message in the source slice. I think you want this to be used this in the ForEach so will not be a problem.

For sure, in case the source is an element of a slice the caller should keep this in mind. It is specified in the methods comment but we cannot do much more then this. The same is happening in case of MoveAndAppendTo method generated for slices.

@bogdandrutu
Copy link
Member

@trasc where do you need this? I need to see few examples to convince myself about the usefulness of this.

@trasc
Copy link
Contributor Author

trasc commented Oct 22, 2021

@trasc where do you need this? I need to see few examples to convince myself about the usefulness of this.

One use case is splitting a slice type (based on some attribute of its elements) for instance ResourceMetricsSlice.

A benchmark for this can look like:

func generateBenchmarksResourceMetricsSlice(l int) ResourceMetricsSlice {
	ret := NewResourceMetricsSlice()
	ret.EnsureCapacity(l)
	for i := 0; i < l; i++ {
		fillTestResourceMetrics(ret.AppendEmpty())
	}
	return ret
}

func BenchmarkMC_CopyTo(b *testing.B) {
	src_rms := generateBenchmarksResourceMetricsSlice(b.N)
	dst_rms := NewResourceMetricsSlice()
	dst_rms.EnsureCapacity(b.N)
	b.ResetTimer()

	src_rms.RemoveIf(func(rm ResourceMetrics) bool {
		if true { //some condition.. , but let's thake the worst case
			rm.CopyTo(dst_rms.AppendEmpty())
			return true
		}
		return false
	})
}

func BenchmarkMC_MoveTo(b *testing.B) {
	src_rms := generateBenchmarksResourceMetricsSlice(b.N)
	dst_rms := NewResourceMetricsSlice()
	dst_rms.EnsureCapacity(b.N)
	b.ResetTimer()

	src_rms.RemoveIf(func(rm ResourceMetrics) bool {
		if true { //some condition.. , but let's thake the worst case
			rm.MoveTo(dst_rms.AppendEmpty())
			return true
		}
		return false
	})
}

and the results I'm getting:

$ go test -run=none -bench=MC_ -benchtime=1000x -benchmem
goos: linux
goarch: amd64
pkg: go.opentelemetry.io/collector/model/pdata
cpu: Intel(R) Core(TM) i7-9850H CPU @ 2.60GHz
BenchmarkMC_CopyTo-12               1000            376079 ns/op          404112 B/op       6045 allocs/op
BenchmarkMC_MoveTo-12               1000                82.40 ns/op           80 B/op          1 allocs/op

@bogdandrutu
Copy link
Member

bogdandrutu commented Oct 26, 2021

Please rebase. Also I think we should update the batch_processor to prove the usefulness of the change and add the benchmark results from there.

Copy link
Member

@bogdandrutu bogdandrutu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM, some more work is needed, see my previous comment.

@trasc
Copy link
Contributor Author

trasc commented Oct 26, 2021

Rebased and changed the batchprocessor to use MoveTo during splits.

Benchmarks:

Before:

go test -run=none -bench=Split  -benchtime=1000x -benchmem
goos: linux
goarch: amd64
pkg: go.opentelemetry.io/collector/processor/batchprocessor
cpu: Intel(R) Core(TM) i7-9850H CPU @ 2.60GHz
BenchmarkSplitLogs-12               1000              4339 ns/op            3200 B/op         53 allocs/op
BenchmarkSplitMetrics-12            1000             12308 ns/op            5056 B/op        109 allocs/op
BenchmarkSplitTraces-12             1000              4599 ns/op            4736 B/op         69 allocs/op
PASS
ok      go.opentelemetry.io/collector/processor/batchprocessor  0.408s

After:

go test -run=none -bench=Split  -benchtime=1000x -benchmem
goos: linux
goarch: amd64
pkg: go.opentelemetry.io/collector/processor/batchprocessor
cpu: Intel(R) Core(TM) i7-9850H CPU @ 2.60GHz
BenchmarkSplitLogs-12               1000              2719 ns/op            1968 B/op         27 allocs/op
BenchmarkSplitMetrics-12            1000              7505 ns/op            1456 B/op         27 allocs/op
BenchmarkSplitTraces-12             1000              2091 ns/op            2736 B/op         27 allocs/op
PASS
ok      go.opentelemetry.io/collector/processor/batchprocessor  0.397s

@codecov
Copy link

codecov bot commented Oct 26, 2021

Codecov Report

Merging #4240 (d6adb62) into main (c64e6ae) will increase coverage by 0.17%.
The diff coverage is 84.90%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #4240      +/-   ##
==========================================
+ Coverage   88.20%   88.38%   +0.17%     
==========================================
  Files         173      173              
  Lines       10327    10434     +107     
==========================================
+ Hits         9109     9222     +113     
+ Misses        978      975       -3     
+ Partials      240      237       -3     
Impacted Files Coverage Δ
processor/batchprocessor/splitmetrics.go 67.18% <51.02%> (+2.00%) ⬆️
model/pdata/generated_common.go 100.00% <100.00%> (ø)
model/pdata/generated_log.go 96.65% <100.00%> (+0.11%) ⬆️
model/pdata/generated_metrics.go 96.98% <100.00%> (+0.15%) ⬆️
model/pdata/generated_resource.go 100.00% <100.00%> (ø)
model/pdata/generated_trace.go 96.88% <100.00%> (+0.12%) ⬆️
processor/batchprocessor/splitlogs.go 100.00% <100.00%> (+7.31%) ⬆️
processor/batchprocessor/splittraces.go 100.00% <100.00%> (+7.31%) ⬆️
config/configauth/mock_clientauth.go 100.00% <0.00%> (ø)
... and 3 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c64e6ae...d6adb62. Read the comment docs.

@bogdandrutu
Copy link
Member

@trasc please ensure that we don't decrease our code coverage.

@trasc
Copy link
Contributor Author

trasc commented Oct 26, 2021

@bogdandrutu can you trigger a re-run?

@bogdandrutu bogdandrutu merged commit fd61bbb into open-telemetry:main Oct 27, 2021
@trasc trasc deleted the add_moveto branch October 27, 2021 18:54
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