optimisation: make Memoise(Memoise(...)) only memoise once

All outputs of Memoise() are fixpoints to the function, i.e. feeding
them as inputs to the function just returns them.

We don't hit this optimisation in current code but have had instances
in the past.
This commit is contained in:
Matthias Radestock
2017-11-23 17:03:33 +00:00
parent cfac26757f
commit 5bdf46956d
2 changed files with 8 additions and 0 deletions

View File

@@ -27,6 +27,9 @@ type memoise struct {
// Memoise wraps the renderer in a loving embrace of caching.
func Memoise(r Renderer) Renderer {
if _, ok := r.(*memoise); ok {
return r // fixpoint
}
return &memoise{
Renderer: r,
id: fmt.Sprintf("%x", rand.Int63()),

View File

@@ -20,6 +20,11 @@ func TestMemoise(t *testing.T) {
return render.Nodes{Nodes: report.Nodes{rpt.ID: report.MakeNode(rpt.ID)}}
})
m := render.Memoise(r)
if render.Memoise(m) != m {
t.Errorf("Memoised renderers should be fixpoints.")
}
rpt1 := report.MakeReport()
result1 := m.Render(rpt1)