From bc7bbce83ae825b852fcc5cc9c343e700a19ceee Mon Sep 17 00:00:00 2001 From: Justin Reichardt Date: Sat, 20 Aug 2022 16:45:33 -0500 Subject: [PATCH] Manages handling fewer entries than cores safely If the number of entries is fewer than the number of cores then it is being assumed that you can run this on a single core. --- src/hosts/hosts.go | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/hosts/hosts.go b/src/hosts/hosts.go index 863d708..91ae04e 100644 --- a/src/hosts/hosts.go +++ b/src/hosts/hosts.go @@ -282,16 +282,21 @@ func removeUnwanted(siteBuff *[]siteList, whitelist *[]string) { // Removing duplicates log.Print("Checking for duplicates") cores := runtime.NumCPU() - fin := make(chan uint) - for i := cores; i > 0 ; i-- { - go removeDuplicate(entry[i-1:],cores, fin) - } - for ;cores > 0; cores-- { - count :=<-fin - c.d =+ count - } lenEntry := len(entry) + if (cores <= lenEntry){ + fin := make(chan uint) + for i := cores; i > 0 ; i-- { + go removeDuplicate(entry[i-1:],cores, fin) + } + for ;cores > 0; cores-- { + count :=<-fin + c.d =+ count + } + } else { + removeDuplicate(entry,1, nil) + } + log.Printf("Total: %d\tDuplicates: %d\tSafeWords: %d\tWhitelisted: %d\n", lenEntry, c.d, c.s, c.w) } @@ -316,7 +321,9 @@ func removeDuplicate (entry []siteEntryPointer, freq int , done chan uint){ } } - done <- cd + if (done != nil){ + done <- cd + } } // write2tmp write the siteBuff to the tempfile