From aba7f0cc95d8f174799dd1493c98fc597acfe0af Mon Sep 17 00:00:00 2001 From: 0736b <76035626+0736b@users.noreply.github.com> Date: Thu, 26 Sep 2024 20:33:47 +0700 Subject: [PATCH] update: cached on keyword, toLower --- .gitignore | 2 ++ main.go | 11 +++++++++++ usecases/registryUsecase.go | 24 +++++++++++++++++++----- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 6f72f89..f3a8815 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,5 @@ go.work.sum # env file .env + +*.prof \ No newline at end of file diff --git a/main.go b/main.go index cac7ce3..65fbc91 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,17 @@ import ( func main() { + // f, err := os.Create("cpu.prof") + // if err != nil { + // log.Fatalln("failed to create cpu profile", err.Error()) + // } + // defer f.Close() + + // if err := pprof.StartCPUProfile(f); err != nil { + // log.Fatalln("failed to start cpu profile", err.Error()) + // } + // defer pprof.StopCPUProfile() + usecase := usecases.NewRegistryUsecase() app, err := gui.NewAppWindow(usecase) diff --git a/usecases/registryUsecase.go b/usecases/registryUsecase.go index a177400..8d54cc2 100644 --- a/usecases/registryUsecase.go +++ b/usecases/registryUsecase.go @@ -26,6 +26,9 @@ var ( keywordCache = make(map[string]string) keywordCacheMu sync.RWMutex + + toLowerCache = make(map[string]string) + toLowerCacheMu sync.RWMutex ) func NewRegistryUsecase() *RegistryUsecaseImpl { @@ -47,20 +50,31 @@ func (u *RegistryUsecaseImpl) FilterByKeyword(reg *entities.Registry, keyword st return true } + regStr := reg.Path + reg.Name + keywordCacheMu.RLock() - processedKeyword, exists := keywordCache[keyword] + processedKeyword, keyExists := keywordCache[keyword] keywordCacheMu.RUnlock() - if !exists { + toLowerCacheMu.RLock() + processedReg, regExists := toLowerCache[regStr] + toLowerCacheMu.RUnlock() + + if !keyExists { processedKeyword = utils.PreProcessStr(keyword) keywordCacheMu.Lock() keywordCache[keyword] = processedKeyword keywordCacheMu.Unlock() } - return strings.Contains(utils.PreProcessStr(reg.Path), processedKeyword) || - strings.Contains(utils.PreProcessStr(reg.Name), processedKeyword) || - strings.Contains(utils.PreProcessStr(reg.Value), processedKeyword) + if !regExists { + processedReg = utils.PreProcessStr(regStr + reg.Value) + toLowerCacheMu.Lock() + toLowerCache[regStr] = processedReg + toLowerCacheMu.Unlock() + } + + return strings.Contains(processedReg, processedKeyword) } func (u *RegistryUsecaseImpl) FilterByKey(reg *entities.Registry, filterKey string) bool {