这是indexloc提供的服务,不要输入任何密码
Skip to content

Firestore: use Integer.compare() and Long.compare() instead Firestore bespoke implementations #7109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

dconeybe
Copy link
Contributor

@dconeybe dconeybe commented Jul 7, 2025

This PR replaces the bespoke Util.compareIntegers() and Util.compareLongs() methods with standard Java Integer.compare(), Long.compare() and Character.compare() methods. It also updates the tests to validate comparisons based on sign rather than exact values, as these tests were previously brittle, erroneously failing due to Character.compare() returning values other than 1, 0, and -1.

The Util.compareIntegers() and Util.compareLongs() methods were originally added because they required minSdkVersion of 19 or greater which, at the time, was not the case. However, minSdkVersion has been 21 for some time now (and is increasing to 23 in the foreseeable future) so there is no longer any need for the bespoke implementations.

Highlights

  • Replaced all calls to Util.compareIntegers() and Util.compareLongs() with Integer.compare(), Long.compare(), or Character.compare(), as appropriate.
  • Removed redundant compareIntegers and compareLongs methods from Util and NumberComparisonHelper.
  • Updated UtilTest to assert via Integer.signum() instead of testing exact equality with 1, 0, or -1.

… bespoke `Util.compareIntegers()` method.

The `Util.compareIntegers()` method was originally added because it
required minSdkVersion of 19 or greater which, at the time, was not the
case. However, minSdkVersion has been 21 for some time now (and is
increasing to 23 in the foreseeable future) so there is no longer any
need for the bespoke implementation.

This comment was marked as resolved.

This comment was marked as off-topic.

gemini-code-assist[bot]

This comment was marked as outdated.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request effectively cleans up the codebase by replacing the custom Util.compareIntegers() with the standard Integer.compare(), leveraging the updated minSdkVersion. The changes are well-executed, and further improvements include updating the CHANGELOG.md placeholder and using Character.compare() for char comparisons in Util.java for better type safety.

@dconeybe dconeybe changed the title Firestore: use Integer.compare() instead of Util.compareIntegers() Firestore: use Integer.compare() instead of Util.compareIntegers() Jul 7, 2025
Copy link
Contributor

github-actions bot commented Jul 7, 2025

Test Results

  186 files  ±0    186 suites  ±0   4m 24s ⏱️ -14s
1 235 tests ±0  1 219 ✅ ±0  16 💤 ±0  0 ❌ ±0 
2 494 runs  ±0  2 462 ✅ ±0  32 💤 ±0  0 ❌ ±0 

Results for commit deeda5f. ± Comparison against base commit 84c4d81.

♻️ This comment has been updated with latest results.

@google-oss-bot

This comment was marked as off-topic.

@dconeybe dconeybe changed the title Firestore: use Integer.compare() instead of Util.compareIntegers() Firestore: use Integer.compare() and Long.compare() instead Firestore bespoke implementations Jul 7, 2025
@google-oss-bot

This comment was marked as off-topic.

@dconeybe
Copy link
Contributor Author

dconeybe commented Jul 7, 2025

/gemini review

/gemini summary

@dconeybe dconeybe requested a review from Copilot July 7, 2025 17:33

This comment was marked as outdated.

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR modernizes integer and long comparisons in the Firestore Android SDK by replacing bespoke Util.compareIntegers() and Util.compareLongs() with standard Java Integer.compare() and Long.compare() methods, updates related imports, and cleans up the now-redundant utility methods. It also amends the UTF-8 string comparison tests to validate comparisons based on sign rather than exact values.

  • Replaced all calls to Util.compareIntegers()/compareLongs() with Integer.compare(), Long.compare(), or Character.compare().
  • Removed redundant compareIntegers and compareLongs methods from Util and NumberComparisonHelper.
  • Updated UtilTest to assert via Integer.signum() and introduced a signName helper.
  • Updated CHANGELOG.md to document the change.

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated no comments.

