Skip to content

Commit 77aa34e

Browse files
authored
firestore: Improve query performance by avoiding excessive Comparator instance creation (#7388)
1 parent 3bc65ea commit 77aa34e

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

firebase-firestore/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
[#7376](//github.com/firebase/firebase-android-sdk/issues/7376)
1111
- [changed] Improve query performance via internal memoization of calculated document data.
1212
[#7370](//github.com/firebase/firebase-android-sdk/issues/7370)
13+
- [changed] Improve query performance by avoiding excessive Comparator instance creation.
14+
[#7388](//github.com/firebase/firebase-android-sdk/pull/7388)
1315

1416
# 26.0.0
1517

firebase-firestore/src/main/java/com/google/firebase/firestore/core/View.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.google.firebase.firestore.remote.TargetChange;
3030
import java.util.ArrayList;
3131
import java.util.Collections;
32+
import java.util.Comparator;
3233
import java.util.List;
3334
import java.util.Map;
3435

@@ -156,6 +157,7 @@ public DocumentChanges computeDocChanges(
156157
? oldDocumentSet.getFirstDocument()
157158
: null;
158159

160+
Comparator<Document> queryComparator = query.comparator();
159161
for (Map.Entry<DocumentKey, Document> entry : docChanges) {
160162
DocumentKey key = entry.getKey();
161163
Document oldDoc = oldDocumentSet.getDocument(key);
@@ -182,9 +184,9 @@ public DocumentChanges computeDocChanges(
182184
changeSet.addChange(DocumentViewChange.create(Type.MODIFIED, newDoc));
183185
changeApplied = true;
184186

185-
if ((lastDocInLimit != null && query.comparator().compare(newDoc, lastDocInLimit) > 0)
187+
if ((lastDocInLimit != null && queryComparator.compare(newDoc, lastDocInLimit) > 0)
186188
|| (firstDocInLimit != null
187-
&& query.comparator().compare(newDoc, firstDocInLimit) < 0)) {
189+
&& queryComparator.compare(newDoc, firstDocInLimit) < 0)) {
188190
// This doc moved from inside the limit to outside the limit. That means there may be
189191
// some doc in the local cache that should be included instead.
190192
needsRefill = true;
@@ -296,15 +298,17 @@ public ViewChange applyChanges(
296298
mutatedKeys = docChanges.mutatedKeys;
297299

298300
// Sort changes based on type and query comparator.
301+
299302
List<DocumentViewChange> viewChanges = docChanges.changeSet.getChanges();
303+
Comparator<Document> queryComparator = query.comparator();
300304
Collections.sort(
301305
viewChanges,
302306
(DocumentViewChange o1, DocumentViewChange o2) -> {
303307
int typeComp = Integer.compare(View.changeTypeOrder(o1), View.changeTypeOrder(o2));
304308
if (typeComp != 0) {
305309
return typeComp;
306310
}
307-
return query.comparator().compare(o1.getDocument(), o2.getDocument());
311+
return queryComparator.compare(o1.getDocument(), o2.getDocument());
308312
});
309313
applyTargetChange(targetChange);
310314
List<LimboDocumentChange> limboDocumentChanges =

0 commit comments

Comments
 (0)