Skip to content

Commit

Permalink
problem: difficult to debug slowness
Browse files Browse the repository at this point in the history
  • Loading branch information
gsovereignty committed Apr 20, 2024
1 parent f80d448 commit 39cece3
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 54 deletions.
106 changes: 54 additions & 52 deletions src/lib/snort_workers/master_worker.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { seedRelays } from '@/snort_workers/seed_relays';
import type { NostrEvent } from '@nostr-dev-kit/ndk';
import { NostrSystem, RequestBuilder, type QueryLike } from '@snort/system';
import { derived, writable } from 'svelte/store';
import { Command, FrontendData, WorkerData } from './types';
import { followsFromKind3, getNostrEvent, tagSplits } from './utils';
import type { NostrEvent } from '@nostr-dev-kit/ndk';
import WorkerPubkeys from './live_subs?worker';
import WorkerEvents from './fetch_events?worker';
import { seedRelays } from '@/snort_workers/seed_relays';
import type { Nostr } from 'nostr-tools';
import { Command, FrontendData, WorkerData } from './types';
import { execTime, followsFromKind3, getNostrEvent, tagSplits } from './utils';

let workerData = new WorkerData();
let workerDataStore = writable(workerData);
Expand All @@ -27,6 +25,7 @@ async function connect() {
}

workerDataStore.subscribe((data) => {
let end = execTime("28 workerDataStore.subscribe")
let fed = new FrontendData();
fed.basePubkey = data.ourPubkey();
fed.baseFollows = data._ourFollows;
Expand Down Expand Up @@ -54,32 +53,44 @@ workerDataStore.subscribe((data) => {
});
fed.replies = data.replies;
fed.rawEvents = data.events;
//console.log(data)
postMessage(fed);
end()
});

let lengthOfFollows = derived(workerDataStore, ($wds) => {
return $wds._ourFollows.size;
});


let q_subToFollows: QueryLike
lengthOfFollows.subscribe((x) => {
console.log('follows updated');
if (x > 0) {
PermaSub([...workerData._ourFollows]);
const rb = new RequestBuilder('sub-to-follows');
rb.withFilter().authors([...workerData._ourFollows]).kinds([1])
rb.withOptions({ leaveOpen: true });
if (q_subToFollows) {q_subToFollows.cancel()}
q_subToFollows = sys.Query(rb);
q_subToFollows.on('event', (evs): void => {
let m = new Map<string, NostrEvent>()
for (let e of evs) {
m.set(e.id, e)
}
if (m.size > 0) {
updateReplies(m)
}
})
}
});

