Skip to content

Commit 297b5a9

Browse files
committed
WIP: Move processing of rows from a background thread to the main thread. Reducing the overhead of queueing each row for processing on a background thread.
1 parent 3034608 commit 297b5a9

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

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

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,15 @@ public Map<DocumentKey, MutableDocument> getAll(Iterable<DocumentKey> documentKe
170170
bindVars,
171171
") ORDER BY path");
172172

173-
BackgroundQueue backgroundQueue = new BackgroundQueue();
173+
// BackgroundQueue backgroundQueue = new BackgroundQueue();
174174
while (longQuery.hasMoreSubqueries()) {
175175
longQuery
176176
.performNextSubquery()
177-
.forEach(row -> processRowInBackground(backgroundQueue, results, row, /*filter*/ null));
177+
// .forEach(row -> processRowInBackground(backgroundQueue, results, row, /*filter*/
178+
// null));
179+
.forEach(row -> processRow(results, row, /*filter*/ null));
178180
}
179-
backgroundQueue.drain();
181+
// backgroundQueue.drain();
180182

181183
// Backfill any rows with null "document_type" discovered by processRowInBackground().
182184
documentTypeBackfiller.backfill(db);
@@ -266,18 +268,19 @@ private Map<DocumentKey, MutableDocument> getAll(
266268
}
267269
bindVars[i] = count;
268270

269-
BackgroundQueue backgroundQueue = new BackgroundQueue();
271+
// BackgroundQueue backgroundQueue = new BackgroundQueue();
270272
Map<DocumentKey, MutableDocument> results = new HashMap<>();
271273
db.query(sql.toString())
272274
.binding(bindVars)
273275
.forEach(
274276
row -> {
275-
processRowInBackground(backgroundQueue, results, row, filter);
277+
// processRowInBackground(backgroundQueue, results, row, filter);
278+
processRow(results, row, filter);
276279
if (context != null) {
277280
context.incrementDocumentReadCount();
278281
}
279282
});
280-
backgroundQueue.drain();
283+
// backgroundQueue.drain();
281284

282285
// Backfill any null "document_type" columns discovered by processRowInBackground().
283286
documentTypeBackfiller.backfill(db);
@@ -326,6 +329,27 @@ private void processRowInBackground(
326329
});
327330
}
328331

332+
private void processRow(
333+
Map<DocumentKey, MutableDocument> results,
334+
Cursor row,
335+
@Nullable Function<MutableDocument, Boolean> filter) {
336+
byte[] rawDocument = row.getBlob(0);
337+
int readTimeSeconds = row.getInt(1);
338+
int readTimeNanos = row.getInt(2);
339+
boolean documentTypeIsNull = row.isNull(3);
340+
String path = row.getString(4);
341+
342+
MutableDocument document = decodeMaybeDocument(rawDocument, readTimeSeconds, readTimeNanos);
343+
if (documentTypeIsNull) {
344+
documentTypeBackfiller.enqueue(path, readTimeSeconds, readTimeNanos, document);
345+
}
346+
if (filter == null || filter.apply(document)) {
347+
synchronized (results) {
348+
results.put(document.getKey(), document);
349+
}
350+
}
351+
}
352+
329353
@Override
330354
public Map<DocumentKey, MutableDocument> getDocumentsMatchingQuery(
331355
Query query, IndexOffset offset, @Nonnull Set<DocumentKey> mutatedKeys) {

0 commit comments

Comments
 (0)