-
Notifications
You must be signed in to change notification settings - Fork 0
fix: 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
base: main
Are you sure you want to change the base?
Conversation
The semantics of this logic were originally fixed by googleapis#2275, but this fix caused a material performance degradation, which was then improved by googleapis#2299 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 is a port of firebase/firebase-js-sdk#9143
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 optimizes the UTF-8 string comparison logic to improve performance and simplify the algorithm. It achieves this by directly comparing UTF-16 code units and handling surrogate pairs appropriately. The changes result in faster string comparisons without sacrificing correctness.
Highlights
- Performance Improvement: The primary goal of this PR is to improve the performance of UTF-8 string comparison logic, bringing it back to near its original speed before a previous fix introduced a performance degradation.
- Algorithm Simplification: Serendipitously, the performance improvements also led to a simplification of the string comparison algorithm.
- UTF-16 Code Unit Comparison: The comparison logic now directly compares UTF-16 code units (chars) for efficiency, leveraging the way UTF-8 and UTF-16 represent Unicode code points.
- Surrogate Handling: The code handles surrogate pairs correctly, ensuring that strings containing surrogates are ordered appropriately relative to non-surrogate strings.
- New Helper Function: Introduced a new helper function
isSurrogate
to determine if a character is a surrogate. - Code Removal: Removed the
getUtf8SafeSubstring
function and the TextEncoder instantiation, simplifying the code and improving performance.
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.
Pull Request Overview
This PR rewrites compareUtf8Strings
to remove costly TextEncoder
usage and byte-level comparisons, instead comparing UTF-16 code units directly and handling surrogate pairs via a new isSurrogate
helper.
- Replaces multi-step UTF-8 byte encoding with direct character comparison.
- Introduces
isSurrogate
and removesgetUtf8SafeSubstring
andTextEncoder
logic. - Falls back to length-based comparison when one string is a prefix of the other.
Comments suppressed due to low confidence (2)
dev/src/order.ts:297
- [nitpick] Add a JSDoc comment for
isSurrogate
to explain its purpose, parameters, return value, and the expected range of inputs (e.g., that it checks for UTF-16 surrogate code units).
export function isSurrogate(s: string): boolean {
dev/src/order.ts:276
- Add unit tests covering the new code paths in
compareUtf8Strings
, including cases with surrogate pairs, non-surrogate characters, and prefix comparisons to ensure correct ordering for astral characters and edge cases.
const length = Math.min(left.length, right.length);
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 significantly improves the performance and simplifies the logic for UTF-8 string comparison by replacing a complex implementation that used TextEncoder
with a more direct character-by-character comparison. The new approach is clever and well-commented.
I have a couple of suggestions to further improve the new implementation's readability and encapsulation. Overall, this is a solid improvement.
The real PR is: googleapis#2380
The semantics of this logic were originally fixed by googleapis#2275, but this fix caused a material performance degradation, which was then improved by googleapis#2299 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 is a port of firebase/firebase-js-sdk#9143