Skip to content

Commit

Permalink
Replace channel-based iterators with new-style ones (#3272)
Browse files Browse the repository at this point in the history
* Convert functions to iterators

* Change things to use it

* Convert this too

* lint
  • Loading branch information
peterebden authored Oct 10, 2024
1 parent 53f05bb commit dc692af
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 188 deletions.
4 changes: 2 additions & 2 deletions src/build/build_step.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,8 +596,8 @@ func prepareDirectory(directory string, remove bool) error {

// Symlinks the source files of this rule into its temp directory.
func prepareSources(state *core.BuildState, graph *core.BuildGraph, target *core.BuildTarget) error {
for source := range core.IterSources(state, graph, target, false) {
if err := core.PrepareSourcePair(source); err != nil {
for src, tmp := range core.IterSources(state, graph, target, false) {
if err := core.PrepareSource(src, tmp); err != nil {
return err
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/build/incrementality.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@ func mustSourceHash(state *core.BuildState, target *core.BuildTarget) []byte {
// Calculate the hash of all sources of this rule
func sourceHash(state *core.BuildState, target *core.BuildTarget) ([]byte, error) {
h := sha1.New()
for source := range core.IterSources(state, state.Graph, target, false) {
result, err := state.PathHasher.Hash(source.Src, false, true, false)
for src := range core.IterSources(state, state.Graph, target, false) {
result, err := state.PathHasher.Hash(src, false, true, false)
if err != nil {
return nil, err
}
h.Write(result)
h.Write([]byte(source.Src))
h.Write([]byte(src))
}
for _, tool := range target.AllTools() {
for _, path := range tool.FullPaths(state.Graph) {
Expand Down Expand Up @@ -440,8 +440,8 @@ func RuntimeHash(state *core.BuildState, target *core.BuildTarget, testRun int)
hash := append(RuleHash(state, target, true, false), RuleHash(state, target, true, true)...)
hash = append(hash, state.Hashes.Config...)
h := sha1.New()
for source := range core.IterRuntimeFiles(state.Graph, target, true, target.TestDir(testRun)) {
result, err := state.PathHasher.Hash(source.Src, false, true, false)
for src := range core.IterRuntimeFiles(state.Graph, target, true, target.TestDir(testRun)) {
result, err := state.PathHasher.Hash(src, false, true, false)
if err != nil {
return result, err
}
Expand All @@ -465,7 +465,7 @@ func PrintHashes(state *core.BuildState, target *core.BuildTarget) {
// Note that the logic here mimics sourceHash, but I don't want to pollute that with
// optional printing nonsense since it's on our hot path.
for source := range core.IterSources(state, state.Graph, target, false) {
fmt.Printf(" Source: %s: %s\n", source.Src, b64(state.PathHasher.MustHash(source.Src, target.HashLastModified())))
fmt.Printf(" Source: %s: %s\n", source, b64(state.PathHasher.MustHash(source, target.HashLastModified())))
}
for _, tool := range target.AllTools() {
if label, ok := tool.Label(); ok {
Expand Down
26 changes: 15 additions & 11 deletions src/core/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"hash/crc64"
"io"
iofs "io/fs"
"iter"
"path/filepath"
"sort"
"strings"
Expand Down Expand Up @@ -1329,23 +1330,26 @@ func (state *BuildState) DownloadAllInputs(target *BuildTarget, targetDir string
return state.RemoteClient.DownloadInputs(target, targetDir, isTest)
}

// IterInputs returns a channel that iterates all the input files needed for a target.
func (state *BuildState) IterInputs(target *BuildTarget, test bool) <-chan BuildInput {
// IterInputs returns a an iterator over all the input files needed for a target.
func (state *BuildState) IterInputs(target *BuildTarget, test bool) iter.Seq[BuildInput] {
if !test {
return IterInputs(state, state.Graph, target, true, target.IsFilegroup)
}
ch := make(chan BuildInput)
go func() {
ch <- target.Label
return func(yield func(BuildInput) bool) {
if !yield(target.Label) {
return
}
for _, datum := range target.AllData() {
ch <- datum
if !yield(datum) {
return
}
}
for _, datum := range target.AllTestTools() {
ch <- datum
for _, tool := range target.AllTestTools() {
if !yield(tool) {
return
}
}
close(ch)
}()
return ch
}
}

// DisableXattrs disables xattr support for this build. This is done for filesystems that
Expand Down
Loading

0 comments on commit dc692af

Please sign in to comment.