Skip to content

Optimize value coercion #5356

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

Closed
wants to merge 2 commits into from

Conversation

JoviDeCroock
Copy link
Contributor

@JoviDeCroock JoviDeCroock commented May 17, 2025

Optimize Argument Coercion Performance

This PR attempts to optimize the argument coercion process in GraphQL-Ruby by adding fast paths for common scenarios. I thought I'd create it as a draft so folks could give input and the CI would run.

Looks like I was a bit too eager, looking into the tests etc

Changes

  1. List Type Coercion (schema/list.rb)
    • Added fast path to avoid unnecessary array creation when input is already an array
    • Special handling for scalar inputs that need conversion to arrays
  2. Arguments Cache (execution/interpreter/arguments_cache.rb)
    • Added optimization to skip argument materialization for empty arguments
    • Fast path when argument owner has no arguments defined
  3. Argument Handling (schema/member/has_arguments.rb)
    • Added fast path for simple cases with small number of arguments (0-2)
    • Simplified execution flow for cases without lazy values
    • Added method to detect if arguments have default values
  4. Argument Coercion (schema/argument.rb)
    • Optimized coerce_into_values with a fast path for statically coercible arguments
    • Direct value handling for simple cases without loading or preparation

Benchmarks

Performance improvements measured with isolated benchmarks, these were custom made as I couldn't get the benchmarks of the repository to run well 😅 :

  • List Coercion:

    • Array input: ~6% improvement
    • Scalar input: ~48% improvement
  • Argument Coercion:

    • Empty arguments: ~74% improvement
    • Simple arguments (1-2 args): ~8% improvement
    • Complex arguments (5+ args): ~5% improvement

@rmosolgo
Copy link
Owner

Hey, thanks for looking into these! If you're interested in pursuing them further, I'd be happy to help debug test failures -- but it'd be a lot easier if you could open PRs for the one-by-one, just so there wouldn't be quite as many moving parts involved.

@JoviDeCroock JoviDeCroock deleted the optimizations branch May 19, 2025 16:20
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