//contract:
onmessage = (m: MessageEvent<Command>) => {
console.log(58)
let end = execTime("88, onmessage")
if (m.data.command == 'start') {
start(m.data.pubkey)
.then(() => {})
.catch((err) => {
console.log(err);
});
}
if (m.data.command == 'push_event') {
console.log(96)
let map = new Map<string, NostrEvent>()
if (m.data.event) {
for (let e of m.data.event) {
Expand All @@ -92,6 +103,7 @@ onmessage = (m: MessageEvent<Command>) => {
}
}
}
end()
};

//connect to seed relays, get our follows and relays.
Expand Down Expand Up @@ -168,10 +180,11 @@ async function start(pubkey?: string, pubkeys?: string[]) {
//fetch all missing roots
//sort roots by number of responses from our follows

let permaSub: Worker | undefined = undefined;
//let permaSub: Worker | undefined = undefined;

function updateReplies(newEvents?:Map<string, NostrEvent>) {
workerDataStore.update((current) => {
let end = execTime("updateReplies")
if (newEvents) {
current.events = new Map([...newEvents, ...current.events]);
}
Expand Down Expand Up @@ -248,24 +261,29 @@ function updateReplies(newEvents?:Map<string, NostrEvent>) {
current.replies.set([...tagsForEvent.replies][0], existing);
}
}
end()
return current;
});
}

async function PermaSub(pubkeys: string[]) {
if (pubkeys.length > 0) {
if (permaSub) {
permaSub.terminate();
}
permaSub = new WorkerPubkeys();
permaSub.onmessage = (x: MessageEvent<Map<string, NostrEvent>>) => {
updateReplies(x.data)
};
let cmd = new Command('sub_to_pubkeys');
cmd.pubkeys = pubkeys;
permaSub.postMessage(cmd);
}
}


// async function PermaSub(pubkeys: string[]) {
// if (pubkeys.length > 0) {


// if (permaSub) {
// permaSub.terminate();
// }
// permaSub = new WorkerPubkeys();
// permaSub.onmessage = (x: MessageEvent<Map<string, NostrEvent>>) => {
// updateReplies(x.data)
// };
// let cmd = new Command('sub_to_pubkeys');
// cmd.pubkeys = pubkeys;
// permaSub.postMessage(cmd);
// }
// }

let numberOfMissingEvents = derived(workerDataStore, ($wds) => {
return $wds.missingEvents.size;
Expand All @@ -275,17 +293,16 @@ let numberOfMissingEvents = derived(workerDataStore, ($wds) => {



let q: QueryLike


let q_missingEvents: QueryLike
numberOfMissingEvents.subscribe((n) => {
let end = execTime("298 numberOfMissingEvents")
if (n > 0) {
const rb = new RequestBuilder('fetch-missing-events');
rb.withFilter().ids([...workerData.missingEvents])
rb.withOptions({ leaveOpen: false });
if (q) {q.cancel()}
q = sys.Query(rb);
q.on('event', (evs): void => {
if (q_missingEvents) {q_missingEvents.cancel()}
q_missingEvents = sys.Query(rb);
q_missingEvents.on('event', (evs): void => {
let m = new Map<string, NostrEvent>()
for (let e of evs) {
m.set(e.id, e)
Expand All @@ -294,25 +311,10 @@ numberOfMissingEvents.subscribe((n) => {
updateReplies(m)
}
})

// console.log(248, n)
// if (fetchEventsWorker) {
// fetchEventsWorker.terminate();
// }
// fetchEventsWorker = new WorkerEvents();
// fetchEventsWorker.onmessage = (x: MessageEvent<Map<string, NostrEvent>>) => {
// console.log(253, x.data.size);
// for (let [_, e] of x.data) {
// workerData.events.set(e.id!, e);
// }
// workerDataStore.update((x) => {
// return x;
// });
// };
// let cmd = new Command('sub_to_pubkeys');
// cmd.events = [...workerData.missingEvents];
// fetchEventsWorker.postMessage(cmd);
}
end()
});



export default {};
11 changes: 11 additions & 0 deletions src/lib/snort_workers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ import { NDKEvent, type NostrEvent } from '@nostr-dev-kit/ndk';
import type { TaggedNostrEvent } from '@snort/system';
import type { Event } from 'nostr-tools';

export let execTime = (name:string):()=>void => {
let start = performance.now()
let ended = false
setTimeout(()=>{if (!ended) {console.log(name, "has timed out")}}, 1000)
return () => {
let end = performance.now()
ended = true
console.log(name, end - start, "ms")
}
}

export function getNostrEvent(ev: TaggedNostrEvent): Event {
return {
id: ev.id,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/views/messages/Messages.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
<h3>HUMBLE HORSE</h3>
<h6>Release Name: "Giddy Up"</h6>
Events in memory: {$FrontendDataStore.rawEvents.size}<br />
<Button onClick={()=>{console.log($FrontendDataStore.replies.get($threadParentID))}}>Print root event data</Button>
<Button onClick={()=>{console.log($threadParentID, $FrontendDataStore.replies.get($threadParentID))}}>Print root event data</Button>

</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/routes/marcus/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</p>

<p class="text-sm font-normal text-gray-900 dark:text-white py-2">
When you click the Marcus button on any note, it will be saved here to your list of personal meditations so that I can remind you to think about them.
When you click the Marcus button on any note, it will be saved here to your list of personal meditations so that I can remind you to add them to your life.
</p>
</div>
</div>
Expand Down

0 comments on commit 39cece3

Please sign in to comment.