Skip to content

Commit

Permalink
simplify function, some marginal perf gain
Browse files Browse the repository at this point in the history
  • Loading branch information
ainghazal committed Oct 23, 2024
1 parent d9c4b3e commit 2acc5df
Showing 1 changed file with 4 additions and 15 deletions.
19 changes: 4 additions & 15 deletions internal/experiment/openvpn/targets.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,10 @@ func sampleN(a []string, n int) []string {
if n > len(a) {
n = len(a)
}

sampled := make([]string, 0)

// Use a map to track indices we've already selected to avoid duplicates
picked := make(map[int]struct{})

for len(sampled) < n {
idx := rand.Intn(len(a)) // Random index
if _, exists := picked[idx]; !exists {
sampled = append(sampled, a[idx])
picked[idx] = struct{}{} // Mark index as used
}
}

return sampled
rand.Shuffle(len(a), func(i, j int) {
a[i], a[j] = a[j], a[i]
})
return a[:n]
}

// resolveOONIAddresses returns a max of maxDefaultOONIAddresses after
Expand Down

0 comments on commit 2acc5df

Please sign in to comment.