Skip to content

Commit

Permalink
perf: replace sort.Slice with slices.SortFunc
Browse files Browse the repository at this point in the history
Since we're already requiring the slices package in stdlib with this
refactor, we can utilize this newer function which should be slightly
more efficient (and has nicer ergonomics imo).

Performs roughly ~11% faster during NewChooser initialization.

goos: darwin
goarch: arm64
pkg: github.com/mroth/weightedrand/v2
                        │  v3-dev1.txt  │             v3-dev2.txt              │
                        │    sec/op     │   sec/op     vs base                 │
NewChooser/size=1e1-8     132.90n ±  0%   70.73n ± 1%  -46.78% (p=0.002 n=6)
NewChooser/size=1e2-8      472.8n ±  0%   444.1n ± 2%   -6.05% (p=0.002 n=6)
NewChooser/size=1e3-8      3.412µ ±  0%   3.333µ ± 0%   -2.30% (p=0.002 n=6)
NewChooser/size=1e4-8      31.03µ ±  0%   30.30µ ± 0%   -2.33% (p=0.002 n=6)
NewChooser/size=1e5-8      295.9µ ±  0%   291.9µ ± 1%   -1.36% (p=0.002 n=6)
NewChooser/size=1e6-8      2.843m ±  1%   2.775m ± 1%   -2.37% (p=0.002 n=6)
NewChooser/size=1e7-8      35.92m ±  1%   32.99m ± 2%   -8.16% (p=0.002 n=6)
geomean                    279.7n         36.41µ       -11.60%               ¹
¹ benchmark set differs from baseline; geomeans may not be comparable
  • Loading branch information
mroth committed Dec 30, 2023
1 parent afeb21f commit 2671bfe
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions weightedrand.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
package weightedrand

import (
"cmp"

Check failure on line 12 in weightedrand.go

View workflow job for this annotation

GitHub Actions / Go 1.19 test

package cmp is not in GOROOT (/opt/hostedtoolcache/go/1.19.13/x64/src/cmp)

Check failure on line 12 in weightedrand.go

View workflow job for this annotation

GitHub Actions / Go 1.20 test

package cmp is not in GOROOT (/opt/hostedtoolcache/go/1.20.12/x64/src/cmp)
"errors"
"math"
"math/rand/v2"

Check failure on line 15 in weightedrand.go

View workflow job for this annotation

GitHub Actions / Go test coverage

package math/rand/v2 is not in std (/opt/hostedtoolcache/go/1.21.6/x64/src/math/rand/v2)

Check failure on line 15 in weightedrand.go

View workflow job for this annotation

GitHub Actions / Go 1.19 test

package math/rand/v2 is not in GOROOT (/opt/hostedtoolcache/go/1.19.13/x64/src/math/rand/v2)

Check failure on line 15 in weightedrand.go

View workflow job for this annotation

GitHub Actions / Go 1.20 test

package math/rand/v2 is not in GOROOT (/opt/hostedtoolcache/go/1.20.12/x64/src/math/rand/v2)

Check failure on line 15 in weightedrand.go

View workflow job for this annotation

GitHub Actions / Go 1.21 test

package math/rand/v2 is not in std (/opt/hostedtoolcache/go/1.21.6/x64/src/math/rand/v2)
"slices"

Check failure on line 16 in weightedrand.go

View workflow job for this annotation

GitHub Actions / Go 1.19 test

package slices is not in GOROOT (/opt/hostedtoolcache/go/1.19.13/x64/src/slices)

Check failure on line 16 in weightedrand.go

View workflow job for this annotation

GitHub Actions / Go 1.20 test

package slices is not in GOROOT (/opt/hostedtoolcache/go/1.20.12/x64/src/slices)
"sort"
)

// Choice is a generic wrapper that can be used to add weights for any item.
Expand Down Expand Up @@ -41,8 +41,8 @@ type Chooser[T any, W integer] struct {

// NewChooser initializes a new Chooser for picking from the provided choices.
func NewChooser[T any, W integer](choices ...Choice[T, W]) (*Chooser[T, W], error) {
sort.Slice(choices, func(i, j int) bool {
return choices[i].Weight < choices[j].Weight
slices.SortFunc(choices, func(a, b Choice[T, W]) int {
return cmp.Compare(a.Weight, b.Weight)
})

totals := make([]uint64, len(choices))
Expand All @@ -67,7 +67,6 @@ func NewChooser[T any, W integer](choices ...Choice[T, W]) (*Chooser[T, W], erro
return &Chooser[T, W]{data: choices, totals: totals, max: runningTotal}, nil
}


// Possible errors returned by NewChooser, preventing the creation of a Chooser
// with unsafe runtime states.
var (
Expand Down

0 comments on commit 2671bfe

Please sign in to comment.