Skip to content

Commit d7ace80

Browse files
authored
Firestore: Choose DEFAULT_RELATIVE_INDEX_READ_COST_PER_DOCUMENT value based on the browser, rather than hardcoding 8 (#7929)
1 parent 6ea51fb commit d7ace80

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

.changeset/witty-comics-rule.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'firebase': patch
3+
'@firebase/firestore': patch
4+
---
5+
6+
Tweak the automatic index creation parameters to use more optimal values for the platform/browser detected at runtime.

packages/firestore/src/local/query_engine.ts

+16-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
* limitations under the License.
1616
*/
1717

18+
import { getUA, isSafari } from '@firebase/util';
19+
1820
import {
1921
LimitType,
2022
newQueryComparator,
@@ -47,6 +49,7 @@ import { LocalDocumentsView } from './local_documents_view';
4749
import { PersistencePromise } from './persistence_promise';
4850
import { PersistenceTransaction } from './persistence_transaction';
4951
import { QueryContext } from './query_context';
52+
import { SimpleDb } from './simple_db';
5053

5154
const DEFAULT_INDEX_AUTO_CREATION_MIN_COLLECTION_SIZE = 100;
5255

@@ -55,10 +58,19 @@ const DEFAULT_INDEX_AUTO_CREATION_MIN_COLLECTION_SIZE = 100;
5558
* (([index, docKey] + [docKey, docContent]) per document in the result set)
5659
* / ([docKey, docContent] per documents in full collection scan) coming from
5760
* experiment [enter PR experiment URL here].
58-
* TODO(b/299284287) Choose a value appropriate for the browser/OS combination,
59-
* as determined by more data points from running the experiment.
6061
*/
61-
const DEFAULT_RELATIVE_INDEX_READ_COST_PER_DOCUMENT = 8;
62+
function getDefaultRelativeIndexReadCostPerDocument(): number {
63+
// These values were derived from an experiment where several members of the
64+
// Firestore SDK team ran a performance test in various environments.
65+
// Googlers can see b/299284287 for details.
66+
if (isSafari()) {
67+
return 8;
68+
} else if (SimpleDb.getAndroidVersion(getUA()) > 0) {
69+
return 6;
70+
} else {
71+
return 4;
72+
}
73+
}
6274

6375
/**
6476
* The Firestore query engine.
@@ -113,7 +125,7 @@ export class QueryEngine {
113125
DEFAULT_INDEX_AUTO_CREATION_MIN_COLLECTION_SIZE;
114126

115127
relativeIndexReadCostPerDocument =
116-
DEFAULT_RELATIVE_INDEX_READ_COST_PER_DOCUMENT;
128+
getDefaultRelativeIndexReadCostPerDocument();
117129

118130
/** Sets the document view to query against. */
119131
initialize(

0 commit comments

Comments
 (0)