From 61e9c4e4b150c06db28954d19395b6c497ac5849 Mon Sep 17 00:00:00 2001 From: Denver Coneybeare Date: Wed, 17 Sep 2025 15:37:16 -0400 Subject: [PATCH 1/2] firestore: View.java: Improve query performance by avoiding excessive Comparator instance creation --- firebase-firestore/CHANGELOG.md | 2 ++ .../java/com/google/firebase/firestore/core/View.java | 10 +++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/firebase-firestore/CHANGELOG.md b/firebase-firestore/CHANGELOG.md index 375c5d3d447..6ee35ec4143 100644 --- a/firebase-firestore/CHANGELOG.md +++ b/firebase-firestore/CHANGELOG.md @@ -8,6 +8,8 @@ [#7376](//github.com/firebase/firebase-android-sdk/issues/7376) - [changed] Improve query performance via internal memoization of calculated document data. [#7370](//github.com/firebase/firebase-android-sdk/issues/7370) +- [changed] Improve query performance by avoiding excessive Comparator instance creation. + [#NNNN](//github.com/firebase/firebase-android-sdk/issues/NNNN) # 26.0.0 diff --git a/firebase-firestore/src/main/java/com/google/firebase/firestore/core/View.java b/firebase-firestore/src/main/java/com/google/firebase/firestore/core/View.java index b136e150182..4ea6d83ed41 100644 --- a/firebase-firestore/src/main/java/com/google/firebase/firestore/core/View.java +++ b/firebase-firestore/src/main/java/com/google/firebase/firestore/core/View.java @@ -29,6 +29,7 @@ import com.google.firebase.firestore.remote.TargetChange; import java.util.ArrayList; import java.util.Collections; +import java.util.Comparator; import java.util.List; import java.util.Map; @@ -156,6 +157,7 @@ public DocumentChanges computeDocChanges( ? oldDocumentSet.getFirstDocument() : null; + Comparator queryComparator = query.comparator(); for (Map.Entry entry : docChanges) { DocumentKey key = entry.getKey(); Document oldDoc = oldDocumentSet.getDocument(key); @@ -182,9 +184,9 @@ public DocumentChanges computeDocChanges( changeSet.addChange(DocumentViewChange.create(Type.MODIFIED, newDoc)); changeApplied = true; - if ((lastDocInLimit != null && query.comparator().compare(newDoc, lastDocInLimit) > 0) + if ((lastDocInLimit != null && queryComparator.compare(newDoc, lastDocInLimit) > 0) || (firstDocInLimit != null - && query.comparator().compare(newDoc, firstDocInLimit) < 0)) { + && queryComparator.compare(newDoc, firstDocInLimit) < 0)) { // This doc moved from inside the limit to outside the limit. That means there may be // some doc in the local cache that should be included instead. needsRefill = true; @@ -296,7 +298,9 @@ public ViewChange applyChanges( mutatedKeys = docChanges.mutatedKeys; // Sort changes based on type and query comparator. + List viewChanges = docChanges.changeSet.getChanges(); + Comparator queryComparator = query.comparator(); Collections.sort( viewChanges, (DocumentViewChange o1, DocumentViewChange o2) -> { @@ -304,7 +308,7 @@ public ViewChange applyChanges( if (typeComp != 0) { return typeComp; } - return query.comparator().compare(o1.getDocument(), o2.getDocument()); + return queryComparator.compare(o1.getDocument(), o2.getDocument()); }); applyTargetChange(targetChange); List limboDocumentChanges = From 9213804b578c7b6d571703d82b72bf97d4f45956 Mon Sep 17 00:00:00 2001 From: Denver Coneybeare Date: Wed, 17 Sep 2025 15:45:27 -0400 Subject: [PATCH 2/2] CHANGELOG.md: add pr number --- firebase-firestore/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firebase-firestore/CHANGELOG.md b/firebase-firestore/CHANGELOG.md index 6ee35ec4143..e9c1025da8f 100644 --- a/firebase-firestore/CHANGELOG.md +++ b/firebase-firestore/CHANGELOG.md @@ -9,7 +9,7 @@ - [changed] Improve query performance via internal memoization of calculated document data. [#7370](//github.com/firebase/firebase-android-sdk/issues/7370) - [changed] Improve query performance by avoiding excessive Comparator instance creation. - [#NNNN](//github.com/firebase/firebase-android-sdk/issues/NNNN) + [#7388](//github.com/firebase/firebase-android-sdk/pull/7388) # 26.0.0