Skip to content

Commit

Permalink
Preallocate dependency info on BuildTarget (#3023)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterebden authored Jan 5, 2024
1 parent 6221539 commit 93c0b96
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/core/build_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"path/filepath"
"slices"
"sort"
"strings"
"sync"
Expand Down Expand Up @@ -1630,6 +1631,11 @@ func (target *BuildTarget) AddDependency(dep BuildLabel) {
target.AddMaybeExportedDependency(dep, false, false, false)
}

// HintDependencies allocates space for at least the given number of dependencies without reallocating.
func (target *BuildTarget) HintDependencies(n int) {
target.dependencies = slices.Grow(target.dependencies, n)
}

// AddMaybeExportedDependency adds a dependency to this target which may be exported. It deduplicates against any existing deps.
func (target *BuildTarget) AddMaybeExportedDependency(dep BuildLabel, exported, source, internal bool) {
if dep == target.Label {
Expand Down
9 changes: 9 additions & 0 deletions src/parse/asp/targets.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ func populateTarget(s *scope, t *core.BuildTarget, args []pyObject) {
addMaybeNamed(s, "data", args[dataBuildRuleArgIdx], t.AddDatum, t.AddNamedDatum, false, false)
addMaybeNamedOutput(s, "outs", args[outsBuildRuleArgIdx], t.AddOutput, t.AddNamedOutput, t, false)
addMaybeNamedOutput(s, "optional_outs", args[optionalOutsBuildRuleArgIdx], t.AddOptionalOutput, nil, t, true)
t.HintDependencies(depLen(args[depsBuildRuleArgIdx]) + depLen(args[exportedDepsBuildRuleArgIdx]) + depLen(args[internalDepsBuildRuleArgIdx]))
addDependencies(s, "deps", args[depsBuildRuleArgIdx], t, false, false)
addDependencies(s, "exported_deps", args[exportedDepsBuildRuleArgIdx], t, true, false)
addDependencies(s, "internal_deps", args[internalDepsBuildRuleArgIdx], t, false, true)
Expand Down Expand Up @@ -301,6 +302,14 @@ func populateTarget(s *scope, t *core.BuildTarget, args []pyObject) {
}
}

// depLen returns the length of a (potential) list
func depLen(obj pyObject) int {
if l, ok := asList(obj); ok {
return len(l)
}
return 0
}

// addEntryPoints adds entry points to a target
func addEntryPoints(s *scope, arg pyObject, target *core.BuildTarget) {
entryPointsPy, ok := asDict(arg)
Expand Down

0 comments on commit 93c0b96

Please sign in to comment.