Show a summary per file
File Description
firebase-firestore/src/main/java/com/google/firebase/firestore/util/Util.java Removed custom compare methods and replaced usages with standard APIs.
firebase-firestore/src/main/java/com/google/cloud/datastore/core/number/NumberComparisonHelper.java Removed compareLongs, replaced calls with Long.compare().
firebase-firestore/src/main/java/com/google/firebase/firestore/model/*.java Updated multiple model comparators to use Integer.compare()/Long.compare().
firebase-firestore/src/main/java/com/google/firebase/firestore/local/*.java Updated local sorting comparators and removed unused imports.
firebase-firestore/src/test/java/com/google/firebase/firestore/util/UtilTest.java Changed test assertions to compare signum and added signName.
firebase-firestore/CHANGELOG.md Added entry for using Integer.compare() and Long.compare().
Comments suppressed due to low confidence (1)

firebase-firestore/src/test/java/com/google/firebase/firestore/util/UtilTest.java:125

  • The error message for the expected value prints only the sign description but omits the actual expected numeric value. Consider adding (" + expected + ") after signName(expected) so the message shows both the sign and the numeric value.
                + signName(expected)

dconeybe added a commit to googleapis/java-firestore that referenced this pull request Jul 7, 2025
The semantics of this logic were originally fixed by #1967, but this fix
caused a material performance degradation, which was then improved by #2021.
The performance was, however, still suboptimal, and this PR further improves the
speed back to close to its original speed and, serendipitously, simplifies the
algorithm too.

This commit effectively ports the following two PRs from the
firebase-android-sdk repository:

- firebase/firebase-android-sdk#7098
- firebase/firebase-android-sdk#7109
@dconeybe dconeybe marked this pull request as ready for review July 7, 2025 18:08
@google-oss-bot
Copy link
Contributor

Startup Time Report 1

Note: Layout is sometimes suboptimal due to limited formatting support on GitHub. Please check this report on GCS.

Notes

Startup Times

  • fire-fst

    DeviceStatisticsDistributions
    oriole-32
    Percentile84c4d814383afeDiffSignificant (?)
    p10286 ±14 μs270 ±8 μs-16.3 μs (-5.7%)NO
    p25297 ±16 μs276 ±8 μs-20.9 μs (-7.0%)NO
    p50315 ±28 μs285 ±8 μs-30.4 μs (-9.6%)NO
    p75352 ±64 μs296 ±10 μs-56.5 μs (-16.1%)NO
    p90382 ±84 μs312 ±11 μs-69.7 μs (-18.2%)NO

    20 test runs in comparison
    CommitTest Runs
    84c4d81
    • 2025-07-07_16:21:54.999985_hxdj
    • 2025-07-07_16:21:55.005010_cbAr
    • 2025-07-07_16:21:55.005020_rTli
    • 2025-07-07_16:21:55.005026_Emkf
    • 2025-07-07_16:21:55.005030_HNqH
    • 2025-07-07_16:21:55.005035_CVUr
    • 2025-07-07_16:21:55.005039_WJNb
    • 2025-07-07_16:21:55.005043_uTiC
    • 2025-07-07_16:21:55.005047_cPSo
    • 2025-07-07_16:21:55.005051_DsXX
    4383afe
    • 2025-07-07_18:00:50.226563_ALve
    • 2025-07-07_18:00:50.228130_aszU
    • 2025-07-07_18:00:50.228145_lGLi
    • 2025-07-07_18:00:50.228151_Cydd
    • 2025-07-07_18:00:50.228156_Xffo
    • 2025-07-07_18:00:50.228160_QKVx
    • 2025-07-07_18:00:50.228165_hIWR
    • 2025-07-07_18:00:50.228169_TiRz
    • 2025-07-07_18:00:50.228172_dcNZ
    • 2025-07-07_18:00:50.228176_qejN
    redfin-30
    Percentile84c4d814383afeDiffSignificant (?)
    p10501 ±59 μs529 ±36 μs+28.0 μs (+5.6%)NO
    p25515 ±59 μs544 ±34 μs+29.1 μs (+5.6%)NO
    p50534 ±60 μs569 ±30 μs+35.9 μs (+6.7%)NO
    p75556 ±62 μs601 ±35 μs+44.8 μs (+8.1%)NO
    p90588 ±68 μs640 ±49 μs+52.0 μs (+8.9%)NO

    20 test runs in comparison
    CommitTest Runs
    84c4d81
    • 2025-07-07_16:21:54.999985_hxdj
    • 2025-07-07_16:21:55.005010_cbAr
    • 2025-07-07_16:21:55.005020_rTli
    • 2025-07-07_16:21:55.005026_Emkf
    • 2025-07-07_16:21:55.005030_HNqH
    • 2025-07-07_16:21:55.005035_CVUr
    • 2025-07-07_16:21:55.005039_WJNb
    • 2025-07-07_16:21:55.005043_uTiC
    • 2025-07-07_16:21:55.005047_cPSo
    • 2025-07-07_16:21:55.005051_DsXX
    4383afe
    • 2025-07-07_18:00:50.226563_ALve
    • 2025-07-07_18:00:50.228130_aszU
    • 2025-07-07_18:00:50.228145_lGLi
    • 2025-07-07_18:00:50.228151_Cydd
    • 2025-07-07_18:00:50.228156_Xffo
    • 2025-07-07_18:00:50.228160_QKVx
    • 2025-07-07_18:00:50.228165_hIWR
    • 2025-07-07_18:00:50.228169_TiRz
    • 2025-07-07_18:00:50.228172_dcNZ
    • 2025-07-07_18:00:50.228176_qejN
  • timeToInitialDisplay

    DeviceStatisticsDistributions
    oriole-32
    Percentile84c4d814383afeDiffSignificant (?)
    p10198 ±4 ms201 ±3 ms+3.09 ms (+1.6%)NO
    p25204 ±4 ms206 ±3 ms+2.23 ms (+1.1%)NO
    p50211 ±4 ms213 ±3 ms+2.37 ms (+1.1%)NO
    p75218 ±4 ms220 ±3 ms+2.98 ms (+1.4%)NO
    p90225 ±4 ms231 ±6 ms+5.85 ms (+2.6%)NO

    20 test runs in comparison
    CommitTest Runs
    84c4d81
    • 2025-07-07_16:21:54.999985_hxdj
    • 2025-07-07_16:21:55.005010_cbAr
    • 2025-07-07_16:21:55.005020_rTli
    • 2025-07-07_16:21:55.005026_Emkf
    • 2025-07-07_16:21:55.005030_HNqH
    • 2025-07-07_16:21:55.005035_CVUr
    • 2025-07-07_16:21:55.005039_WJNb
    • 2025-07-07_16:21:55.005043_uTiC
    • 2025-07-07_16:21:55.005047_cPSo
    • 2025-07-07_16:21:55.005051_DsXX
    4383afe
    • 2025-07-07_18:00:50.226563_ALve
    • 2025-07-07_18:00:50.228130_aszU
    • 2025-07-07_18:00:50.228145_lGLi
    • 2025-07-07_18:00:50.228151_Cydd
    • 2025-07-07_18:00:50.228156_Xffo
    • 2025-07-07_18:00:50.228160_QKVx
    • 2025-07-07_18:00:50.228165_hIWR
    • 2025-07-07_18:00:50.228169_TiRz
    • 2025-07-07_18:00:50.228172_dcNZ
    • 2025-07-07_18:00:50.228176_qejN
    redfin-30
    Percentile84c4d814383afeDiffSignificant (?)
    p10221 ±3 ms246 ±3 ms+25.2 ms (+11.4%)YES
    p25227 ±3 ms252 ±4 ms+25.5 ms (+11.3%)YES
    p50234 ±3 ms260 ±5 ms+25.4 ms (+10.8%)YES
    p75242 ±4 ms270 ±5 ms+27.7 ms (+11.4%)YES
    p90250 ±4 ms284 ±8 ms+34.1 ms (+13.6%)YES

    20 test runs in comparison
    CommitTest Runs
    84c4d81
    • 2025-07-07_16:21:54.999985_hxdj
    • 2025-07-07_16:21:55.005010_cbAr
    • 2025-07-07_16:21:55.005020_rTli
    • 2025-07-07_16:21:55.005026_Emkf
    • 2025-07-07_16:21:55.005030_HNqH
    • 2025-07-07_16:21:55.005035_CVUr
    • 2025-07-07_16:21:55.005039_WJNb
    • 2025-07-07_16:21:55.005043_uTiC
    • 2025-07-07_16:21:55.005047_cPSo
    • 2025-07-07_16:21:55.005051_DsXX
    4383afe
    • 2025-07-07_18:00:50.226563_ALve
    • 2025-07-07_18:00:50.228130_aszU
    • 2025-07-07_18:00:50.228145_lGLi
    • 2025-07-07_18:00:50.228151_Cydd
    • 2025-07-07_18:00:50.228156_Xffo
    • 2025-07-07_18:00:50.228160_QKVx
    • 2025-07-07_18:00:50.228165_hIWR
    • 2025-07-07_18:00:50.228169_TiRz
    • 2025-07-07_18:00:50.228172_dcNZ
    • 2025-07-07_18:00:50.228176_qejN

  1. https://storage.googleapis.com/firebase-sdk-metric-reports/6xQfr8xMrv/index.html

@dconeybe dconeybe requested a review from tom-andersen July 7, 2025 18:46
@dconeybe dconeybe merged commit 1b0a574 into main Jul 7, 2025
34 checks passed
@dconeybe dconeybe deleted the dconeybe/firestore/UtilCompareIntegersToIntegerCompare branch July 7, 2025 19:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants