perf(tui): cache Detect+Plan results in wizard renderPlan

This commit is contained in:
MDietermann
2026-04-14 08:18:38 +02:00
parent 05dc8aa7f5
commit 83bb622232
2 changed files with 125 additions and 28 deletions

View File

@@ -44,6 +44,7 @@ func Run(ctx context.Context, reg *module.Registry, initial tui.WizardState) (tu
selectedModules := defaults
parallelismStr := fmt.Sprintf("%d", s.Parallelism)
var proceed bool
cache := newPlanCache()
form := huh.NewForm(
huh.NewGroup(huh.NewNote().
@@ -74,7 +75,7 @@ func Run(ctx context.Context, reg *module.Registry, initial tui.WizardState) (tu
Title("Review plan").
DescriptionFunc(func() string {
s.Modules = syncSelected(s.Modules, selectedModules)
return renderPlan(ctx, reg, s.SelectedIDs())
return renderPlanCached(ctx, reg, s.SelectedIDs(), cache)
}, &selectedModules)),
huh.NewGroup(huh.NewConfirm().
@@ -115,38 +116,75 @@ func syncSelected(mods []tui.ModuleChoice, selected []string) []tui.ModuleChoice
return out
}
// renderPlan runs Detect + Plan for every selected module and renders a
// human-readable summary. A failing Detect/Plan is shown inline but does
// not abort the wizard.
func renderPlan(ctx context.Context, reg *module.Registry, selectedIDs []string) string {
// planCache memoises rendered Detect+Plan output per module ID so
// DescriptionFunc (which fires on every redraw) does not re-run
// potentially expensive Detect/Plan calls. Entries are evicted when a
// module leaves the current selection.
type planCacheEntry struct{ out string }
type planCache struct{ entries map[string]planCacheEntry }
func newPlanCache() *planCache { return &planCache{entries: map[string]planCacheEntry{}} }
func (c *planCache) get(id string) (string, bool) {
e, ok := c.entries[id]
return e.out, ok
}
func (c *planCache) put(id, out string) { c.entries[id] = planCacheEntry{out: out} }
func (c *planCache) prune(keepIDs []string) {
keep := map[string]bool{}
for _, id := range keepIDs {
keep[id] = true
}
for k := range c.entries {
if !keep[k] {
delete(c.entries, k)
}
}
}
// renderPlanCached is the cached equivalent of running Detect+Plan for
// every selected module and joining the rendered lines. IDs no longer in
// the selection are evicted so a deselect+reselect re-runs Detect.
func renderPlanCached(ctx context.Context, reg *module.Registry, selectedIDs []string, cache *planCache) string {
cache.prune(selectedIDs)
if len(selectedIDs) == 0 {
return "No modules selected."
}
var b strings.Builder
for _, id := range selectedIDs {
m, ok := reg.Get(id)
if !ok {
fmt.Fprintf(&b, "• %s — (unknown)\n", id)
if cached, ok := cache.get(id); ok {
b.WriteString(cached)
continue
}
state, err := m.Detect(ctx)
if err != nil {
fmt.Fprintf(&b, "• %s — detect failed: %v\n", id, err)
continue
}
plan, err := m.Plan(ctx, state)
if err != nil {
fmt.Fprintf(&b, "• %s — plan failed: %v\n", id, err)
continue
}
if plan.IsEmpty() {
fmt.Fprintf(&b, "• %s — already up to date\n", id)
continue
}
fmt.Fprintf(&b, "• %s\n", id)
for _, a := range plan.Actions {
fmt.Fprintf(&b, " - %s\n", a.Description)
}
line := renderOne(ctx, reg, id)
cache.put(id, line)
b.WriteString(line)
}
return b.String()
}
// renderOne runs Detect+Plan for a single module and returns a rendered
// line. A failing Detect/Plan is shown inline but does not abort.
func renderOne(ctx context.Context, reg *module.Registry, id string) string {
m, ok := reg.Get(id)
if !ok {
return fmt.Sprintf("• %s — (unknown)\n", id)
}
state, err := m.Detect(ctx)
if err != nil {
return fmt.Sprintf("• %s — detect failed: %v\n", id, err)
}
plan, err := m.Plan(ctx, state)
if err != nil {
return fmt.Sprintf("• %s — plan failed: %v\n", id, err)
}
if plan.IsEmpty() {
return fmt.Sprintf("• %s — already up to date\n", id)
}
var b strings.Builder
fmt.Fprintf(&b, "• %s\n", id)
for _, a := range plan.Actions {
fmt.Fprintf(&b, " - %s\n", a.Description)
}
return b.String()
}