firebase-js-sdk icon indicating copy to clipboard operation
firebase-js-sdk copied to clipboard

Firestore: Make client-side indexing code tree-shakeable

Open dconeybe opened this issue 1 year ago • 3 comments

Firestore supports a feature called "client-side indexing" (e.g. enablePersistentCacheIndexAutoCreation()) which requires a significant amount of code to support it. This feature, however, is infrequently used and causes all users of Firestore who use IndexedDb persistence to pay the code size price for this feature, even if they do not use it. According to the "Size Report" comment in this PR, the price can be as much as 23 kB.

This PR refactors Firestore so that the client-side indexing code can be tree-shaken away if not used. Namely, only the following functions will force inclusion of the client-side indexing code (otherwise it can be tree-shaken away):

  • enablePersistentCacheIndexAutoCreation()
  • setIndexConfiguration() (which is deprecated and slated for deletion)

Notably, using the functions disablePersistentCacheIndexAutoCreation() and/or deleteAllPersistentCacheIndexes() will not pull in the entire client-side indexing code but, rather, only the code required to support these specific operations. This allows customers to "opt out" of client-side indexing that they had previously used without paying the code size cost of the entire client-side indexing code.

One side effect of this change is that the indexes will neither be used nor updated until the first call to either enablePersistentCacheIndexAutoCreation() or setIndexConfiguration(). Before these functions are called, or if they are never called, the indexes will neither be used nor updated. Therefore, applications are encouraged to call one of these functions early in their application, such as immediately following creating the Firestore instance, if they are used at all.

Another side effect of this change is that when using multi-tab persistence, if the tab that happens to have been elected as the "primary" tab has not called one of these functions to enable the client-side indexing functionality, then none of the tabs will get the indexes. In practice, this shouldn't be a problem because all tabs should generally be running the same version of the same app in each tab, thus all either using or not using client-side indexing.

Work In Progress

This PR is a work-in-progress. As of Jan 22, 2024, work on this PR has been de-prioritized but I'm leaving it here so it can be picked up in the future. Googlers see b/293449522 for details.

dconeybe avatar Dec 22 '23 19:12 dconeybe

🦋 Changeset detected

Latest commit: bef8149d791a573562f66f653a5bdb398904ba4a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@firebase/firestore Patch
firebase Patch
@firebase/firestore-compat Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

changeset-bot[bot] avatar Dec 22 '23 19:12 changeset-bot[bot]

Size Report 1

Affected Products

  • @firebase/firestore

    TypeBase (ab883d0)Merge (b35dbad)Diff
    browser378 kB380 kB+2.13 kB (+0.6%)
    esm5363 kB365 kB+1.55 kB (+0.4%)
    main582 kB585 kB+3.22 kB (+0.6%)
    module378 kB380 kB+2.13 kB (+0.6%)
    react-native378 kB380 kB+2.13 kB (+0.6%)
  • bundle

    TypeBase (ab883d0)Merge (b35dbad)Diff
    firestore (CSI Auto Indexing Disable and Delete)268 kB245 kB-22.7 kB (-8.5%)
    firestore (CSI Auto Indexing Enable)268 kB269 kB+704 B (+0.3%)
    firestore (Persistence)303 kB280 kB-22.9 kB (-7.6%)
    firestore (Query Cursors)240 kB238 kB-1.94 kB (-0.8%)
    firestore (Query)238 kB236 kB-1.94 kB (-0.8%)
    firestore (Read data once)226 kB224 kB-1.94 kB (-0.9%)
    firestore (Read Write w Persistence)322 kB299 kB-22.9 kB (-7.1%)
    firestore (Realtime updates)228 kB226 kB-1.94 kB (-0.9%)
    firestore (Transaction)205 kB203 kB-1.94 kB (-0.9%)
    firestore (Write data)205 kB203 kB-1.94 kB (-0.9%)
  • firebase

    TypeBase (ab883d0)Merge (b35dbad)Diff
    firebase-compat.js786 kB763 kB-22.3 kB (-2.8%)
    firebase-firestore-compat.js341 kB319 kB-22.3 kB (-6.5%)
    firebase-firestore.js437 kB439 kB+2.07 kB (+0.5%)

Test Logs

google-oss-bot avatar Dec 22 '23 19:12 google-oss-bot

Size Analysis Report 1

This report is too large (504,164 characters) to be displayed here in a GitHub comment. Please use the below link to see the full report on Google Cloud Storage.

Test Logs

google-oss-bot avatar Dec 22 '23 20:12 google-oss-bot