Skip to content

Commit

Permalink
Merge pull request #4 from 0736b/profiler
Browse files Browse the repository at this point in the history
update: cached on keyword, toLower
  • Loading branch information
0736b authored Sep 26, 2024
2 parents 0471f4b + aba7f0c commit 44bbdb2
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ go.work.sum

# env file
.env

*.prof
11 changes: 11 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 19 additions & 5 deletions usecases/registryUsecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ var (

keywordCache = make(map[string]string)
keywordCacheMu sync.RWMutex

toLowerCache = make(map[string]string)
toLowerCacheMu sync.RWMutex
)

func NewRegistryUsecase() *RegistryUsecaseImpl {
Expand All @@ -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 {
Expand Down

0 comments on commit 44bbdb2

Please sign in to comment.