Skip to content
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

Use MurmurHash3 when native hashing function is not available #27399

Merged
merged 1 commit into from
Sep 21, 2023

Conversation

acdlite
Copy link
Collaborator

@acdlite acdlite commented Sep 20, 2023

Adds a pure JS implementation of a string hashing function. We do not use it for security or obfuscation purposes, only to create compact hashes. So we prioritize speed over collision avoidance. For example, we use this to hash the component key path used by useFormState for MPA-style submissions. See #27397 for details.

In environments where built-in hashing functions are available, we prefer those instead. Like Node's crypto module, or Bun.hash. Unfortunately this does not include the web standard crypto API because those methods are all async. For our purposes, we need it to be sync because the cost of context switching is too high to be worth it.

The most popular hashing algorithm that meets these requirements in the JS ecosystem is MurmurHash3, and almost all implementations I could find used some version of the implementation by Gary Court. So that's the one I've used.

In the future we should try to migrate these to native calls whenever possible. It's especially unfortunate that the Edge build doesn't use a native implementation, because that's the one used by newer frameworks like Next.js.

@facebook-github-bot facebook-github-bot added CLA Signed React Core Team Opened by a member of the React Core Team labels Sep 20, 2023
@react-sizebot
Copy link

react-sizebot commented Sep 20, 2023

Comparing: 2b3d582...acf0b72

Critical size changes

Includes critical production bundles, as well as any change greater than 2%:

Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable/react-dom/cjs/react-dom.production.min.js = 166.62 kB 166.62 kB = 52.13 kB 52.13 kB
oss-experimental/react-dom/cjs/react-dom.production.min.js = 176.05 kB 176.05 kB = 54.97 kB 54.97 kB
facebook-www/ReactDOM-prod.classic.js = 571.73 kB 571.73 kB = 100.64 kB 100.64 kB
facebook-www/ReactDOM-prod.modern.js = 555.46 kB 555.46 kB = 97.75 kB 97.75 kB
oss-experimental/react-dom/cjs/react-dom-server-legacy.browser.production.min.js +2.89% 71.36 kB 73.42 kB +4.55% 21.47 kB 22.45 kB
oss-experimental/react-dom/umd/react-dom-server-legacy.browser.production.min.js +2.89% 71.51 kB 73.58 kB +4.51% 21.81 kB 22.79 kB
oss-experimental/react-dom/cjs/react-dom-server.browser.production.min.js +2.72% 76.01 kB 78.07 kB +4.22% 23.03 kB 24.00 kB
oss-experimental/react-dom/umd/react-dom-server.browser.production.min.js +2.71% 76.13 kB 78.20 kB +4.26% 23.37 kB 24.36 kB
oss-experimental/react-dom/cjs/react-dom-server-legacy.node.production.min.js +2.67% 77.38 kB 79.44 kB +4.21% 23.31 kB 24.30 kB
oss-experimental/react-dom/cjs/react-dom-server.edge.production.min.js +2.54% 81.35 kB 83.42 kB +4.15% 24.69 kB 25.71 kB

Significant size changes

Includes any change greater than 0.2%:

Expand to show
Name +/- Base Current +/- gzip Base gzip Current gzip
oss-experimental/react-dom/cjs/react-dom-server-legacy.browser.production.min.js +2.89% 71.36 kB 73.42 kB +4.55% 21.47 kB 22.45 kB
oss-experimental/react-dom/umd/react-dom-server-legacy.browser.production.min.js +2.89% 71.51 kB 73.58 kB +4.51% 21.81 kB 22.79 kB
oss-experimental/react-dom/cjs/react-dom-server.browser.production.min.js +2.72% 76.01 kB 78.07 kB +4.22% 23.03 kB 24.00 kB
oss-experimental/react-dom/umd/react-dom-server.browser.production.min.js +2.71% 76.13 kB 78.20 kB +4.26% 23.37 kB 24.36 kB
oss-experimental/react-dom/cjs/react-dom-server-legacy.node.production.min.js +2.67% 77.38 kB 79.44 kB +4.21% 23.31 kB 24.30 kB
oss-experimental/react-dom/cjs/react-dom-server.edge.production.min.js +2.54% 81.35 kB 83.42 kB +4.15% 24.69 kB 25.71 kB
facebook-www/ReactDOMServer-prod.modern.js +1.75% 173.81 kB 176.86 kB +3.87% 30.70 kB 31.89 kB
facebook-www/ReactDOMServer-prod.classic.js +1.74% 174.75 kB 177.79 kB +3.83% 30.94 kB 32.13 kB
facebook-www/ReactDOMServerStreaming-prod.modern.js +1.67% 182.18 kB 185.23 kB +3.59% 32.74 kB 33.92 kB
facebook-www/ReactDOMServerStreaming-dev.modern.js +1.00% 384.45 kB 388.28 kB +1.87% 83.21 kB 84.77 kB
facebook-www/ReactDOMServer-dev.modern.js +0.98% 389.75 kB 393.58 kB +1.87% 84.47 kB 86.04 kB
facebook-www/ReactDOMServer-dev.classic.js +0.96% 397.18 kB 401.01 kB +1.82% 86.11 kB 87.68 kB
oss-experimental/react-dom/cjs/react-dom-server-legacy.browser.development.js +0.92% 397.26 kB 400.94 kB +1.78% 86.39 kB 87.93 kB
oss-experimental/react-dom/umd/react-dom-server-legacy.browser.development.js +0.92% 416.19 kB 420.02 kB +1.81% 87.76 kB 89.35 kB
oss-experimental/react-dom/cjs/react-dom-server-legacy.node.development.js +0.92% 399.12 kB 402.79 kB +1.76% 86.86 kB 88.38 kB
oss-experimental/react-dom/cjs/react-dom-server.browser.development.js +0.91% 402.80 kB 406.47 kB +1.73% 87.06 kB 88.57 kB
oss-experimental/react-dom/cjs/react-dom-server.edge.development.js +0.91% 403.21 kB 406.88 kB +1.73% 87.18 kB 88.69 kB
oss-experimental/react-dom/umd/react-dom-server.browser.development.js +0.91% 421.99 kB 425.82 kB +1.74% 88.43 kB 89.97 kB

Generated by 🚫 dangerJS against acf0b72

Adds a pure JS implementation of a string hashing function. We do not
use it for security or obfuscation purposes, only to create compact
hashes. So we prioritize speed over collision avoidance. For example, we
use this to hash the component key path used by useFormState for
MPA-style submissions. See facebook#27397 for details.

In environments where built-in hashing functions are available, we
prefer those instead. Like Node's crypto module, or Bun.hash.
Unfortunately this does not include the web standard crypto API because
those methods are all async. For our purposes, we need it to be sync
because the cost of context switching is too high to be worth it.

The most popular hashing algorithm that meets these requirements in the
JS ecosystem is MurmurHash3, and almost all implementations I could find
used some version of the implementation by Gary Court. So that's the
one I've used.

In the future we should try to migrate these to native calls whenever
possible. It's especially unfortunate that the Edge build doesn't use
a native implementation, because that's the one used by newer frameworks
like Next.js.
@acdlite acdlite merged commit 68ac6db into facebook:main Sep 21, 2023
2 checks passed
github-actions bot pushed a commit that referenced this pull request Sep 21, 2023
bigfootjon pushed a commit that referenced this pull request Apr 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed React Core Team Opened by a member of the React Core Team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants