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

OverlappingFieldsCanBeMergedRule: Fix performance degradation #3958

Merged
merged 4 commits into from
Sep 5, 2023
Merged

OverlappingFieldsCanBeMergedRule: Fix performance degradation #3958

merged 4 commits into from
Sep 5, 2023

Conversation

AaronMoat
Copy link
Contributor

@AaronMoat AaronMoat commented Aug 24, 2023

Update(from @IvanGoncharov): In addition to reversing #3457 this PR improves performance further, see below discussion.

Resolves #3955 (at least greatly mitigates it)

Effectively reverts #3457 which introduces a performance degradation found in #3955. This pull request was identified in a git bisect. The performance issue results in the potential for DOS attacks.

I think there are potential performance increases still available in this file, but this is a simple enough fix for the immediate problem.

The following query:

const maliciousQuery = `{ ${'hello '.repeat(2000)}}`;

now takes 564ms on my machine, and on main takes 12s.

@netlify
Copy link

netlify bot commented Aug 24, 2023

Deploy Preview for compassionate-pike-271cb3 ready!

Name Link
🔨 Latest commit 2991112
🔍 Latest deploy log https://app.netlify.com/sites/compassionate-pike-271cb3/deploys/64f771eb500b3b00086f752e
😎 Deploy Preview https://deploy-preview-3958--compassionate-pike-271cb3.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@AaronMoat
Copy link
Contributor Author

Hi @graphql/graphql-js-reviewers (I can't seem to ping this list - so @martijnwalraven @Cito @yaacovCR @IvanGoncharov @saihaj) any chance we could get some eyes on this? 🙏

@tadhglewis
Copy link

tadhglewis commented Aug 28, 2023

I can create a issue as this can be done in a separate PR but it's somewhat concerning this regression was possible in the first place. Would be great to have some perf tests in place.

@github-actions
Copy link

Hi @AaronMoat, I'm @github-actions bot happy to help you with this PR 👋

Supported commands

Please post this commands in separate comments and only one per comment:

  • @github-actions run-benchmark - Run benchmark comparing base and merge commits for this PR
  • @github-actions publish-pr-on-npm - Build package from this PR and publish it on NPM

@AaronMoat

This comment has been minimized.

@github-actions
Copy link

@github-actions run-benchmark

@AaronMoat Please, see benchmark results here: https://github.com/graphql/graphql-js/actions/runs/6011999302/job/16306504508#step:6:1

@graphql graphql deleted a comment from github-actions bot Aug 30, 2023
@yaacovCR
Copy link
Contributor

yaacovCR commented Aug 30, 2023

If I’m interpreting the benchmark results correctly, I am not seeing a performance improvement with this fix…

@AaronMoat
Copy link
Contributor Author

I don't think this specific scenario is in the benchmarks.

@yaacovCR
Copy link
Contributor

I don't think this specific scenario is in the benchmarks.

I guess what I’m wondering is (1) where the crossover point is, as performance seems better on main with our current tests but worse in your above scenario, and (2) whether the better mitigation is to use the maxTokens option when parsing?

maxTokens?: number | undefined;

That’s just my own 2 cents, would really love @IvanGoncharov take

@AaronMoat
Copy link
Contributor Author

The note on maxTokens is an interesting one, noting it's linear. I'd argue this is nonlinear, even with this change.

@IvanGoncharov
Copy link
Member

@AaronMoat Can you please add a benchmark to this PR otherwise this problem can reappear in the future.

@AaronMoat

This comment has been minimized.

@github-actions
Copy link

github-actions bot commented Sep 1, 2023

@github-actions run-benchmark

@AaronMoat Something went wrong, please check log.

@AaronMoat

This comment has been minimized.

@github-actions
Copy link

github-actions bot commented Sep 2, 2023

@github-actions run-benchmark

@AaronMoat Something went wrong, please check log.

@AaronMoat
Copy link
Contributor Author

Unsure what's going on with the benchmarks; it seems to have not even got up to the new one this time.

@IvanGoncharov
Copy link
Member

@AaronMoat Sorry for delay, I spend some time playing with this code and came up with 2991112 it further improves performance:
image

It reduces memory usage by ~40%.

@IvanGoncharov IvanGoncharov added the PR: bug fix 🐞 requires increase of "patch" version number label Sep 5, 2023
@IvanGoncharov
Copy link
Member

@AaronMoat If you need this fix in v16, please a create separate PR for:
https://github.com/graphql/graphql-js/tree/16.x.x

@IvanGoncharov IvanGoncharov changed the title Fix performance degradation OverlappingFieldsCanBeMergedRule: Fix performance degradation Sep 5, 2023
@IvanGoncharov IvanGoncharov merged commit f94b511 into graphql:main Sep 5, 2023
19 checks passed
@AaronMoat AaronMoat deleted the fix-performance-degradation branch September 5, 2023 22:17
@AaronMoat
Copy link
Contributor Author

Thanks @IvanGoncharov for taking that and running with it -- raised #3967 for 16.x.x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: bug fix 🐞 requires increase of "patch" version number
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Resource exhaustion exploit when parsing queries
4 participants