Skip to content

Commit

Permalink
perf: use map instead of slice.Contains
Browse files Browse the repository at this point in the history
  • Loading branch information
mircearoata committed Oct 2, 2024
1 parent 493281d commit bc37da3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
12 changes: 5 additions & 7 deletions resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package resolver
import (
"errors"
"fmt"

Check failure on line 5 in resolver.go

View workflow job for this annotation

GitHub Actions / Lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/satisfactorymodding/ficsit-resolver) -s blank -s dot --custom-order (gci)

Check failure on line 5 in resolver.go

View workflow job for this annotation

GitHub Actions / Lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/satisfactorymodding/ficsit-resolver) -s blank -s dot --custom-order (gci)

Check failure on line 5 in resolver.go

View workflow job for this annotation

GitHub Actions / Lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/satisfactorymodding/ficsit-resolver) -s blank -s dot --custom-order (gci)
"slices"

"github.com/mircearoata/pubgrub-go/pubgrub"
"github.com/mircearoata/pubgrub-go/pubgrub/helpers"
"github.com/mircearoata/pubgrub-go/pubgrub/semver"
Expand All @@ -16,10 +14,10 @@ const (
factoryGamePkg = "FactoryGame"
)

var allTargets = []TargetName{
TargetNameLinuxServer,
TargetNameWindows,
TargetNameWindowsServer,
var allTargets = map[TargetName]bool{
TargetNameLinuxServer: true,
TargetNameWindows: true,
TargetNameWindowsServer: true,
}

type DependencyResolver struct {
Expand Down Expand Up @@ -49,7 +47,7 @@ func (d DependencyResolver) ResolveModDependencies(constraints map[string]string

mappedTargets := make(map[TargetName]bool, len(requiredTargets))
for _, target := range requiredTargets {
if !slices.Contains(allTargets, target) {
if !allTargets[target] {
return nil, fmt.Errorf("invalid target: %s", target)
}
mappedTargets[target] = true
Expand Down
14 changes: 7 additions & 7 deletions source.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ type ficsitAPISource struct {
gameVersion semver.Version
}

var clientTargets = []TargetName{
TargetNameWindows,
var clientTargets = map[TargetName]bool{
TargetNameWindows: true,
}

var serverTargets = []TargetName{
TargetNameWindowsServer,
TargetNameLinuxServer,
var serverTargets = map[TargetName]bool{
TargetNameWindowsServer: true,
TargetNameLinuxServer: true,
}

func (f *ficsitAPISource) GetPackageVersions(pkg string) ([]pubgrub.PackageVersion, error) {
Expand Down Expand Up @@ -124,9 +124,9 @@ func (f *ficsitAPISource) matchesTargetRequirements(modVersion ModVersion) (bool
requiredServerTargets := make(map[TargetName]bool)

for target := range f.requiredTargets {
if slices.Contains(clientTargets, target) {
if clientTargets[target] {
requiredClientTargets[target] = true
} else if slices.Contains(serverTargets, target) {
} else if serverTargets[target] {
requiredServerTargets[target] = true
} else {
return false, fmt.Errorf("unknown requested target %s", target)
Expand Down

0 comments on commit bc37da3

Please sign in to comment.