Skip to content

Commit ef82d0f

Browse files
committed
go/runtime/history: Avoid nested for loops
1 parent 3d23793 commit ef82d0f

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

go/runtime/history/prune.go

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -128,32 +128,38 @@ func (p *keepLastPruner) Prune(latestRound uint64) error {
128128
pruned = append(pruned, round)
129129
}
130130

131-
// If there is nothing to prune, do not call any handlers.
132-
if len(pruned) == 0 {
131+
if len(pruned) == 0 { // nothing to prune
133132
return nil
134133
}
135134

136-
// Before pruning anything, run all prune handlers. If any of them
137-
// fails we abort the prune.
138-
p.mu.RLock()
139-
defer p.mu.RUnlock()
140-
141-
for _, ph := range p.handlers {
142-
if err := ph.CanPruneRuntime(pruned); err != nil {
143-
p.logger.Debug("prune handler failed, aborting prune",
144-
"err", err,
145-
"round_count", len(pruned),
146-
"round_min", pruned[0],
147-
"round_max", pruned[len(pruned)-1],
148-
)
149-
return fmt.Errorf("runtime/history: prune handler failed: %w", err)
150-
}
135+
if err := p.canPrune(pruned); err != nil {
136+
p.logger.Debug("prune handler blocked pruning, aborting prune",
137+
"err", err,
138+
"round_count", len(pruned),
139+
"round_min", pruned[0],
140+
"round_max", pruned[len(pruned)-1], // safe due to length check above
141+
)
142+
return fmt.Errorf("prune handler blocked pruning: %w", err)
151143
}
152144

153145
return nil
154146
})
155147
}
156148

149+
// canPrune checks if all prune handlers allow pruning specified rounds.
150+
func (p *keepLastPruner) canPrune(rounds []uint64) error {
151+
p.mu.RLock()
152+
defer p.mu.RUnlock()
153+
154+
for _, ph := range p.handlers {
155+
if err := ph.CanPruneRuntime(rounds); err != nil {
156+
return err
157+
}
158+
}
159+
160+
return nil
161+
}
162+
157163
// PruneInterval implements Pruner.
158164
func (p *keepLastPruner) PruneInterval() time.Duration {
159165
return p.pruneInterval

0 commit comments

Comments
 (0)