Skip to content

GODRIVER-3108 Optimize writeServerSelector #1509

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 5 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions mongo/description/selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,27 @@ func BenchmarkSelector_Sharded(b *testing.B) {
}
}

func Benchmark_SelectServer_SelectServer(b *testing.B) {
topology := Topology{Kind: ReplicaSet} // You can change the topology as needed
candidates := []Server{
{Kind: Mongos},
{Kind: RSPrimary},
{Kind: Standalone},
}

selector := writeServerSelector{} // Assuming this is the receiver type

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
_, err := selector.SelectServer(topology, candidates)
if err != nil {
b.Fatalf("Error selecting server: %v", err)
}
}
}

func TestSelector_Single(t *testing.T) {
t.Parallel()

Expand Down
12 changes: 11 additions & 1 deletion mongo/description/server_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,17 @@ func (writeServerSelector) SelectServer(t Topology, candidates []Server) ([]Serv
case Single, LoadBalanced:
return candidates, nil
default:
result := []Server{}
// Determine the capacity of the results slice.
selected := 0
for _, candidate := range candidates {
switch candidate.Kind {
case Mongos, RSPrimary, Standalone:
selected++
}
}

// Append candidates to the results slice.
result := make([]Server, 0, selected)
for _, candidate := range candidates {
switch candidate.Kind {
case Mongos, RSPrimary, Standalone:
Expand Down