Skip to content

fix: Further improve performance of the UTF-8 string comparison logic #1

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

dconeybe
Copy link
Owner

@dconeybe dconeybe commented Jul 7, 2025

This PR is a mirror of googleapis#2182

The semantics of the UTF-8 string comparison logic were originally fixed by googleapis#1967, but this fix caused a material performance degradation, which was then improved by googleapis#2021. The performance was, however, still sub-optimal, and this PR further improves the speed back to close to its original speed and, serendipitously, simplifies the algorithm too.

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

The semantics of this logic were originally fixed by googleapis#1967, but this fix
caused a material performance degradation, which was then improved by googleapis#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 requested a review from Copilot July 7, 2025 18:02
Copy link

@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.

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 crucial performance enhancement and simplification to the UTF-8 string comparison logic within the Firestore client library. It addresses prior performance regressions by introducing a highly optimized and more elegant algorithm for string ordering, ensuring efficient and correct data sorting.

Highlights

  • Performance Optimization: The core compareUtf8Strings method in Order.java has been completely rewritten to significantly improve performance and simplify its logic. The new algorithm leverages the relationship between UTF-8 and UTF-16 representations for more efficient string comparison, avoiding costly byte string conversions.
  • Algorithm Simplification: The previous complex logic involving codePointAt and ByteString.copyFromUtf8 for non-ASCII characters has been replaced with a more straightforward character-by-character comparison that intelligently handles surrogate pairs.
  • Test Robustness: The OrderTest.java has been updated to improve the robustness of the compareUtf8Strings test. Instead of asserting exact return values, it now checks the signum (sign) of the comparison result, which is a more appropriate way to validate comparator behavior.
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

  1. 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.

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 further improves performance of Firebase’s UTF-8 string comparison by simplifying the algorithm and adjusts the corresponding tests to validate only the comparison sign.

  • Simplified compareUtf8Strings in Order.java to a single-pass UTF-16 char comparison leveraging surrogate checks.
  • Updated OrderTest.java to compare only the sign of results and introduced a signName helper for clearer error messages.

Reviewed Changes

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

File Description
Order.java Replaced the multi-step code-point and byte-encoding logic with a direct UTF-16 char loop using Character.isSurrogate and Character.compare for performance.
OrderTest.java Changed test assertions to use Integer.signum, added signName(int) to describe result signs, and updated error output accordingly.
Comments suppressed due to low confidence (1)

google-cloud-firestore/src/test/java/com/google/cloud/firestore/OrderTest.java:235

  • The error message prints only the descriptive expected sign but omits the actual numeric expected value. For consistency with the actual output, include (" + expected + ") after signName(expected) to display the numeric value as well.
                + signName(expected)

Copy link

@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 improves the performance of UTF-8 string comparison. The new implementation is simpler and the test suite has been updated to verify the comparator's behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant