Skip to content

GODRIVER-2628 optimize allocations in selectServerFromDescription #1111

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

Conversation

isopov
Copy link
Contributor

@isopov isopov commented Oct 27, 2022

GODRIVER-2628

Summary

Optimize allocations in selectServerFromDescription

Background & Motivation

Same way as in #1108 and #1107 I want to reduce allocations in selectServerFromDescription func which flat allocations in my case are > 100GB and which possess the very first place by flat allocations in my long running job.

Benchmark attached without changes to the code:

BenchmarkSelectServerFromDescription
BenchmarkSelectServerFromDescription/AllFit
BenchmarkSelectServerFromDescription/AllFit-10         	   63248	     17195 ns/op	  111265 B/op	       8 allocs/op
BenchmarkSelectServerFromDescription/AllButOneFit
BenchmarkSelectServerFromDescription/AllButOneFit-10   	   65990	     18173 ns/op	  111265 B/op	       8 allocs/op
BenchmarkSelectServerFromDescription/HalfFit
BenchmarkSelectServerFromDescription/HalfFit-10        	  110966	      9053 ns/op	   53920 B/op	       7 allocs/op
BenchmarkSelectServerFromDescription/OneFit
BenchmarkSelectServerFromDescription/OneFit-10         	 3362158	       351.3 ns/op	     416 B/op	       1 allocs/op
PASS

And with changes:

BenchmarkSelectServerFromDescription
BenchmarkSelectServerFromDescription/AllFit
BenchmarkSelectServerFromDescription/AllFit-10         	 2548441	       480.4 ns/op	     896 B/op	       1 allocs/op
BenchmarkSelectServerFromDescription/AllButOneFit
BenchmarkSelectServerFromDescription/AllButOneFit-10   	  159066	      6740 ns/op	   41856 B/op	       2 allocs/op
BenchmarkSelectServerFromDescription/HalfFit
BenchmarkSelectServerFromDescription/HalfFit-10        	  295821	      3710 ns/op	   21376 B/op	       2 allocs/op
BenchmarkSelectServerFromDescription/OneFit
BenchmarkSelectServerFromDescription/OneFit-10         	 1814713	       639.6 ns/op	    1312 B/op	       2 allocs/op
PASS

@isopov
Copy link
Contributor Author

isopov commented Oct 27, 2022

BTW in all the cases it seems beneficial to use bool slice for filtering like

	allowedIndexes := make([]bool, len(desc.Servers))
	allowedIndexesCount := 0
	for i, s := range desc.Servers {
		if s.Kind != description.Unknown {
			allowedIndexes[i] = true
			allowedIndexesCount++
		}
	}
	var allowed []description.Server
	if allowedIndexesCount == len(desc.Servers) {
		allowed = desc.Servers
	} else {
		allowed = make([]description.Server, 0, allowedIndexesCount)
		for i, allowedIndex := range allowedIndexes {
			if allowedIndex {
				allowed = append(allowed, desc.Servers[i])
			}
		}
	}

In this case benchmark results are:

BenchmarkSelectServerFromDescription
BenchmarkSelectServerFromDescription/AllFit
BenchmarkSelectServerFromDescription/AllFit-10         	 4538070	       276.1 ns/op	     112 B/op	       1 allocs/op
BenchmarkSelectServerFromDescription/AllButOneFit
BenchmarkSelectServerFromDescription/AllButOneFit-10   	  146278	      7216 ns/op	   41072 B/op	       2 allocs/op
BenchmarkSelectServerFromDescription/HalfFit
BenchmarkSelectServerFromDescription/HalfFit-10        	  297627	      3904 ns/op	   20592 B/op	       2 allocs/op
BenchmarkSelectServerFromDescription/OneFit
BenchmarkSelectServerFromDescription/OneFit-10         	 2960940	       393.9 ns/op	     528 B/op	       2 allocs/op
PASS

Should I use this approach in all three places?

@benjirewis benjirewis self-requested a review October 28, 2022 16:08
@prestonvasquez prestonvasquez self-requested a review October 28, 2022 16:11
@matthewdale matthewdale changed the title optimize allocations in selectServerFromDescription GODRIVER-2628 optimize allocations in selectServerFromDescription Oct 28, 2022
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.

Thank you for this PR @isopov !

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
Contributor

@benjirewis benjirewis left a comment

Choose a reason for hiding this comment

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

LGTM assuming tests pass! This seems like a very reasonable addition given the previous changes in GODRIVER-2526. Thanks for your contribution, @isopov .

@benjirewis benjirewis merged commit a2a01cb into mongodb:master Nov 9, 2022
@isopov isopov deleted the optimize-selectServerFromDescription branch January 31, 2023 10:44
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