77 "strings"
88 "unsafe"
99
10- "github.com/hephbuild/heph/internal/hproto/hashpb"
11-
1210 "github.com/hephbuild/heph/internal/hsync"
1311
1412 cache "github.com/Code-Hex/go-generics-cache"
@@ -22,28 +20,12 @@ import (
2220type Ref = pluginv1.TargetRef
2321type RefOut = pluginv1.TargetRefWithOutput
2422
25- var _ Refable = (* Ref )(nil )
26- var _ Refable = (* RefOut )(nil )
27- var _ RefableOut = (* RefOut )(nil )
28-
29- type Refable interface {
30- GetArgs () map [string ]string
31- GetPackage () string
32- GetName () string
33- }
34-
3523type HashStore interface {
3624 GetHash () uint64
3725 HasHash () bool
3826 SetHash (uint64 )
3927}
4028
41- type RefableOut interface {
42- Refable
43- GetOutput () string
44- GetFilters () []string
45- }
46-
4729func FormatFile (pkg string , file string ) string {
4830 return Format (New (JoinPackage ("@heph/file" , pkg ), "content" , map [string ]string {"f" : file }))
4931}
@@ -117,89 +99,84 @@ func ParseQuery(ref *pluginv1.TargetRef) (QueryOptions, error) {
11799var formatCache = cache.New [uint64 , string ](cache.AsLFU [uint64 , string ](lfu .WithCapacity (10000 )))
118100var formatSf = hsingleflight.Group [uint64 , string ]{}
119101
120- var formatHashPool = hsync.Pool [* xxh3.Hasher ]{New : xxh3 .New }
121-
122- func sumRef (ref hashpb.StableWriter ) uint64 {
123- h := formatHashPool .Get ()
124- defer formatHashPool .Put (h )
125- h .Reset ()
102+ var formatOutCache = cache.New [uint64 , string ](cache.AsLFU [uint64 , string ](lfu .WithCapacity (10000 )))
103+ var formatOutSf = hsingleflight.Group [uint64 , string ]{}
126104
127- switch ref := ref .(type ) {
128- case * pluginv1.TargetRef :
129- sumRefTargetRef (h , ref )
130- case * pluginv1.TargetRefWithOutput :
131- sumRefTargetRefWithOutput (h , ref )
132- default :
133- hashpb .Hash (h , ref , nil )
134- }
105+ var formatHashPool = hsync.Pool [* xxh3.Hasher ]{New : xxh3 .New }
135106
136- return h .Sum64 ()
137- }
107+ func sumRefTargetRef (m * pluginv1.TargetRef ) uint64 {
108+ hasher := formatHashPool .Get ()
109+ defer formatHashPool .Put (hasher )
110+ hasher .Reset ()
138111
139- func sumRefTargetRef (hasher * xxh3.Hasher , m * pluginv1.TargetRef ) {
140112 _ , _ = hasher .Write (unsafe .Slice (unsafe .StringData (m .GetPackage ()), len (m .GetPackage ())))
141113 _ , _ = hasher .Write (unsafe .Slice (unsafe .StringData (m .GetName ()), len (m .GetName ())))
142114 for k , v := range hmaps .Sorted (m .GetArgs ()) {
143115 _ , _ = hasher .Write (unsafe .Slice (unsafe .StringData (k ), len (k )))
144116 _ , _ = hasher .Write (unsafe .Slice (unsafe .StringData (v ), len (v )))
145117 }
118+
119+ return hasher .Sum64 ()
146120}
147121
148- func sumRefTargetRefWithOutput (hasher * xxh3.Hasher , m * pluginv1.TargetRefWithOutput ) {
149- _ , _ = hasher .Write (unsafe .Slice (unsafe .StringData (m .GetPackage ()), len (m .GetPackage ())))
150- _ , _ = hasher .Write (unsafe .Slice (unsafe .StringData (m .GetName ()), len (m .GetName ())))
151- for k , v := range hmaps .Sorted (m .GetArgs ()) {
152- _ , _ = hasher .Write (unsafe .Slice (unsafe .StringData (k ), len (k )))
153- _ , _ = hasher .Write (unsafe .Slice (unsafe .StringData (v ), len (v )))
154- }
155- _ , _ = hasher .Write (unsafe .Slice (unsafe .StringData (m .GetOutput ()), len (m .GetOutput ())))
156- if len (m .GetFilters ()) > 0 {
157- for _ , v := range m .GetFilters () {
122+ func sumRefTargetRefWithOutput (m * pluginv1.TargetRefWithOutput ) uint64 {
123+ hasher := formatHashPool .Get ()
124+ defer formatHashPool .Put (hasher )
125+ hasher .Reset ()
126+
127+ {
128+ m := m .GetTarget ()
129+
130+ _ , _ = hasher .Write (unsafe .Slice (unsafe .StringData (m .GetPackage ()), len (m .GetPackage ())))
131+ _ , _ = hasher .Write (unsafe .Slice (unsafe .StringData (m .GetName ()), len (m .GetName ())))
132+ for k , v := range hmaps .Sorted (m .GetArgs ()) {
133+ _ , _ = hasher .Write (unsafe .Slice (unsafe .StringData (k ), len (k )))
158134 _ , _ = hasher .Write (unsafe .Slice (unsafe .StringData (v ), len (v )))
159135 }
160136 }
137+
138+ _ , _ = hasher .Write (unsafe .Slice (unsafe .StringData (m .GetOutput ()), len (m .GetOutput ())))
139+ for _ , v := range m .GetFilters () {
140+ _ , _ = hasher .Write (unsafe .Slice (unsafe .StringData (v ), len (v )))
141+ }
142+
143+ return hasher .Sum64 ()
161144}
162145
163- func Format (ref Refable ) string {
164- if refh , ok := ref .(hashpb.StableWriter ); ok {
165- var sum uint64
166- if s , ok := ref .(HashStore ); ok {
167- if s .HasHash () {
168- sum = s .GetHash ()
169- } else {
170- sum = sumRef (refh )
171- s .SetHash (sum )
172- }
173- } else {
174- sum = sumRef (refh )
175- }
146+ func Format (ref * Ref ) string {
147+ var sum uint64
148+ if ref .HasHash () {
149+ sum = ref .GetHash ()
150+ } else {
151+ sum = sumRefTargetRef (ref )
152+ ref .SetHash (sum )
153+ }
176154
155+ f , ok := formatCache .Get (sum )
156+ if ok {
157+ return f
158+ }
159+
160+ f , _ , _ = formatSf .Do (sum , func () (string , error ) {
177161 f , ok := formatCache .Get (sum )
178162 if ok {
179- return f
163+ return f , nil
180164 }
181165
182- f , _ , _ = formatSf .Do (sum , func () (string , error ) {
183- f , ok := formatCache .Get (sum )
184- if ok {
185- return f , nil
186- }
187-
188- f = format (ref )
166+ f = format (ref )
189167
190- formatCache .Set (sum , f )
168+ formatCache .Set (sum , f )
191169
192- return f , nil
193- })
170+ return f , nil
171+ })
194172
195- return f
196- }
173+ return f
197174
198- return format (ref )
199175}
200176
201- func format (ref Refable ) string {
177+ func format (ref * Ref ) string {
202178 var sb strings.Builder
179+
203180 sb .WriteString ("//" )
204181 sb .WriteString (ref .GetPackage ())
205182 sb .WriteString (":" )
@@ -225,24 +202,65 @@ func format(ref Refable) string {
225202 }
226203 }
227204
228- if ref , ok := ref .(RefableOut ); ok {
229- out := ref .GetOutput ()
230- if out != "" {
231- sb .WriteString ("|" )
232- sb .WriteString (out )
205+ return sb .String ()
206+ }
207+
208+ func FormatOut (ref * RefOut ) string {
209+ var sum uint64
210+ if ref .HasHash () {
211+ sum = ref .GetHash ()
212+ } else {
213+ sum = sumRefTargetRefWithOutput (ref )
214+ ref .SetHash (sum )
215+ }
216+
217+ f , ok := formatOutCache .Get (sum )
218+ if ok {
219+ return f
220+ }
221+
222+ f , _ , _ = formatOutSf .Do (sum , func () (string , error ) {
223+ f , ok := formatOutCache .Get (sum )
224+ if ok {
225+ return f , nil
233226 }
234227
235- if len (ref .GetFilters ()) > 0 {
236- sb .WriteString (" filters=" )
237- first := true
238- for _ , f := range ref .GetFilters () {
239- if ! first {
240- sb .WriteString ("," )
241- } else {
242- first = false
243- }
244- sb .WriteString (f )
228+ f = formatOut (ref )
229+
230+ formatOutCache .Set (sum , f )
231+
232+ return f , nil
233+ })
234+
235+ return f
236+
237+ }
238+
239+ func formatOut (ref * RefOut ) string {
240+ if ref .GetOutput () == "" && len (ref .GetFilters ()) == 0 {
241+ return Format (ref .GetTarget ())
242+ }
243+
244+ var sb strings.Builder
245+
246+ sb .WriteString (Format (ref .GetTarget ()))
247+
248+ out := ref .GetOutput ()
249+ if out != "" {
250+ sb .WriteString ("|" )
251+ sb .WriteString (out )
252+ }
253+
254+ if len (ref .GetFilters ()) > 0 {
255+ sb .WriteString (" filters=" )
256+ first := true
257+ for _ , f := range ref .GetFilters () {
258+ if ! first {
259+ sb .WriteString ("," )
260+ } else {
261+ first = false
245262 }
263+ sb .WriteString (f )
246264 }
247265 }
248266
0 commit comments