mirror of
https://github.com/aljazceru/njump.git
synced 2025-12-18 06:44:22 +01:00
refactor unique() to be faster.
This commit is contained in:
8
nostr.go
8
nostr.go
@@ -290,12 +290,11 @@ func relaysForPubkey(ctx context.Context, pubkey string, extraRelays ...string)
|
|||||||
cache.SetJSONWithTTL("io:"+pubkey, pubkeyRelays, time.Hour*24*7)
|
cache.SetJSONWithTTL("io:"+pubkey, pubkeyRelays, time.Hour*24*7)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pubkeyRelays = unique(pubkeyRelays)
|
return unique(pubkeyRelays)
|
||||||
return pubkeyRelays
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func contactsForPubkey(ctx context.Context, pubkey string, extraRelays ...string) []string {
|
func contactsForPubkey(ctx context.Context, pubkey string, extraRelays ...string) []string {
|
||||||
pubkeyContacts := make([]string, 0, 100)
|
pubkeyContacts := make([]string, 0, 300)
|
||||||
relays := make([]string, 0, 12)
|
relays := make([]string, 0, 12)
|
||||||
if ok := cache.GetJSON("cc:"+pubkey, &pubkeyContacts); !ok {
|
if ok := cache.GetJSON("cc:"+pubkey, &pubkeyContacts); !ok {
|
||||||
log.Debug().Msgf("searching contacts for %s", pubkey)
|
log.Debug().Msgf("searching contacts for %s", pubkey)
|
||||||
@@ -335,6 +334,5 @@ func contactsForPubkey(ctx context.Context, pubkey string, extraRelays ...string
|
|||||||
cache.SetJSONWithTTL("cc:"+pubkey, pubkeyContacts, time.Hour*6)
|
cache.SetJSONWithTTL("cc:"+pubkey, pubkeyContacts, time.Hour*6)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pubkeyContacts = unique(pubkeyContacts)
|
return unique(pubkeyContacts)
|
||||||
return pubkeyContacts
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -65,7 +65,6 @@ func loadNpubsArchive(ctx context.Context) {
|
|||||||
log.Debug().Msg("refreshing the npubs archive")
|
log.Debug().Msg("refreshing the npubs archive")
|
||||||
|
|
||||||
contactsArchive := make([]string, 0, 500)
|
contactsArchive := make([]string, 0, 500)
|
||||||
|
|
||||||
for _, pubkey := range trustedPubKeys {
|
for _, pubkey := range trustedPubKeys {
|
||||||
ctx, cancel := context.WithTimeout(ctx, time.Second*4)
|
ctx, cancel := context.WithTimeout(ctx, time.Second*4)
|
||||||
pubkeyContacts := contactsForPubkey(ctx, pubkey)
|
pubkeyContacts := contactsForPubkey(ctx, pubkey)
|
||||||
@@ -73,8 +72,7 @@ func loadNpubsArchive(ctx context.Context) {
|
|||||||
cancel()
|
cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
contactsArchive = unique(contactsArchive)
|
for _, contact := range unique(contactsArchive) {
|
||||||
for _, contact := range contactsArchive {
|
|
||||||
log.Debug().Msgf("adding contact %s", contact)
|
log.Debug().Msgf("adding contact %s", contact)
|
||||||
cache.SetWithTTL("pa:"+contact, nil, time.Hour*24*90)
|
cache.SetWithTTL("pa:"+contact, nil, time.Hour*24*90)
|
||||||
}
|
}
|
||||||
@@ -92,8 +90,7 @@ func loadRelaysArchive(ctx context.Context) {
|
|||||||
cancel()
|
cancel()
|
||||||
}
|
}
|
||||||
|
|
||||||
relaysArchive = unique(relaysArchive)
|
for _, relay := range unique(relaysArchive) {
|
||||||
for _, relay := range relaysArchive {
|
|
||||||
for _, excluded := range excludedRelays {
|
for _, excluded := range excludedRelays {
|
||||||
if strings.Contains(relay, excluded) {
|
if strings.Contains(relay, excluded) {
|
||||||
log.Debug().Msgf("skipping relay %s", relay)
|
log.Debug().Msgf("skipping relay %s", relay)
|
||||||
|
|||||||
15
utils.go
15
utils.go
@@ -14,6 +14,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/microcosm-cc/bluemonday"
|
"github.com/microcosm-cc/bluemonday"
|
||||||
|
"golang.org/x/exp/slices"
|
||||||
"mvdan.cc/xurls/v2"
|
"mvdan.cc/xurls/v2"
|
||||||
|
|
||||||
"github.com/nbd-wtf/go-nostr"
|
"github.com/nbd-wtf/go-nostr"
|
||||||
@@ -381,15 +382,15 @@ func previewNotesFormatting(input string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func unique(strSlice []string) []string {
|
func unique(strSlice []string) []string {
|
||||||
keys := make(map[string]bool)
|
slices.Sort(strSlice)
|
||||||
list := []string{}
|
j := 0
|
||||||
for _, entry := range strSlice {
|
for i := 1; i < len(strSlice); i++ {
|
||||||
if _, ok := keys[entry]; !ok {
|
if strSlice[j] != strSlice[i] {
|
||||||
keys[entry] = true
|
j++
|
||||||
list = append(list, entry)
|
strSlice[j] = strSlice[i]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return list
|
return strSlice[:j+1]
|
||||||
}
|
}
|
||||||
|
|
||||||
func trimProtocol(relay string) string {
|
func trimProtocol(relay string) string {
|
||||||
|
|||||||
Reference in New Issue
Block a user