Skip to content

Build your personal knowledge base with Trilium Notes

chore(mobile): remove the save and backup timing logs

Elian Doran1ee3c376479c
diff --git a/.claude/skills/developing-capacitor-mobile/SKILL.md b/.claude/skills/developing-capacitor-mobile/SKILL.md
index 79a24e1653..81de59f1d6 100644
--- a/.claude/skills/developing-capacitor-mobile/SKILL.md
+++ b/.claude/skills/developing-capacitor-mobile/SKILL.md
@@ -93,11 +93,11 @@ sheet.
`capacitor.config.json` (iOS only builds what is listed), and `cap update android` to regenerate the
tracked `capacitor.settings.gradle` / `app/capacitor.build.gradle`. iOS's `CapApp-SPM/Package.swift`
is gitignored and regenerated by CI.
-- **Throughput is measured, not guessed.** Every save logs one `[Save]` line (total, MB/s, encode vs
- transport-wait split, transport used) and the worker logs a `[Backup]` line for backups (reads vs
- waiting-on-consumer vs hashing/crypto). Read those before optimizing: the remaining backup-side
- cost on a phone is the container's incremental SHA-256, which is `@noble/hashes` pure JS
- (`packages/trilium-backup-container/src/backend-web.ts`) because WebCrypto has no streaming digest.
+- **The known cost profile** (1.8 GB backup on a mid-range phone, Sept 2026): ~16s, split evenly
+ between SQLite page reads and the container's WASM SHA-256 (`hash-wasm` in
+ `packages/trilium-backup-container/src/backend-web.ts`); the sink's writes are not a factor. The
+ base64 plugin bridge caps around 13 MB/s (`MessageHandler` re-parses each call's JSON whole with
+ `org.json`), which is why the sink exists.
- **The database backup takes the same last step, from a different source.** `saveDatabase()` in
`local-bridge.ts` consumes the worker's `BACKUP_STREAM` channel in the page rather than relaying the
port to the service worker, so no SW is involved (which is also why it works on iOS), and
diff --git a/apps/standalone/src/lightweight/backup-download.ts b/apps/standalone/src/lightweight/backup-download.ts
index e95683c9fa..1d103f0dd3 100644
--- a/apps/standalone/src/lightweight/backup-download.ts
+++ b/apps/standalone/src/lightweight/backup-download.ts
@@ -6,8 +6,7 @@ import {
import {
type LiveDatabaseReader,
- streamLiveDatabasePages,
- type StreamTiming
+ streamLiveDatabasePages
} from "./backup-stream.js";
/**
@@ -130,23 +129,13 @@ export async function streamDatabaseDownload(
return requests.shift() ?? "cancel";
};
- /**
- * How long was spent with a chunk ready and nowhere to put it.
- *
- * The one thing this side cannot make faster: the browser asks for the next piece as it commits
- * the last one to disk, so a large figure here means the download itself is the limit and no
- * amount of reading or hashing faster would show up in the total.
- */
- let waitedMs = 0;
let sentBytes = 0;
let totalBytes = 0;
let reportedAt = 0;
/** Posts one chunk per granted pull, or throws the stop that was granted instead. */
const sendChunk = async (chunk: Uint8Array): Promise<void> => {
- const waitingSince = Date.now();
const request = await nextRequest();
- waitedMs += Date.now() - waitingSince;
if (request !== "pull") {
throw new DownloadStopped(request);
@@ -169,9 +158,8 @@ export async function streamDatabaseDownload(
};
let source: ReadableStream<Uint8Array> | undefined;
- const startedAt = Date.now();
try {
- const { byteSize, stream, timing } = streamLiveDatabasePages(db);
+ const { byteSize, stream } = streamLiveDatabasePages(db);
source = stream;
// A container either way, so every backup this application writes has one shape and one
@@ -196,7 +184,6 @@ export async function streamDatabaseDownload(
// The last one is unconditional: a throttled report would otherwise leave the screen a
// chunk short of the total it is about to be told is finished.
options.onProgress?.(sentBytes, totalBytes);
- reportTiming(byteSize, timing, waitedMs, Date.now() - startedAt);
return { status: "done" };
} catch (e) {
@@ -220,34 +207,6 @@ export async function streamDatabaseDownload(
}
}
-/**
- * One line saying where a backup's time went, split three ways so a slow one can be acted on.
- *
- * `read` is the database coming out through SQLite, `waiting` is having a chunk ready and the
- * download not yet asking for it, and what neither accounts for is the hashing and encryption in
- * between. Only the first is worth optimising: waiting is the browser committing the file to disk,
- * which is the floor, and the rest is a hash the format cannot do without.
- */
-function reportTiming(
- byteSize: number,
- timing: StreamTiming,
- waitedMs: number,
- totalMs: number
-): void {
- const mib = byteSize / (1024 * 1024);
- const rate = (ms: number) => (ms > 0 ? mib / (ms / 1000) : 0).toFixed(1);
- const share = (ms: number) => Math.round(100 * ms / Math.max(totalMs, 1));
- const restMs = Math.max(0, totalMs - timing.readMs - waitedMs);
-
- console.log(
- `[Backup] ${mib.toFixed(0)} MiB in ${(totalMs / 1000).toFixed(1)}s (${rate(totalMs)} MiB/s). `
- + `Reads ${(timing.readMs / 1000).toFixed(1)}s (${share(timing.readMs)}%, `
- + `${rate(timing.readMs)} MiB/s over ${timing.pages} pages), `
- + `waiting on the download ${(waitedMs / 1000).toFixed(1)}s (${share(waitedMs)}%), `
- + `hashing and crypto ${(restMs / 1000).toFixed(1)}s (${share(restMs)}%).`
- );
-}
-
/** A best-effort message: the port may already be gone, and this report is all that would say so. */
function post(port: DownloadPort, message: unknown): void {
try {
diff --git a/apps/standalone/src/lightweight/backup-stream.ts b/apps/standalone/src/lightweight/backup-stream.ts
index 2e8f9ad3eb..4df1416059 100644
--- a/apps/standalone/src/lightweight/backup-stream.ts
+++ b/apps/standalone/src/lightweight/backup-stream.ts
@@ -32,14 +32,6 @@ export interface LiveDatabaseReader {
getColumn(sql: string, params?: unknown[]): unknown[];
}
-/** What the read cost, for a caller that wants to say where a slow backup spent its time. */
-export interface StreamTiming {
- /** Milliseconds spent inside the page reads, as against waiting on whatever consumes them. */
- readMs: number;
- /** Pages handed over so far. */
- pages: number;
-}
-
/** Thrown by {@link streamLiveDatabasePages} when a write lands mid-stream. The caller retries. */
export class DatabaseChangedError extends Error {
constructor() {
@@ -52,8 +44,6 @@ export interface DatabaseStream {
/** Exact size of the streamed database, known before the first byte flows. */
byteSize: number;
stream: ReadableStream<Uint8Array>;
- /** Updated as the stream runs, so a caller can report where the time went. */
- timing: StreamTiming;
}
/**
@@ -75,15 +65,12 @@ export function streamLiveDatabasePages(db: LiveDatabaseReader): DatabaseStream
const pageCount = countOf(db.getValue("PRAGMA page_count"), "page_count");
const fingerprint = fingerprintOf(db);
- const timing: StreamTiming = { readMs: 0, pages: 0 };
let nextPage = 1;
return {
byteSize: pageSize * pageCount,
- timing,
stream: new ReadableStream<Uint8Array>({
pull(controller) {
- const startedAt = Date.now();
try {
if (fingerprintOf(db) !== fingerprint) {
throw new DatabaseChangedError();
@@ -96,13 +83,10 @@ export function streamLiveDatabasePages(db: LiveDatabaseReader): DatabaseStream
const pages = Math.min(BATCH_PAGES, pageCount - nextPage + 1);
const batch = readLivePages(db, nextPage, pages, pageSize);
nextPage += pages;
- timing.pages += pages;
controller.enqueue(batch);
} catch (e) {
controller.error(e);
- } finally {
- timing.readMs += Date.now() - startedAt;
}
}
})
diff --git a/apps/standalone/src/services/capacitor_download.ts b/apps/standalone/src/services/capacitor_download.ts
index 7803831094..b31a6bbafb 100644
--- a/apps/standalone/src/services/capacitor_download.ts
+++ b/apps/standalone/src/services/capacitor_download.ts
@@ -109,11 +109,7 @@ export async function saveChunksToDevice(
target: SaveTarget
): Promise<StandaloneSaveResult> {
try {
- const started = performance.now();
- const outcome = await writeChunks(fileName, chunks, target);
- reportThroughput(fileName, outcome, performance.now() - started);
- const { uri } = outcome;
-
+ const uri = await writeChunks(fileName, chunks, target);
return { ...await shareFile(uri, fileName), location: displayPath(uri) };
} catch (e) {
return {
@@ -161,20 +157,12 @@ function getPlugin<T>(name: string): T {
return plugin as T;
}
-interface WriteOutcome {
- uri: string;
- bytes: number;
- encodeMs: number;
- waitMs: number;
- transport: "sink" | "plugin";
-}
-
/** Writes the chunks into `target` one at a time and returns the finished file's URI. */
async function writeChunks(
fileName: string,
chunks: AsyncIterable<Uint8Array>,
{ directory, folder, sweep }: SaveTarget
-): Promise<WriteOutcome> {
+): Promise<string> {
const filesystem = getPlugin<FilesystemPlugin>("Filesystem");
const path = `${folder}/${fileName}`;
@@ -192,15 +180,13 @@ async function writeChunks(
await filesystem.writeFile({ path, data: "", directory, recursive: true });
const { uri } = await filesystem.getUri({ path, directory });
- return { uri, transport: "sink", ...await writeThroughSink(sink, displayPath(uri), chunks) };
+ await writeThroughSink(sink, displayPath(uri), chunks);
+ return uri;
} finally {
releaseFileSink();
}
}
- let bytes = 0;
- let encodeMs = 0;
- let waitMs = 0;
let started = false;
// One call is kept in flight while the next chunk encodes, so the encode's cost hides behind
// the bridge's. Never two: appends are ordered only because each is issued after the previous
@@ -209,25 +195,20 @@ async function writeChunks(
const awaitPending = async () => {
if (pending) {
- const waitStart = performance.now();
await pending;
- waitMs += performance.now() - waitStart;
pending = null;
}
};
await withoutCallLogging(async () => {
for await (const chunk of rechunk(chunks, chunkBytes)) {
- const encodeStart = performance.now();
const data = toBase64(chunk);
- encodeMs += performance.now() - encodeStart;
await awaitPending();
pending = started
? filesystem.appendFile({ path, data, directory })
: filesystem.writeFile({ path, data, directory, recursive: true });
started = true;
- bytes += chunk.length;
}
await awaitPending();
});
@@ -237,7 +218,7 @@ async function writeChunks(
}
const { uri } = await filesystem.getUri({ path, directory });
- return { uri, bytes, encodeMs, waitMs, transport: "plugin" };
+ return uri;
}
/**
@@ -281,10 +262,7 @@ async function writeThroughSink(
sink: NativeFileSink,
filePath: string,
chunks: AsyncIterable<Uint8Array>
-): Promise<{ bytes: number; encodeMs: number; waitMs: number }> {
- let bytes = 0;
- let waitMs = 0;
-
+): Promise<void> {
// Replies arrive in send order, so the head of this queue is always the awaited step.
const waiting: ((reply: string) => void)[] = [];
const listener = (event: { data: unknown }) => waiting.shift()?.(String(event.data));
@@ -312,17 +290,12 @@ async function writeThroughSink(
try {
await send(JSON.stringify({ type: "open", path: filePath }), "opened");
for await (const chunk of rechunk(chunks, chunkBytes)) {
- const waitStart = performance.now();
await send(exactBuffer(chunk), "written");
- waitMs += performance.now() - waitStart;
- bytes += chunk.length;
}
await send(JSON.stringify({ type: "close" }), "closed");
} finally {
sink.removeEventListener("message", listener);
}
-
- return { bytes, encodeMs: 0, waitMs };
}
/** The chunk's bytes as exactly one ArrayBuffer, which is what the message channel carries. */
@@ -359,32 +332,6 @@ async function withoutCallLogging<T>(write: () => Promise<T>): Promise<T> {
}
}
-/**
- * What the write achieved, which is the only place the transport's cost per byte is visible.
- *
- * The split names the suspect: `base64` is time in {@link toBase64}, the wait is time blocked on
- * the transport. What is left of the total is producing the bytes (the worker, for a backup) plus
- * everything the pipelining managed to hide.
- */
-function reportThroughput(
- fileName: string,
- { bytes, encodeMs, waitMs, transport }: WriteOutcome,
- elapsedMs: number
-): void {
- const seconds = elapsedMs / 1000;
- const megabytes = bytes / 1_000_000;
- const rate = seconds > 0 ? (megabytes / seconds).toFixed(1) : "—";
- const split = transport === "sink"
- ? `via the binary sink — write wait ${(waitMs / 1000).toFixed(1)}s`
- : `via the plugin bridge — base64 ${(encodeMs / 1000).toFixed(1)}s, `
- + `bridge wait ${(waitMs / 1000).toFixed(1)}s`;
-
- console.log(
- `[Save] ${fileName}: ${megabytes.toFixed(1)} MB in ${seconds.toFixed(1)}s (${rate} MB/s) `
- + `${split}, ${Math.ceil(bytes / chunkBytes)} writes of ${chunkBytes / 1024} KiB`
- );
-}
-
/**
* The file's URI as somewhere a person can be told to look. `getUri` answers with a `file://` URL,
* percent-encoded, which is a path with two layers of machinery on top of it.