:balance_scale: Fast weighted random selection for Go
As of go 1.21, global rand should no longer suffer from lock contention issues as long as rand.Seed
(which was deprecated in go1.20) is not called. [reference] This removes the primary use case for Chooser.PickSource
in this module, which was to avoid rand lock contention when using a single Choose in extremely high-throughput environments across multiple goroutines.
This update primarily updates documentation to reflect this, marking PickSource
as deprecated in favor of just using the default Pick
. The simple case will now perform just as well in that situation.
See #28 for details.
Full Changelog: https://github.com/mroth/weightedrand/compare/v2.0.2...v2.1.0
Full Changelog: https://github.com/mroth/weightedrand/compare/v2.0.1...v2.0.2
Full Changelog: https://github.com/mroth/weightedrand/compare/v2.0.0...v2.0.1
🎉 weightedrand
now utilizes generics for the container data structure to clean up the API and improve end-user ergonomics, thus typecasting is no longer required when obtaining your result, and can utilize any integer type for scores.
This requires a tiny code change if upgrading from a previous version (most likely just removing now unneeded typecasts), and manual upgrading of your import path to include the /v2
(see https://golang.cafe/blog/how-to-upgrade-to-a-major-version-in-go.html).
E.g. what used to be:
result := chooser.Pick().(type)
Can now simply be expressed as:
result := chooser.Pick()
This also means that the current version of the library requires go >= 1.18. For previous versions, you can continue to rely upon v1
of this library. The go module versioning system ensures you will not be automatically upgraded to a new major version without manual action.
Full Changelog: https://github.com/mroth/weightedrand/compare/v1.0.0...v2.0.0
Only CI and test related files, no changes to the library itself. This is a tagged v1.0.0 release to denote the finalization of this version of the API for API compatibility. This version will be preserved in the v1
branch.
A v2 release will closely follow integrating the API changes from generics in #14.
Full Changelog: https://github.com/mroth/weightedrand/compare/v0.4.1...v1.0.0
Contains a bug fix for a corner edge case introduced in #4. (thx @zifter!)
https://github.com/mroth/weightedrand/compare/v0.4.0...v0.4.1
This major release version changes the NewChooser
constructor to check for and error on edge conditions that could later cause a runtime issue during Pick. NOTE: As this changes the API signature for NewChooser
, thus you might have to adjust your code. Since we are still in pre-release semantic versioning now is the time to make these beneficial API refinements before we hit v1.0
and need to enforce stability.
// previous
func NewChooser(choices ...Choice) Chooser
// now
func NewChooser(choices ...Choice) (*Chooser, error)
All relevant documentation and examples has been updated to reflect this change.
https://github.com/mroth/weightedrand/compare/v0.3.1...v0.4.0
The primary change in this release is an optimized internal implementation of the binary int search, replacing the Go standard library usage, resulting in a roughly ~33% further performance boost to samplings from a Chooser. 🐎
https://github.com/mroth/weightedrand/compare/v0.3.0...v0.3.1
🎲 The major new feature introduced in this version is the PickSource()
method, which allows for utilizing a single Chooser in parallel across multiple goroutines while avoiding rand lock contention, thus allowing linearly scaling performance across CPU cores. For more details, see PR #2.
https://github.com/mroth/weightedrand/compare/v0.2.2...v0.3.0
Features:
Chores:
https://github.com/mroth/weightedrand/compare/v0.2.1...v0.3.0