Skip to content

Commit 482ac0e

Browse files
authored
firestore: Improve query performance by using an unsorted map instead of a sorted map (#7389)
1 parent 77aa34e commit 482ac0e

File tree

3 files changed

+15
-19
lines changed

3 files changed

+15
-19
lines changed

firebase-firestore/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
[#7370](//github.com/firebase/firebase-android-sdk/issues/7370)
1313
- [changed] Improve query performance by avoiding excessive Comparator instance creation.
1414
[#7388](//github.com/firebase/firebase-android-sdk/pull/7388)
15+
- [changed] Improve query performance by using an unsorted HashMap instead of a sorted TreeMap.
16+
[#7389](//github.com/firebase/firebase-android-sdk/pull/7389)
1517

1618
# 26.0.0
1719

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,15 @@
1919
import com.google.firebase.firestore.core.DocumentViewChange.Type;
2020
import com.google.firebase.firestore.model.DocumentKey;
2121
import java.util.ArrayList;
22+
import java.util.HashMap;
2223
import java.util.List;
23-
import java.util.TreeMap;
2424

2525
/** A set of changes to documents with respect to a view. This set is mutable. */
2626
public class DocumentViewChangeSet {
27-
// This map is sorted to make the unit tests simpler.
28-
private final TreeMap<DocumentKey, DocumentViewChange> changes;
27+
private final HashMap<DocumentKey, DocumentViewChange> changes;
2928

3029
public DocumentViewChangeSet() {
31-
changes = new TreeMap<>();
30+
changes = new HashMap<>();
3231
}
3332

3433
public void addChange(DocumentViewChange change) {

firebase-firestore/src/test/java/com/google/firebase/firestore/core/DocumentViewChangeSetTest.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package com.google.firebase.firestore.core;
1616

17+
import static com.google.common.truth.Truth.assertThat;
1718
import static com.google.firebase.firestore.testutil.TestUtil.EMPTY_MAP;
1819
import static com.google.firebase.firestore.testutil.TestUtil.doc;
1920
import static org.junit.Assert.assertEquals;
@@ -71,20 +72,14 @@ public void testTrack() {
7172

7273
List<DocumentViewChange> changes = set.getChanges();
7374

74-
assertEquals(7, changes.size());
75-
assertEquals(added, changes.get(0).getDocument());
76-
assertEquals(Type.ADDED, changes.get(0).getType());
77-
assertEquals(removed, changes.get(1).getDocument());
78-
assertEquals(Type.REMOVED, changes.get(1).getType());
79-
assertEquals(modified, changes.get(2).getDocument());
80-
assertEquals(Type.MODIFIED, changes.get(2).getType());
81-
assertEquals(addedThenModified, changes.get(3).getDocument());
82-
assertEquals(Type.ADDED, changes.get(3).getType());
83-
assertEquals(removedThenAdded, changes.get(4).getDocument());
84-
assertEquals(Type.MODIFIED, changes.get(4).getType());
85-
assertEquals(modifiedThenRemoved, changes.get(5).getDocument());
86-
assertEquals(Type.REMOVED, changes.get(5).getType());
87-
assertEquals(modifiedThenModified, changes.get(6).getDocument());
88-
assertEquals(Type.MODIFIED, changes.get(6).getType());
75+
assertThat(changes)
76+
.containsExactly(
77+
DocumentViewChange.create(Type.ADDED, added),
78+
DocumentViewChange.create(Type.REMOVED, removed),
79+
DocumentViewChange.create(Type.MODIFIED, modified),
80+
DocumentViewChange.create(Type.ADDED, addedThenModified),
81+
DocumentViewChange.create(Type.MODIFIED, removedThenAdded),
82+
DocumentViewChange.create(Type.REMOVED, modifiedThenRemoved),
83+
DocumentViewChange.create(Type.MODIFIED, modifiedThenModified));
8984
}
9085
}

0 commit comments

Comments
 (0)