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

Optimize VariableCoercer #1856

Merged
merged 3 commits into from
Aug 28, 2023
Merged

Conversation

kyri-petrou
Copy link
Collaborator

@kyri-petrou kyri-petrou commented Aug 26, 2023

Seems that when we refactored the Validator to use ZPure we forgot(?) to do it with the VariableCoercer. Reality is that in most cases the variables will be a small / shallow object so this doesn't really provide much performance benefits for most users, except perhaps in cases where the inputs are deeply nested JSON files.

In this PR there are 2 different optimizations:

  1. Use ZPure in favour of ZIO effects when recursing
  2. Make context a by-name argument. This prevents us building the error context except when we need to fail the validation

Benchmarking results

Before:

[info] Benchmark                              Mode  Cnt      Score     Error  Units
[info] ValidationBenchmark.variableCoercer100   thrpt    5  14006.365 ± 180.586  ops/s
[info] ValidationBenchmark.variableCoercer1000  thrpt    5    518.925 ±  18.125  ops/s

After:

[info] Benchmark                                 Mode  Cnt      Score     Error  Units
[info] ValidationBenchmark.variableCoercer100   thrpt    5  87682.811 ± 661.300  ops/s
[info] ValidationBenchmark.variableCoercer1000  thrpt    5   8429.327 ±  28.180  ops/s

Copy link
Owner

@ghostdogpr ghostdogpr left a comment

Choose a reason for hiding this comment

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

Nice!

@@ -105,16 +105,18 @@ trait GraphQL[-R] { self =>
}
typeToValidate = if (intro) introspectionRootType else rootType
schemaToExecute = if (intro) introspectionRootSchema else schema
validatedReq <- VariablesCoercer.coerceVariables(request, doc, typeToValidate, skipValidation)
validatedVariables <-
VariablesCoercer
Copy link
Owner

Choose a reason for hiding this comment

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

This is super minor but how about extracting request.variables.getOrElse(Map.empty) into a variable so that hopefully this fits in one line and stays easier to read?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I was a bit wary of extracting them into a variable so we don't accidentally use them elsewhere but yeah the formatting was annoying me as well. Hopefully naming them unsafeVars (unsafeVariables didn't fit in a single line) will be clear enough that they're not to be used 😅

@kyri-petrou kyri-petrou merged commit 2268b67 into series/2.x Aug 28, 2023
@kyri-petrou kyri-petrou deleted the variable-coercer-optimizations branch August 28, 2023 05:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants