Skip to content

Commit

Permalink
fix: small performance tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
v0l committed Dec 6, 2024
1 parent 897bb46 commit 0901e54
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 54 deletions.
7 changes: 5 additions & 2 deletions packages/app/src/Components/Toaster/Toaster.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,11 @@ class ToasterSlots extends ExternalStore<Array<ToastNotification>> {

#eatToast() {
const now = unixNow();
this.#stack = this.#stack.filter(a => (a.expire ?? 0) > now);
this.notifyChange();
const newStack = this.#stack.filter(a => (a.expire ?? 0) > now);
if (newStack.length !== this.#stack.length) {
this.#stack = newStack;
this.notifyChange();
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/Feed/WorkerRelayView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function useNotificationsView() {
leaveOpen: true,
});
if (publicKey) {
rb.withFilter().kinds(kinds).tag("p", [publicKey]);
rb.withFilter().kinds(kinds).tag("p", [publicKey]).limit(100);
}
return rb;
}, [publicKey]);
Expand Down
38 changes: 5 additions & 33 deletions packages/app/src/Hooks/useProfileSearch.tsx
Original file line number Diff line number Diff line change
@@ -1,43 +1,15 @@
import { useEffect, useMemo, useState } from "react";

import fuzzySearch from "@/Db/FuzzySearch";
import useTimelineFeed, { TimelineFeedOptions, TimelineSubject } from "@/Feed/TimelineFeed";
import { debounce } from "@/Utils";

import useWoT from "./useWoT";

const options: TimelineFeedOptions = { method: "LIMIT_UNTIL" };

export default function useProfileSearch(search: string) {
const [debouncedSearch, setDebouncedSearch] = useState(search);

useEffect(() => {
return debounce(500, () => {
setDebouncedSearch(search);
});
}, [search]);

const subject = useMemo(
() =>
({
type: "profile_keyword",
items: debouncedSearch ? [debouncedSearch] : [],
discriminator: debouncedSearch,
}) as TimelineSubject,
[debouncedSearch],
);
const feed = useTimelineFeed(subject, options);
const results = useMemo(() => {
return userSearch(search);
}, [search, feed]);

return results;
export default function useProfileSearch(search: string | undefined) {
return userSearch(search);
}

export function userSearch(search: string) {
export function userSearch(search: string | undefined) {
const wot = useWoT();
const searchString = search.trim();
const fuseResults = fuzzySearch.search(searchString);
const searchString = search?.trim() ?? "";
const fuseResults = (searchString?.length ?? 0) > 0 ? fuzzySearch.search(searchString) : [];

const followDistanceNormalizationFactor = 3;
const seenIds = new Set();
Expand Down
12 changes: 2 additions & 10 deletions packages/app/src/Pages/Layout/HasNotificationsMarker.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
import { useMemo } from "react";

import { useNotificationsView } from "@/Feed/WorkerRelayView";
import useLogin from "@/Hooks/useLogin";

export function HasNotificationsMarker() {
const readNotifications = useLogin(s => s.readNotifications);
const notifications = useNotificationsView();
const latestNotification = useMemo(
() => notifications.reduce((acc, v) => (v.created_at > acc ? v.created_at : acc), 0),
[notifications],
);
const hasNotifications = useMemo(
() => latestNotification * 1000 > readNotifications,
[notifications, readNotifications],
);
const latestNotification = 0; // TODO: get latest timestamp
const hasNotifications = useMemo(() => latestNotification * 1000 > readNotifications, [readNotifications]);

if (hasNotifications) {
return (
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/Pages/Layout/LogoHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const getExtra = () => {
};

export function LogoHeader({ showText = false }: { showText: boolean }) {
const { subscriptions } = useLogin();
const subscriptions = useLogin(s => s.subscriptions);
const currentSubscription = getCurrentSubscription(subscriptions);

const appName = CONFIG.appName === "iris" && isStPatricksDay() ? "Irish" : CONFIG.appName;
Expand Down
5 changes: 5 additions & 0 deletions packages/system/src/query-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ export class QueryManager extends EventEmitter<QueryManagerEvents> {
// check for empty filters
filters = trimFilters(filters);

if (filters.length === 0) {
this.#log("Dropping %s %o", q.id);
return;
}

// automated outbox model, load relays for queried authors
for (const f of filters) {
if (f.authors) {
Expand Down
6 changes: 1 addition & 5 deletions packages/worker-relay/src/sqlite/sqlite-relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,6 @@ export class SqliteRelay extends EventEmitter<RelayHandlerEvents> implements Rel
this.#pool = await this.#sqlite.installOpfsSAHPoolVfs({});
this.db = new this.#pool.OpfsSAHPoolDb(path);
this.#log(`Opened ${this.db.filename}`);
/*this.db.exec(
`PRAGMA cache_size=${32 * 1024
}; PRAGMA page_size=8192; PRAGMA journal_mode=MEMORY; PRAGMA temp_store=MEMORY;`,
);*/
}

/**
Expand Down Expand Up @@ -237,7 +233,7 @@ export class SqliteRelay extends EventEmitter<RelayHandlerEvents> implements Rel
};
}) ?? [];
const time = unixNowMs() - start;
this.#log(`Query ${id} results took ${time.toLocaleString()}ms`);
this.#log(`Query ${id} results took ${time.toLocaleString()}ms`, req);
return results;
}

Expand Down
2 changes: 0 additions & 2 deletions packages/worker-relay/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import { SqliteRelay } from "./sqlite/sqlite-relay";
import { InMemoryRelay } from "./memory-relay";
import { setLogging } from "./debug";
import { WorkQueueItem, barrierQueue, processWorkQueue } from "./queue";
import {
NostrEvent,
RelayHandler,
Expand Down Expand Up @@ -42,7 +41,6 @@ async function insertBatch() {
setTimeout(() => insertBatch(), 100);
}

const cmdQueue: Array<WorkQueueItem> = [];
try {
setTimeout(() => insertBatch(), 100);
} catch (e) {
Expand Down

0 comments on commit 0901e54

Please sign in to comment.