From 709c48d013cc80e17f4f70ac4926d5868780534a Mon Sep 17 00:00:00 2001 From: snackmgmg <16898622+snackmgmg@users.noreply.github.com> Date: Wed, 20 Mar 2024 14:01:53 +0900 Subject: [PATCH] reduce number of loops --- callbacks.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/callbacks.go b/callbacks.go index 54a4be8575..79eef6d39a 100644 --- a/callbacks.go +++ b/callbacks.go @@ -187,20 +187,20 @@ func (p *processor) Replace(name string, fn func(*DB)) error { func (p *processor) compile() (err error) { var ( - callbacks []*callback - removed []string + callbacks []*callback + removedMap map[string]bool ) for _, callback := range p.callbacks { if callback.match == nil || callback.match(p.db) { callbacks = append(callbacks, callback) } if callback.remove { - removed = append(removed, callback.name) + removedMap[callback.name] = true } } - if len(removed) > 0 { - callbacks = removeCallbacks(callbacks, removed) + if len(removedMap) > 0 { + callbacks = removeCallbacks(callbacks, removedMap) } p.callbacks = callbacks @@ -351,10 +351,10 @@ func sortCallbacks(cs []*callback) (fns []func(*DB), err error) { return } -func removeCallbacks(cs []*callback, names []string) []*callback { +func removeCallbacks(cs []*callback, nameMap map[string]bool) []*callback { callbacks := make([]*callback, 0, len(cs)) for _, callback := range cs { - if utils.Contains(names, callback.name) { + if nameMap[callback.name] { continue } callbacks = append(callbacks, callback)