Use a lexical heap for analysis scheduling

This commit is contained in:
2026-11-30 04:06:00 +00:00
parent 949e1ab189
commit 4d81a37638
3 changed files with 40 additions and 10 deletions

View File

@@ -298,7 +298,12 @@ This changes lookup machinery, not the agreed Narratio roster requirements.
**Priority:** P3 **Priority:** P3
**Status:** For consideration **Status:** Complete
Analysis fingerprint ordering uses a standard-library lexical min-heap and no
longer sorts the ready queue on insertion. Dependency validation and the
smallest-ready-key order remain intact. Verified fingerprint, planning, and
execution graph tests with `go test ./internal/stage`.
**Stages:** 16 and 21; inherited analysis scheduling used by the cutover. **Stages:** 16 and 21; inherited analysis scheduling used by the cutover.

View File

@@ -1,6 +1,7 @@
package stage package stage
import ( import (
"container/heap"
"crypto/sha256" "crypto/sha256"
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
@@ -184,6 +185,20 @@ func hashAnalyzeFingerprintPayload(payload analyzeFingerprintPayload) (string, e
return hex.EncodeToString(digest[:]), nil 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( func orderConfiguredAnalyzeArtifacts(
configured map[string]config.ScriptoriumArtifactConfig, configured map[string]config.ScriptoriumArtifactConfig,
) ([]string, error) { ) ([]string, error) {
@@ -201,26 +216,21 @@ func orderConfiguredAnalyzeArtifacts(
indegree[key]++ indegree[key]++
} }
} }
for key := range edges { ready := make(analyzeReadyQueue, 0, len(indegree))
sort.Strings(edges[key])
}
ready := make([]string, 0, len(indegree))
for key, degree := range indegree { for key, degree := range indegree {
if degree == 0 { if degree == 0 {
ready = append(ready, key) ready = append(ready, key)
} }
} }
sort.Strings(ready) heap.Init(&ready)
order := make([]string, 0, len(indegree)) order := make([]string, 0, len(indegree))
for len(ready) > 0 { for len(ready) > 0 {
key := ready[0] key := heap.Pop(&ready).(string)
ready = ready[1:]
order = append(order, key) order = append(order, key)
for _, dependent := range edges[key] { for _, dependent := range edges[key] {
indegree[dependent]-- indegree[dependent]--
if indegree[dependent] == 0 { if indegree[dependent] == 0 {
ready = append(ready, dependent) heap.Push(&ready, dependent)
sort.Strings(ready)
} }
} }
} }

View File

@@ -1,6 +1,7 @@
package stage package stage
import ( import (
"fmt"
"path/filepath" "path/filepath"
"reflect" "reflect"
"strings" "strings"
@@ -12,6 +13,20 @@ import (
"gitea.maximumdirect.net/eric/narratio/internal/manifest" "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) { func TestAnalyzeFingerprintCanonicalPayloadSensitivity(t *testing.T) {
tests := []struct { tests := []struct {
name string name string