Skip to content

Commit

Permalink
Reduce slice allocations during unique insertions (#705)
Browse files Browse the repository at this point in the history
Just reduce some really easy to avoid slice allocations for every unique
insertion:

* No reason to have `requiredV3states` allocated every time a new unique
  opts is validated. It never changes.

* Leave `missingStates` unallocated by default. Most of the time a
  unique insertion will have have no missing unique states, so this
  skips a unique allocation completely the overwhelmingly majority of
  the time.
  • Loading branch information
brandur authored Dec 27, 2024
1 parent d73c280 commit f88dfff
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Sleep durations are now logged as Go-like duration strings (e.g. "10s") in either text or JSON instead of duration strings in text and nanoseconds in JSON. [PR #699](https://github.com/riverqueue/river/pull/699).
- Altered the migration comments from `river migrate-get` to include the "line" of the migration being run (`main`, or for River Pro `workflow` and `sequence`) to make them more distinguishable. [PR #703](https://github.com/riverqueue/river/pull/703).
- Fewer slice allocations during unique insertions. [PR #705](https://github.com/riverqueue/river/pull/705).

### Fixed

Expand Down
15 changes: 8 additions & 7 deletions insert_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,13 @@ func (o *UniqueOpts) isEmpty() bool {

var jobStateAll = rivertype.JobStates() //nolint:gochecknoglobals

var requiredV3states = []rivertype.JobState{ //nolint:gochecknoglobals
rivertype.JobStateAvailable,
rivertype.JobStatePending,
rivertype.JobStateRunning,
rivertype.JobStateScheduled,
}

func (o *UniqueOpts) validate() error {
if o.isEmpty() {
return nil
Expand All @@ -209,13 +216,7 @@ func (o *UniqueOpts) validate() error {
return nil
}

requiredV3states := []rivertype.JobState{
rivertype.JobStateAvailable,
rivertype.JobStatePending,
rivertype.JobStateRunning,
rivertype.JobStateScheduled,
}
missingStates := []string{}
var missingStates []string
for _, state := range requiredV3states {
if !slices.Contains(o.ByState, state) {
missingStates = append(missingStates, string(state))
Expand Down

0 comments on commit f88dfff

Please sign in to comment.