Skip to content

Commit 1b98cc5

Browse files
committed
avoid asynchronous scheduling if there is only one row in the cursor
1 parent a9dc976 commit 1b98cc5

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

firebase-firestore/src/main/java/com/google/firebase/firestore/local/SQLiteDocumentOverlayCache.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,21 @@ private void processOverlaysInBackground(
202202
byte[] rawMutation = row.getBlob(0);
203203
int largestBatchId = row.getInt(1);
204204

205-
backgroundQueue.submit(
205+
Runnable runnable =
206206
() -> {
207207
Overlay overlay = decodeOverlay(rawMutation, largestBatchId);
208208
synchronized (results) {
209209
results.put(overlay.getKey(), overlay);
210210
}
211-
});
211+
};
212+
213+
// If the cursor has exactly one row then just process that row synchronously to avoid the
214+
// unnecessary overhead of scheduling its processing to run asynchronously.
215+
if (row.isFirst() && row.isLast()) {
216+
runnable.run();
217+
} else {
218+
backgroundQueue.submit(runnable);
219+
}
212220
}
213221

214222
private Overlay decodeOverlay(byte[] rawMutation, int largestBatchId) {

firebase-firestore/src/main/java/com/google/firebase/firestore/local/SQLiteRemoteDocumentCache.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ private void processRowInBackground(
306306
boolean documentTypeIsNull = row.isNull(3);
307307
String path = row.getString(4);
308308

309-
backgroundQueue.submit(
309+
Runnable runnable =
310310
() -> {
311311
MutableDocument document =
312312
decodeMaybeDocument(rawDocument, readTimeSeconds, readTimeNanos);
@@ -318,7 +318,15 @@ private void processRowInBackground(
318318
results.put(document.getKey(), document);
319319
}
320320
}
321-
});
321+
};
322+
323+
// If the cursor has exactly one row then just process that row synchronously to avoid the
324+
// unnecessary overhead of scheduling its processing to run asynchronously.
325+
if (row.isFirst() && row.isLast()) {
326+
runnable.run();
327+
} else {
328+
backgroundQueue.submit(runnable);
329+
}
322330
}
323331

324332
@Override

0 commit comments

Comments
 (0)