faster "event seen" check

This commit is contained in:
Martti Malmi 2023-03-31 12:34:45 +03:00
parent 78d51c1bb3
commit e94a9963ae
3 changed files with 17 additions and 4 deletions

View File

@ -46,6 +46,7 @@ const Events = {
DEFAULT_GLOBAL_FILTER,
getEventHash,
db: events,
seen: new Set<string>(),
deletedEvents: new Set<string>(),
directMessagesByUser: new Map<string, SortedLimitedEventSet>(),
latestNotificationByTargetAndKind: new Map<string, string>(),
@ -411,7 +412,7 @@ const Events = {
},
handle(event: Event & { id: string }, force = false, saveToIdb = true): boolean {
if (!event) return;
if (!force && !!this.db.by('id', event.id)) {
if (!force && this.seen.has(event.id)) {
return false;
}
if (!force && !this.acceptEvent(event)) {
@ -437,6 +438,8 @@ const Events = {
return false;
}
this.seen.add(event.id);
this.handledMsgsPerSecond++;
PubSub.subscribedEventIds.delete(event.id);

View File

@ -23,8 +23,18 @@ let dev: any = {
};
const relayPool = new RelayPool(Relays.DEFAULT_RELAYS, {
useEventCache: false,
externalGetEventById: (id) => Events.db.by('id', id),
externalGetEventById: (id) =>
(Events.seen.has(id) && {
// make externalGetEventById take booleans instead of events?
sig: '',
id: '',
kind: 0,
tags: [],
content: '',
created_at: 0,
pubkey: '',
}) ||
undefined,
});
localState.get('dev').on((d) => {
dev = d;

View File

@ -307,7 +307,7 @@ const Relays = {
},
relayInit(url: string, subscribeAll = true) {
const relay = relayInit(url, (id) => {
const have = !!Events.db.by('id', id);
const have = !!Events.seen.has(id);
// console.log('have?', have);
return have;
});