Skip to content

Commit 3d4f7c3

Browse files
author
Hein
committed
Add context and background
1 parent f21e78c commit 3d4f7c3

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed

tutorials/code-changes/supabase-connector-performance.mdx

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,39 @@ title: "Improve Supabase Connector Performance"
33
description: "In this tutorial we will show you how to improve the performance of the Supabase Connector for the [React Native To-Do List example app](https://github.com/powersync-ja/powersync-js/tree/main/demos/react-native-supabase-todolist)."
44
---
55

6+
# Background
7+
8+
The demos in the [powersync-js](https://github.com/powersync-ja/powersync-js/tree/main/demos) monorepo provide a minimal working example that illustrate the use of PowerSync with different frameworks..
9+
The demos are therefore not necessarily optimized for performance and can therefore be improved.
10+
11+
This tutorial demonstrates how to improve the Supabase Connector's performance by implementing two batching strategies that reduce the number of database operations.
12+
13+
# Batching Strategies
14+
15+
The two batching strategies that will be implemented are:
16+
17+
1. Sequential Merge Strategy, and
18+
2. Pre-sorted Batch Strategy
19+
620
<AccordionGroup>
721
<Accordion title="Sequential Merge Strategy">
22+
Overview:
23+
- Merge adjacent `PUT` and `DELETE` operations for the same table
24+
- Limit the number of operations that are merged into a single API request to Supabase
825
<Note>
926
Shoutout to @christoffer_configura for the original implementation of this optimization.
1027
</Note>
11-
```typescript {7-8, 11, 13-15, 17, 19-20, 24-36, 39, 43-56, 59-60, 75}
28+
```typescript {6-12, 15, 17-19, 21, 23-24, 28-40, 43, 47-60, 63-64, 79}
1229
async uploadData(database: AbstractPowerSyncDatabase): Promise<void> {
1330
const transaction = await database.getNextCrudTransaction();
1431
if (!transaction) {
1532
return;
1633
}
17-
34+
/**
35+
* Maximum number of PUT or DELETE operations that are merged into a single API request to Supabase.
36+
* Larger numbers can speed up the sync process considerably, but watch out for possible payload size limitations.
37+
* A value of 1 or below disables merging.
38+
*/
1839
const MERGE_BATCH_LIMIT = 100;
1940
let batchedOps: CrudEntry[] = [];
2041

@@ -36,7 +57,7 @@ description: "In this tutorial we will show you how to improve the performance o
3657
while (
3758
i + 1 < cruds.length &&
3859
cruds[i + 1].op === op.op &&
39-
cruds[i + 1].op === op.op &&
60+
cruds[i + 1].table === op.table &&
4061
batched < MERGE_BATCH_LIMIT
4162
) {
4263
i++;
@@ -56,7 +77,7 @@ description: "In this tutorial we will show you how to improve the performance o
5677
while (
5778
i + 1 < cruds.length &&
5879
cruds[i + 1].op === op.op &&
59-
cruds[i + 1].op === op.table &&
80+
cruds[i + 1].table === op.table &&
6081
batched < MERGE_BATCH_LIMIT
6182
) {
6283
i++;
@@ -94,7 +115,15 @@ description: "In this tutorial we will show you how to improve the performance o
94115
}
95116
```
96117
</Accordion>
97-
<Accordion title="Pre-sorted Batch Strategy ">
118+
<Accordion title="Pre-sorted Batch Strategy">
119+
Overview:
120+
- Create three collections to group operations by type:
121+
- `putOps`: For `PUT` operations, organized by table name
122+
- `deleteOps`: For `DELETE` operations, organized by table name
123+
- `patchOps`: For `PATCH` operations (partial updates)
124+
125+
- Loop through all operations, sort them into the three collections, and then process all operations in batches.
126+
98127
```typescript {8-11, 17-20, 23, 26-29, 32-53, 56, 72}
99128
async uploadData(database: AbstractPowerSyncDatabase): Promise<void> {
100129
const transaction = await database.getNextCrudTransaction();

0 commit comments

Comments
 (0)