Skip to content

Commit 939be87

Browse files
authored
Add unionWith to SortedSet. (#1241)
* Add unionWith to sorted set to mirror Web SDK.
1 parent dbf2967 commit 939be87

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

firebase-database-collection/src/main/java/com/google/firebase/database/collection/ImmutableSortedSet.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,22 @@ public ImmutableSortedSet<T> insert(T entry) {
8787
return new ImmutableSortedSet<T>(map.insert(entry, null));
8888
}
8989

90+
public ImmutableSortedSet<T> unionWith(ImmutableSortedSet<T> other) {
91+
ImmutableSortedSet<T> result = this;
92+
93+
// Make sure `result` always refers to the larger one of the two sets.
94+
if (result.size() < other.size()) {
95+
result = other;
96+
other = this;
97+
}
98+
99+
for (T elem : other) {
100+
result = result.insert(elem);
101+
}
102+
103+
return result;
104+
}
105+
90106
public T getMinEntry() {
91107
return this.map.getMinKey();
92108
}

firebase-firestore/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# Unreleased
2+
- [fixed] Fixed a performance regression introduced by the addition of
3+
`Query.limitToLast(n: long)` in Firestore 23.3.1.
24
- [changed] Changed the in-memory representation of Firestore documents to
35
reduce memory allocations and improve performance. Calls to
46
`DocumentSnapshot.getData()` and `DocumentSnapshot.toObject()` will see

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -354,17 +354,17 @@ public ImmutableSortedSet<DocumentKey> getRemoteKeysForTarget(int targetId) {
354354
if (limboResolution != null && limboResolution.receivedDocument) {
355355
return DocumentKey.emptyKeySet().insert(limboResolution.key);
356356
} else {
357-
List<DocumentKey> remoteKeys = Lists.newArrayList();
357+
ImmutableSortedSet<DocumentKey> remoteKeys = DocumentKey.emptyKeySet();
358358
if (queriesByTarget.containsKey(targetId)) {
359359
for (Query query : queriesByTarget.get(targetId)) {
360360
if (queryViewsByQuery.containsKey(query)) {
361-
remoteKeys.addAll(
362-
Lists.newArrayList(queryViewsByQuery.get(query).getView().getSyncedDocuments()));
361+
remoteKeys =
362+
remoteKeys.unionWith(queryViewsByQuery.get(query).getView().getSyncedDocuments());
363363
}
364364
}
365365
}
366366

367-
return new ImmutableSortedSet(remoteKeys, DocumentKey.comparator());
367+
return remoteKeys;
368368
}
369369
}
370370

0 commit comments

Comments
 (0)