internal/pkg: cache computed identifiers
All checks were successful
Test / Create distribution (push) Successful in 49s
Test / Sandbox (push) Successful in 3m1s
Test / ShareFS (push) Successful in 4m56s
Test / Sandbox (race detector) (push) Successful in 5m21s
Test / Hpkg (push) Successful in 5m30s
Test / Hakurei (push) Successful in 5m53s
Test / Hakurei (race detector) (push) Successful in 7m56s
Test / Flake checks (push) Successful in 1m57s

This eliminates duplicate identifier computations. The new implementation also significantly reduces allocations while computing identifier for a large dependency tree.

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
2026-01-15 22:05:24 +09:00
parent 088d35e4e6
commit 3499a82785
9 changed files with 276 additions and 199 deletions

View File

@@ -2,7 +2,6 @@ package pkg
import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
@@ -101,8 +100,9 @@ func (a *execNetArtifact) Checksum() Checksum { return a.checksum }
func (a *execNetArtifact) Kind() Kind { return KindExecNet }
// Params is [Checksum] concatenated with [KindExec] params.
func (a *execNetArtifact) Params() []byte {
return slices.Concat(a.checksum[:], a.execArtifact.Params())
func (a *execNetArtifact) Params(ctx *IContext) {
ctx.GetHash().Write(a.checksum[:])
a.execArtifact.Params(ctx)
}
// Cure cures the [Artifact] in the container described by the caller. The
@@ -165,40 +165,40 @@ func NewExec(
// Kind returns the hardcoded [Kind] constant.
func (a *execArtifact) Kind() Kind { return KindExec }
// Params returns paths, executable pathname and args concatenated together.
func (a *execArtifact) Params() []byte {
var buf bytes.Buffer
// Params writes paths, executable pathname and args.
func (a *execArtifact) Params(ctx *IContext) {
h := ctx.GetHash()
_0, _1 := []byte{0}, []byte{1}
for _, p := range a.paths {
if p.W {
buf.WriteByte(1)
h.Write(_1)
} else {
buf.WriteByte(0)
h.Write(_0)
}
if p.P != nil {
buf.WriteString(p.P.String())
h.Write([]byte(p.P.String()))
} else {
buf.WriteString("invalid P\x00")
h.Write([]byte("invalid P\x00"))
}
buf.WriteByte(0)
h.Write(_0)
for _, d := range p.A {
id := Ident(d)
buf.Write(id[:])
ctx.WriteIdent(d)
}
buf.WriteByte(0)
h.Write(_0)
}
buf.WriteByte(0)
buf.WriteString(a.dir.String())
buf.WriteByte(0)
h.Write(_0)
h.Write([]byte(a.dir.String()))
h.Write(_0)
for _, e := range a.env {
buf.WriteString(e)
h.Write([]byte(e))
}
buf.WriteByte(0)
buf.WriteString(a.path.String())
buf.WriteByte(0)
h.Write(_0)
h.Write([]byte(a.path.String()))
h.Write(_0)
for _, arg := range a.args {
buf.WriteString(arg)
h.Write([]byte(arg))
}
return buf.Bytes()
}
// Dependencies returns a slice of all artifacts collected from caller-supplied