Skip to content

GODRIVER-2627 optimize latency selector #1108

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

Merged
merged 3 commits into from
Nov 8, 2022

Conversation

isopov
Copy link
Contributor

@isopov isopov commented Oct 26, 2022

Summary

Optimize allocations in latency server selector.

Background & Motivation

Similar to #1107 i try to optimize allocations in latency server selector. Benchmark provided without changes:

BenchmarkLatencySelector
BenchmarkLatencySelector/AllFit
BenchmarkLatencySelector/AllFit-10         	   64196	     17866 ns/op	  111273 B/op	       9 allocs/op
BenchmarkLatencySelector/AllButOneFit
BenchmarkLatencySelector/AllButOneFit-10   	   71056	     17038 ns/op	  111273 B/op	       9 allocs/op
BenchmarkLatencySelector/HalfFit
BenchmarkLatencySelector/HalfFit-10        	  124551	      9748 ns/op	   53928 B/op	       8 allocs/op
BenchmarkLatencySelector/OneFit
BenchmarkLatencySelector/OneFit-10         	 1471053	       825.6 ns/op	    1320 B/op	       3 allocs/op
PASS

And with proposed optimization:

BenchmarkLatencySelector
BenchmarkLatencySelector/AllFit
BenchmarkLatencySelector/AllFit-10         	 1868062	       686.8 ns/op	     904 B/op	       2 allocs/op
BenchmarkLatencySelector/AllButOneFit
BenchmarkLatencySelector/AllButOneFit-10   	  155904	      7382 ns/op	   41864 B/op	       3 allocs/op
BenchmarkLatencySelector/HalfFit
BenchmarkLatencySelector/HalfFit-10        	  277989	      4409 ns/op	   21384 B/op	       3 allocs/op
BenchmarkLatencySelector/OneFit
BenchmarkLatencySelector/OneFit-10         	 1247785	       966.7 ns/op	    1800 B/op	       3 allocs/op
PASS

@matthewdale matthewdale self-requested a review October 26, 2022 17:33
@matthewdale matthewdale changed the title optimize latency selector GODRIVER-2627 optimize latency selector Oct 28, 2022
@prestonvasquez prestonvasquez self-requested a review October 28, 2022 19:52
Copy link
Member

@prestonvasquez prestonvasquez left a comment

Choose a reason for hiding this comment

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

@isopov thank you for the PR!

@@ -230,6 +230,67 @@ func TestSelector_Sharded(t *testing.T) {
require.Equal([]Server{s}, result)
}

func BenchmarkLatencySelector(b *testing.B) {
bench := func(b *testing.B, serversHook func(servers []Server)) {
Copy link
Member

Choose a reason for hiding this comment

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

Instead of using a closure and constructing sub-benchmarks for each case, can we iterate over the benchmark cases? Something like this:

	for _, bcase := range []struct {
		name        string
		serversHook func(servers []description.Server)
	}{
		{
			"AllFit",
			func(servers []description.Server) {},
		},
		{
			"AllButOneFit",
			func(servers []description.Server) {
				servers[0].AverageRTT = 2 * time.Second
			},
		},
		// Continue test cases
	} {
		bcase := bcase

		b.Run(bcase.name, func(b *testing.B) {
			// your benchmark logic
		})
	}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed benchmark this way

Copy link
Member

@prestonvasquez prestonvasquez left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Collaborator

@matthewdale matthewdale left a comment

Choose a reason for hiding this comment

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

@isopov thanks for the improvements! Looks good 👍

@matthewdale matthewdale merged commit 4c5c832 into mongodb:master Nov 8, 2022
@isopov isopov deleted the optimize-latency-selector branch November 9, 2022 09:51
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.

3 participants