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

Reuse the resolution struct to avoid unnecessary allocations #636

Merged
merged 2 commits into from
Nov 16, 2018

Conversation

benwilson512
Copy link
Contributor

Wide Query

Name                     ips        average  deviation         median         99th %
wide-query-new        803.28        1.24 ms    ±20.39%        1.13 ms        1.98 ms
wide-query-old        505.16        1.98 ms    ±11.62%        1.99 ms        2.54 ms

Comparison: 
wide-query-new        803.28
wide-query-old        505.16 - 1.59x slower

Memory usage statistics:

Name              Memory usage
wide-query-new         1.26 MB
wide-query-old         2.29 MB - 1.82x memory usage

Deep Query

Name                     ips        average  deviation         median         99th %
deep-query-new         84.07       11.89 ms    ±13.62%       11.65 ms       16.01 ms
deep-query-old         67.71       14.77 ms    ±10.15%       14.56 ms       18.74 ms

Comparison: 
deep-query-new         84.07
deep-query-old         67.71 - 1.24x slower

Memory usage statistics:

Name              Memory usage
deep-query-new         9.00 MB
deep-query-old        14.25 MB - 1.58x memory usage

definition: nil,
arguments: nil
}
|> Map.merge(common)
Copy link
Contributor

Choose a reason for hiding this comment

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

%{adapter: adapter, context: context, acc: acc, root_value: root_value, schema: schema, fragments: fragments, fields_cache: fields_cache} = exec

%Absinthe.Resolution{
  path: nil,
  source: nil,
  parent_type: nil,
  middleware: nil,
  definition: nil,
  arguments: nil,
  adapter: adapter, 
  context: context, 
  acc: acc, 
  root_value: root_value, 
  schema: schema, 
  fragments: fragments, 
  fields_cache: fields_cache
}

explicit maps should be even slightly more performant, though quite verbose in here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, this allocation happens just once per request though, the biggest win was https://github.com/absinthe-graphql/absinthe/pull/636/files#diff-9e88b6c0fafa4e1d518da81f1c8f16a8L195 which is called for every field, potentially thousands of times.

Copy link

Choose a reason for hiding this comment

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

so a resolution is still created on every request? I was noticing how big that struct is and wondered if it would slow things down to be created on every request when you are trying to handle dozens per second on an instance. googling led me here :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @atomkirk dozens of requests per second should be trivially achieved on a single instance, the presence of large maps is not going to be your bottleneck.

Copy link

Choose a reason for hiding this comment

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

I think it probably also greatly reduces allocations when the vast majority of the keys are atoms. I would assume atoms are shared across all of the vm's processes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is correct, structs benefit from the VM's constant heap allocation. Not just the keys, but the entire "key structure" part of the map is allocated once when the application boots, and then referenced.

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.

3 participants