Skip to content

perf: Use charCodeAt instead of TextEncoder for improved UTF string comparison performance. #8779

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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/firestore/src/model/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { Integer } from '@firebase/webchannel-wrapper/bloom-blob';

import { debugAssert, fail } from '../util/assert';
import { Code, FirestoreError } from '../util/error';
import { primitiveComparator, compareUtf8Strings } from '../util/misc';
import { primitiveComparator, compareUtf16Strings } from '../util/misc';

export const DOCUMENT_KEY_NAME = '__name__';

Expand Down Expand Up @@ -202,7 +202,7 @@ abstract class BasePath<B extends BasePath<B>> {
);
} else {
// both non-numeric
return compareUtf8Strings(lhs, rhs);
return compareUtf16Strings(lhs, rhs);
}
}

Expand Down
6 changes: 3 additions & 3 deletions packages/firestore/src/model/values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { fail } from '../util/assert';
import {
arrayEquals,
primitiveComparator,
compareUtf8Strings
compareUtf16Strings
} from '../util/misc';
import { forEach, objectSize } from '../util/obj';
import { isNegativeZero } from '../util/types';
Expand Down Expand Up @@ -255,7 +255,7 @@ export function valueCompare(left: Value, right: Value): number {
getLocalWriteTime(right)
);
case TypeOrder.StringValue:
return compareUtf8Strings(left.stringValue!, right.stringValue!);
return compareUtf16Strings(left.stringValue!, right.stringValue!);
case TypeOrder.BlobValue:
return compareBlobs(left.bytesValue!, right.bytesValue!);
case TypeOrder.RefValue:
Expand Down Expand Up @@ -404,7 +404,7 @@ function compareMaps(left: MapValue, right: MapValue): number {
rightKeys.sort();

for (let i = 0; i < leftKeys.length && i < rightKeys.length; ++i) {
const keyCompare = compareUtf8Strings(leftKeys[i], rightKeys[i]);
const keyCompare = compareUtf16Strings(leftKeys[i], rightKeys[i]);
if (keyCompare !== 0) {
return keyCompare;
}
Expand Down
15 changes: 5 additions & 10 deletions packages/firestore/src/util/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,15 @@ export interface Equatable<T> {
isEqual(other: T): boolean;
}

/** Compare strings in UTF-8 encoded byte order */
export function compareUtf8Strings(left: string, right: string): number {
// Convert the string to UTF-8 encoded bytes
const encodedLeft = newTextEncoder().encode(left);
const encodedRight = newTextEncoder().encode(right);

for (let i = 0; i < Math.min(encodedLeft.length, encodedRight.length); i++) {
const comparison = primitiveComparator(encodedLeft[i], encodedRight[i]);
/** Compare strings in UTF-16 encoded byte order */
export function compareUtf16Strings(left: string, right: string): number {
for (let i = 0; i < Math.min(left.length, right.length); i++) {
const comparison = primitiveComparator(left.charCodeAt(i), right.charCodeAt(i));
if (comparison !== 0) {
return comparison;
}
}

return primitiveComparator(encodedLeft.length, encodedRight.length);
return primitiveComparator(left.length, right.length);
}

export interface Iterable<V> {
Expand Down