Skip to content

Commit

Permalink
IMPROVE remote performance
Browse files Browse the repository at this point in the history
  • Loading branch information
pubkey committed Apr 10, 2023
1 parent 63c1ab2 commit 1155ccd
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 25 deletions.
45 changes: 33 additions & 12 deletions orga/performance-trackings.md
Original file line number Diff line number Diff line change
Expand Up @@ -1252,18 +1252,22 @@ performanceResult: 12.78
AFTERR4:
performanceResult: 12.87

### insert documents
AFTER5:
15.15
18.47
13.31

BEFORE:
'performanceResult: 34.52'
LOG LOG: 'performanceResult: 34.1'
AFTER6: (Send JSON-string via postMessage)

12.47
12.37

AFTER:
'performanceResult: 28.6
AFTER7:



### insert documents

FIXED TEST!!

BEFORE:
Expand All @@ -1274,12 +1278,11 @@ performanceResult: 83.39






AFTER fixing merging sorted array

AFTER2:
AFTER 3:
40.8
44.3
37.9
38.7


### Query
Expand Down Expand Up @@ -1364,3 +1367,21 @@ AFTER(use inner monad)
64.17834001779556
72.26790100336075
69.85145297646523



## categorizeBulkWriteRows()

BEFORE:
31.00946400000248
35
43
35


AFTER1: (faster event keys)
29.86
31.29
32
31

35 changes: 23 additions & 12 deletions src/plugins/storage-remote/rx-storage-remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,27 @@ export class RxStorageRemote implements RxStorage<RxStorageRemoteInternals, any>
}
}

/**
* Because postMessage() can be very slow on complex objects,
* and some RxStorage implementations do need a JSON-string internally
* anyway, it is allowed to transfer a string instead of an object
* which must then be JSON.parse()-ed before RxDB can use it.
* @link https://surma.dev/things/is-postmessage-slow/
*/
function getMessageReturn(
msg: MessageFromRemote
) {
if (msg.method === 'getAttachmentData') {
return msg.return;
} else {
if (typeof msg.return === 'string') {
return JSON.parse(msg.return);
} else {
return msg.return;
}
}
}

export class RxStorageInstanceRemote<RxDocType> implements RxStorageInstance<RxDocType, RxStorageRemoteInternals, any, any> {
private changes$: Subject<EventBulk<RxStorageChangeEvent<RxDocumentData<RxDocType>>, any>> = new Subject();
private conflicts$: Subject<RxConflictResultionTask<RxDocType>> = new Subject();
Expand All @@ -168,17 +189,7 @@ export class RxStorageInstanceRemote<RxDocType> implements RxStorageInstance<RxD
this.subs.push(
this.messages$.subscribe(msg => {
if (msg.method === 'changeStream') {
/**
* Because postMessage() can be very slow on complex objects,
* and some RxStorage implementations do need a JSON-string internally
* anyway, it is allowed to transfer a string instead of an object
* which must then be JSON.parse()-ed before RxDB can use it.
* @link https://surma.dev/things/is-postmessage-slow/
*/
if (typeof msg.return === 'string') {
msg.return = JSON.parse(msg.return);
}
this.changes$.next(msg.return);
this.changes$.next(getMessageReturn(msg));
}
if (msg.method === 'conflictResultionTasks') {
this.conflicts$.next(msg.return);
Expand Down Expand Up @@ -212,7 +223,7 @@ export class RxStorageInstanceRemote<RxDocType> implements RxStorageInstance<RxD
error: response.error
}, null, 4));
} else {
return response.return;
return getMessageReturn(response);
}
}
bulkWrite(
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/storage-remote/storage-remote-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export type MessageFromRemote = {
answerTo: string; // id of the request
method: keyof RxStorageInstance<any, any, any> | 'create' | 'custom';
error?: PlainJsonError;
return?: any;
return?: any | string;
};

export type MessageToRemote = {
Expand Down

0 comments on commit 1155ccd

Please sign in to comment.