aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/pkg/file.go
diff options
context:
space:
mode:
authorOphestra <cat@gensokyo.uk>2026-01-15 22:05:24 +0900
committerOphestra <cat@gensokyo.uk>2026-01-15 23:30:43 +0900
commit3499a82785f8cb82bfe528875391d76d5e03c3de (patch)
tree66d82b1a95fc3137cfb7a2ffa82af0d84b613aa0 /internal/pkg/file.go
parent088d35e4e65f1a22d32ac66d223672ed923fa328 (diff)
internal/pkg: cache computed identifiers
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>
Diffstat (limited to 'internal/pkg/file.go')
-rw-r--r--internal/pkg/file.go25
1 files changed, 13 insertions, 12 deletions
diff --git a/internal/pkg/file.go b/internal/pkg/file.go
index 98b8eee6..4558eb40 100644
--- a/internal/pkg/file.go
+++ b/internal/pkg/file.go
@@ -9,7 +9,7 @@ import (
// A fileArtifact is an [Artifact] that cures into data known ahead of time.
type fileArtifact []byte
-var _ KnownChecksum = fileArtifact{}
+var _ KnownChecksum = new(fileArtifact)
// fileArtifactNamed embeds fileArtifact alongside a caller-supplied name.
type fileArtifactNamed struct {
@@ -18,10 +18,11 @@ type fileArtifactNamed struct {
name string
}
-var _ fmt.Stringer = fileArtifactNamed{}
+var _ fmt.Stringer = new(fileArtifactNamed)
+var _ KnownChecksum = new(fileArtifactNamed)
// String returns the caller-supplied reporting name.
-func (a fileArtifactNamed) String() string { return a.name }
+func (a *fileArtifactNamed) String() string { return a.name }
// NewFile returns a [File] that cures into a caller-supplied byte slice.
//
@@ -29,26 +30,26 @@ func (a fileArtifactNamed) String() string { return a.name }
func NewFile(name string, data []byte) File {
f := fileArtifact(data)
if name != "" {
- return fileArtifactNamed{f, name}
+ return &fileArtifactNamed{f, name}
}
- return f
+ return &f
}
// Kind returns the hardcoded [Kind] constant.
-func (a fileArtifact) Kind() Kind { return KindFile }
+func (a *fileArtifact) Kind() Kind { return KindFile }
-// Params returns the result of Data.
-func (a fileArtifact) Params() []byte { return a }
+// Params writes the result of Cure.
+func (a *fileArtifact) Params(ctx *IContext) { ctx.GetHash().Write(*a) }
// Dependencies returns a nil slice.
-func (a fileArtifact) Dependencies() []Artifact { return nil }
+func (a *fileArtifact) Dependencies() []Artifact { return nil }
// Checksum computes and returns the checksum of caller-supplied data.
-func (a fileArtifact) Checksum() Checksum {
+func (a *fileArtifact) Checksum() Checksum {
h := sha512.New384()
- h.Write(a)
+ h.Write(*a)
return Checksum(h.Sum(nil))
}
// Cure returns the caller-supplied data.
-func (a fileArtifact) Cure(context.Context) ([]byte, error) { return a, nil }
+func (a *fileArtifact) Cure(context.Context) ([]byte, error) { return *a, nil }