-
Notifications
You must be signed in to change notification settings - Fork 627
fix(firestore): Further improved performance of UTF-8 string comparison logic #7098
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
base: main
Are you sure you want to change the base?
fix(firestore): Further improved performance of UTF-8 string comparison logic #7098
Conversation
Firebase AI Mock Responses Check
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary of Changes
Hello @dconeybe, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request delivers a critical performance enhancement for Firestore's UTF-8 string ordering logic, which had previously regressed and caused customer-reported issues. It introduces a new, highly optimized comparison utility that avoids memory allocations, ensuring faster and more efficient string operations. This change also refactors the codebase by centralizing the comparison logic and updating existing components to leverage the new, improved implementation, alongside adding robust test coverage.
Highlights
- Performance Improvement: Implemented a highly optimized, allocation-free UTF-8 string comparison logic to address significant performance degradation in Firestore's string ordering, specifically fixing issue #7053.
- Code Refactoring: Extracted the UTF-8 string comparison logic into a new dedicated
Utf8Compare
utility class, centralizing the functionality and removing the less efficient, previous implementation fromUtil.java
. - Test Coverage: Introduced a new comprehensive test suite for the
Utf8Compare
class, including extensive random string generation to validate correctness across various Unicode characters and surrogate pairs.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command>
or @gemini-code-assist <command>
. Below is a summary of the supported commands.
Feature | Command | Description |
---|---|---|
Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/
folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
The pull request introduces a new Utf8Compare
class to improve the performance of UTF-8 string comparisons. The changes involve replacing calls to Util.compareUtf8Strings
with Utf8Compare.compareUtf8Strings
in BasePath
and Values
classes. Additionally, a new test class Utf8CompareTest
is added to verify the correctness of the new comparison logic. The old implementation of compareUtf8Strings
is removed from Util
class. The debug checks in Utf8Compare
class could be improved by logging errors instead of throwing exceptions.
firebase-firestore/src/main/java/com/google/firebase/firestore/util/Utf8Compare.java
Outdated
Show resolved
Hide resolved
firebase-firestore/src/main/java/com/google/firebase/firestore/util/Utf8Compare.java
Outdated
Show resolved
Hide resolved
firebase-firestore/src/main/java/com/google/firebase/firestore/util/Utf8Compare.java
Outdated
Show resolved
Hide resolved
firebase-firestore/src/main/java/com/google/firebase/firestore/util/Utf8Compare.java
Outdated
Show resolved
Hide resolved
Coverage Report 1Affected Products
Test Logs |
Test Results 186 files - 797 186 suites - 797 4m 42s ⏱️ - 29m 1s Results for commit 02c2371. ± Comparison against base commit ec904ec. This pull request removes 4317 tests.
♻️ This comment has been updated with latest results. |
Size Report 1Affected Products
Test Logs |
Startup Time Report 1Note: Layout is sometimes suboptimal due to limited formatting support on GitHub. Please check this report on GCS. Notes
Startup Times
|
/gemini review |
There was a problem hiding this 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 replaces the previous UTF-8 byte-oriented string comparison with a more efficient surrogate-aware char-by-char algorithm and updates the changelog to document the fix.
- Simplified
compareUtf8Strings
to a single-pass char comparison usingCharacter.isSurrogate
- Removed
ByteString
fallback and eliminated codePoint/substring overhead - Added an unreleased entry in
CHANGELOG.md
for the performance fix
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
File | Description |
---|---|
firebase-firestore/src/main/java/com/google/firebase/firestore/util/Util.java | Rewrote compareUtf8Strings with optimized surrogate-aware logic; removed helper and ByteString paths |
firebase-firestore/CHANGELOG.md | Documented the UTF-8 comparison performance improvement under Unreleased |
Comments suppressed due to low confidence (2)
firebase-firestore/src/main/java/com/google/firebase/firestore/util/Util.java:90
- Update this Javadoc to describe the new surrogate-aware, char-by-char comparison algorithm rather than the previous byte-level UTF-8 logic.
/** Compare strings in UTF-8 encoded byte order */
firebase-firestore/src/main/java/com/google/firebase/firestore/util/Util.java:113
- Add unit tests covering edge cases around surrogate pairs versus non-surrogate characters to verify the new comparison logic across diverse Unicode inputs.
for (int i = 0; i < length; i++) {
There was a problem hiding this 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 introduces a performance improvement to the UTF-8 string comparison logic by avoiding object allocations. Suggestions have been provided to improve code readability.
firebase-firestore/src/main/java/com/google/firebase/firestore/util/Util.java
Outdated
Show resolved
Hide resolved
firebase-firestore/src/main/java/com/google/firebase/firestore/util/Util.java
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is wonderful, thanks Denver!
Further improved performance of UTF-8 string ordering logic, which had degraded in v25.1.2 due to the fix #6615 and received some improvements in v25.1.3 by the fix #6706. Even with these improvements, customers still observed a material performance degradation (#7053), which this PR addresses.
This PR was ported to the web sdk in firebase/firebase-js-sdk#9143
Fixes: #7053