Skip to content

perf: experiment with an alternate binary search implementation #9852

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

Merged
merged 3 commits into from
Jul 8, 2025

Conversation

vicb
Copy link
Contributor

@vicb vicb commented Jul 4, 2025

Experiment with an alternate binary search implementation for asset.

The new implementation is ~10% faster in test.

The PR has 2 commits for easier review:
The first commit has the changes to the algorithm.
The second commit add the experiment with both the current and the updated version.

/cc @GregBrimble


  • Tests
    • Tests included
    • Tests not necessary because:
  • Public documentation
    • Cloudflare docs PR(s):
    • Documentation not necessary because: perf test
  • Wrangler V3 Backport
    • Wrangler PR:
    • Not necessary because: not a Wrangler change

@vicb vicb requested review from a team as code owners July 4, 2025 07:36
Copy link

changeset-bot bot commented Jul 4, 2025

⚠️ No Changeset found

Latest commit: badeed5

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

pkg-pr-new bot commented Jul 4, 2025

create-cloudflare

npm i https://pkg.pr.new/create-cloudflare@9852

@cloudflare/kv-asset-handler

npm i https://pkg.pr.new/@cloudflare/kv-asset-handler@9852

miniflare

npm i https://pkg.pr.new/miniflare@9852

@cloudflare/pages-shared

npm i https://pkg.pr.new/@cloudflare/pages-shared@9852

@cloudflare/unenv-preset

npm i https://pkg.pr.new/@cloudflare/unenv-preset@9852

@cloudflare/vite-plugin

npm i https://pkg.pr.new/@cloudflare/vite-plugin@9852

@cloudflare/vitest-pool-workers

npm i https://pkg.pr.new/@cloudflare/vitest-pool-workers@9852

@cloudflare/workers-editor-shared

npm i https://pkg.pr.new/@cloudflare/workers-editor-shared@9852

wrangler

npm i https://pkg.pr.new/wrangler@9852

commit: badeed5

@@ -231,6 +232,12 @@ export default class<TEnv extends Env = Env> extends WorkerEntrypoint<TEnv> {
pathname: string,
_request?: Request
): Promise<string | null> {
const BINARY_SEARCH_EXPERIMENT_SAMPLE_RATE = 0.5;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd consider starting off with a lower threshold. 5%? Just make sure no errors are thrown — then we can jump up to more accurately compare performance.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd consider starting off with a lower threshold. 5%? Just make sure no errors are thrown — then we can jump up to more accurately compare performance.

That is something I actually wanted to discuss with you and I think makes complete sense.

@github-project-automation github-project-automation bot moved this from Untriaged to Approved in workers-sdk Jul 7, 2025
@vicb vicb force-pushed the vicb/binarySearch branch from 96ae732 to bda3956 Compare July 8, 2025 15:02
vicb added 3 commits July 8, 2025 17:19
Optimizations:
- remove recursion to use a while loop instead
- do not allocate unnecessary array (to reduce GC)
- convert the manifest to a Uint8Array once upfront
- added a faster vesrion of compare (compareSearchValueWithEntry). The compare implementatiopn was moved to the tests where it is used

Other changes
- BS: use indexes instead of offest (IMO easier to read/debug)
- BS: return the content hash instead of the whole entry
@vicb vicb force-pushed the vicb/binarySearch branch from 98d25f1 to badeed5 Compare July 8, 2025 15:20
@vicb
Copy link
Contributor Author

vicb commented Jul 8, 2025

@GregBrimble I have rebased to fix an issue with the CI (the main branch was failing when I branched off).

I have reduced the experiment to 5% of the request for now and added a fallback to the current impl.

Let me know if it looks good.

Thanks!

@vicb vicb requested a review from GregBrimble July 8, 2025 15:22
Copy link
Contributor

@GregBrimble GregBrimble left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect, thank you!

@vicb vicb added this pull request to the merge queue Jul 8, 2025
Merged via the queue into main with commit 1f3050b Jul 8, 2025
39 of 42 checks passed
@vicb vicb deleted the vicb/binarySearch branch July 8, 2025 17:30
@github-project-automation github-project-automation bot moved this from Approved to Done in workers-sdk Jul 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants