Use a lexical heap for analysis scheduling
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package stage
|
||||
|
||||
import (
|
||||
"container/heap"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
@@ -184,6 +185,20 @@ func hashAnalyzeFingerprintPayload(payload analyzeFingerprintPayload) (string, e
|
||||
return hex.EncodeToString(digest[:]), nil
|
||||
}
|
||||
|
||||
type analyzeReadyQueue []string
|
||||
|
||||
func (q analyzeReadyQueue) Len() int { return len(q) }
|
||||
func (q analyzeReadyQueue) Less(i, j int) bool { return q[i] < q[j] }
|
||||
func (q analyzeReadyQueue) Swap(i, j int) { q[i], q[j] = q[j], q[i] }
|
||||
func (q *analyzeReadyQueue) Push(value any) { *q = append(*q, value.(string)) }
|
||||
func (q *analyzeReadyQueue) Pop() any {
|
||||
last := len(*q) - 1
|
||||
value := (*q)[last]
|
||||
(*q)[last] = ""
|
||||
*q = (*q)[:last]
|
||||
return value
|
||||
}
|
||||
|
||||
func orderConfiguredAnalyzeArtifacts(
|
||||
configured map[string]config.ScriptoriumArtifactConfig,
|
||||
) ([]string, error) {
|
||||
@@ -201,26 +216,21 @@ func orderConfiguredAnalyzeArtifacts(
|
||||
indegree[key]++
|
||||
}
|
||||
}
|
||||
for key := range edges {
|
||||
sort.Strings(edges[key])
|
||||
}
|
||||
ready := make([]string, 0, len(indegree))
|
||||
ready := make(analyzeReadyQueue, 0, len(indegree))
|
||||
for key, degree := range indegree {
|
||||
if degree == 0 {
|
||||
ready = append(ready, key)
|
||||
}
|
||||
}
|
||||
sort.Strings(ready)
|
||||
heap.Init(&ready)
|
||||
order := make([]string, 0, len(indegree))
|
||||
for len(ready) > 0 {
|
||||
key := ready[0]
|
||||
ready = ready[1:]
|
||||
key := heap.Pop(&ready).(string)
|
||||
order = append(order, key)
|
||||
for _, dependent := range edges[key] {
|
||||
indegree[dependent]--
|
||||
if indegree[dependent] == 0 {
|
||||
ready = append(ready, dependent)
|
||||
sort.Strings(ready)
|
||||
heap.Push(&ready, dependent)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package stage
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
@@ -12,6 +13,20 @@ import (
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/manifest"
|
||||
)
|
||||
|
||||
func TestAnalyzeFingerprintOrderWideFanout(t *testing.T) {
|
||||
items := map[string]config.ScriptoriumArtifactConfig{"root": {}}
|
||||
want := []string{"root"}
|
||||
for i := 0; i < 64; i++ {
|
||||
key := fmt.Sprintf("child_%03d", i)
|
||||
items[key] = config.ScriptoriumArtifactConfig{DependsOn: []string{"root"}}
|
||||
want = append(want, key)
|
||||
}
|
||||
got, err := orderConfiguredAnalyzeArtifacts(items)
|
||||
if err != nil || strings.Join(got, ",") != strings.Join(want, ",") {
|
||||
t.Fatalf("wide fanout order: %v, %v", got, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAnalyzeFingerprintCanonicalPayloadSensitivity(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
||||
Reference in New Issue
Block a user