use binary search in RestrictToSpecifiedKinds()

This commit is contained in:
fiatjaf 2023-11-18 23:23:01 -03:00
parent dcdf86c4e4
commit 5db3b5fb8b
No known key found for this signature in database
GPG Key ID: BAD43C4BE5C1A3A1

View File

@ -64,9 +64,7 @@ func PreventLargeTags(maxTagValueLen int) func(context.Context, *nostr.Event) (b
func RestrictToSpecifiedKinds(kinds ...uint16) func(context.Context, *nostr.Event) (bool, string) {
max := 0
min := 0
allowed := make(map[uint16]struct{}, len(kinds))
for _, kind := range kinds {
allowed[kind] = struct{}{}
if int(kind) > max {
max = int(kind)
}
@ -86,7 +84,7 @@ func RestrictToSpecifiedKinds(kinds ...uint16) func(context.Context, *nostr.Even
}
// hopefully this map of uint16s is very fast
if _, allowed := allowed[uint16(event.Kind)]; allowed {
if _, allowed := slices.BinarySearch(kinds, uint16(event.Kind)); allowed {
return false, ""
}
return true, "event kind not allowed"