Skip to content

Build your personal knowledge base with Trilium Notes

perf(sync): estimate response size instead of double-serializing

Elian Dorana9481364af46
diff --git a/packages/trilium-core/src/services/sync.spec.ts b/packages/trilium-core/src/services/sync.spec.ts
index f48b8e532a..7aba8f0578 100644
--- a/packages/trilium-core/src/services/sync.spec.ts
+++ b/packages/trilium-core/src/services/sync.spec.ts
@@ -11,7 +11,7 @@ import options from "./options.js";
import { type ExecOpts, initRequest, type RequestProvider } from "./request.js";
import { getSql } from "./sql/index.js";
import setupService from "./setup.js";
-import syncService from "./sync.js";
+import syncService, { estimateEntityChangeRecordSize } from "./sync.js";
import syncOptions from "./sync_options.js";
import dateUtils from "./utils/date.js";
@@ -284,6 +284,27 @@ describe("sync service", () => {
});
});
+describe("estimateEntityChangeRecordSize", () => {
+ const ec = {} as EntityChange;
+
+ it("uses (base64) string content length plus a fixed metadata allowance", () => {
+ expect(estimateEntityChangeRecordSize({ entityChange: ec, entity: { content: "A".repeat(1000) } as never })).toBe(1300);
+ });
+
+ it("handles binary (Uint8Array) content", () => {
+ expect(estimateEntityChangeRecordSize({ entityChange: ec, entity: { content: new Uint8Array(500) } as never })).toBe(800);
+ });
+
+ it("returns just the allowance for records without content (metadata / erased)", () => {
+ expect(estimateEntityChangeRecordSize({ entityChange: ec })).toBe(300);
+ expect(estimateEntityChangeRecordSize({ entityChange: ec, entity: {} as never })).toBe(300);
+ });
+
+ it("a single large blob record exceeds the ~1 MB cap threshold", () => {
+ expect(estimateEntityChangeRecordSize({ entityChange: ec, entity: { content: "x".repeat(1_000_001) } as never })).toBeGreaterThan(1_000_000);
+ });
+});
+
function pulledOptionChange(): { entityChange: EntityChange; entity: undefined } {
// An options change with no entity row -> applied as a no-op by updateEntities.
return {
diff --git a/packages/trilium-core/src/services/sync.ts b/packages/trilium-core/src/services/sync.ts
index b5f9a3283f..dea7277882 100644
--- a/packages/trilium-core/src/services/sync.ts
+++ b/packages/trilium-core/src/services/sync.ts
@@ -469,7 +469,7 @@ function getEntityChangeRecords(entityChanges: EntityChange[]) {
records.push(record);
- length += JSON.stringify(record).length;
+ length += estimateEntityChangeRecordSize(record);
if (length > 1_000_000) {
// each sync request/response should have at most ~1 MB.
@@ -480,6 +480,20 @@ function getEntityChangeRecords(entityChanges: EntityChange[]) {
return records;
}
+/**
+ * Rough serialized byte size of an entity-change record, used only to bound a sync response to
+ * ~1 MB. Avoids a full `JSON.stringify(record)` here — the record's (base64-encoded) blob content
+ * would otherwise be serialized just to be measured, then serialized again when the response is
+ * sent. Blob content dominates the payload; a fixed allowance covers the entityChange plus the
+ * record's other (small) entity fields, which is precise enough for a size threshold.
+ */
+export function estimateEntityChangeRecordSize(record: EntityChangeRecord): number {
+ const content = record.entity?.content;
+ const contentLength = typeof content === "string" ? content.length : content?.length ?? 0;
+
+ return contentLength + 300;
+}
+
function getLastSyncedPull() {
return parseInt(optionService.getOption("lastSyncedPull"));
}