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

Reduce more allocations in Lookahead#selects? #4212

Merged
merged 3 commits into from
Oct 6, 2022
Merged

Conversation

rmosolgo
Copy link
Owner

@rmosolgo rmosolgo commented Sep 28, 2022

More fixing for #4187

I used this script to check my changes:

require "bundler/inline"

gemfile do
  gem "graphql", path: "./"
  gem "memory_profiler"
end

class Schema < GraphQL::Schema
  class Query < GraphQL::Schema::Object
    field :field, String
    field :field2, String
  end

  query(Query)
end

query = GraphQL::Query.new(Schema, "{ field }")
lookahead = query.lookahead
lookahead.selects?(:field)
res = nil
report = MemoryProfiler.report do
  res = [
    lookahead.selects?(:field),
    lookahead.selects?(:field2),
    lookahead.selects?(:field_2),
    lookahead.selects?(:field_2),
    lookahead.selects?(:field_2),
    lookahead.selects?(:field3),
    lookahead.selects?(:field_3),
    lookahead.selects?("__field_4"),
  ]
end

pp res
report.pretty_print

And found the following changes (summarized):

-Total allocated: 37960 bytes (253 objects)
+Total allocated: 37600 bytes (244 objects)
 Total retained:  26752 bytes (139 objects)

 allocated memory by gem
 -----------------------------------
-     37816  graphql-ruby/lib
+     37456  graphql-ruby/lib
        144  other

 allocated objects by gem
 -----------------------------------
-       251  graphql-ruby/lib
+       242  graphql-ruby/lib
          2  other
          
 allocated objects by class
 -----------------------------------
-        66  Array
-        46  String
+        62  Array
         44  GraphQL::Schema::Field
+        41  String
         33  GraphQL::Schema::NonNull
         30  Hash
         16  GraphQL::Schema::List
        
Allocated String Report
 -----------------------------------
+         8  "Field"
+         8  /Users/rmosolgo/code/graphql-ruby/lib/graphql/schema/member/build_type.rb:123
+
          6  "2"
          6  /Users/rmosolgo/code/graphql-ruby/lib/graphql/schema/member/build_type.rb:123

          4  "F"
          4  /Users/rmosolgo/code/graphql-ruby/lib/graphql/schema/member/build_type.rb:124

-         4  "Field"
-         4  /Users/rmosolgo/code/graphql-ruby/lib/graphql/schema/member/build_type.rb:123
-
          4  "f"
          4  /Users/rmosolgo/code/graphql-ruby/lib/graphql/schema/member/build_type.rb:124

-         4  "field"
-         4  /Users/rmosolgo/code/graphql-ruby/lib/graphql/schema/member/build_type.rb:123
-
          3  "field2"
          3  /Users/rmosolgo/code/graphql-ruby/lib/graphql/schema/member/build_type.rb:123

-         3  "field_2"
-         3  /Users/rmosolgo/code/graphql-ruby/lib/graphql/execution/lookahead.rb:111
-

...

-         2  "field3"
-         1  /Users/rmosolgo/code/graphql-ruby/lib/graphql/execution/lookahead.rb:111
-         1  /Users/rmosolgo/code/graphql-ruby/lib/graphql/schema/member/build_type.rb:123

...

          1  "__field_4"
          1  lookahead-memory.rb:30

-         1  "field_3"
-         1  /Users/rmosolgo/code/graphql-ruby/lib/graphql/execution/lookahead.rb:111
+         1  "field3"
+         1  /Users/rmosolgo/code/graphql-ruby/lib/graphql/schema/member/build_type.rb:123

@rmosolgo
Copy link
Owner Author

😢 Apparently Symbol#name is not always there -- I'll add a fallback.

@rmosolgo rmosolgo merged commit 1473cea into master Oct 6, 2022
@rmosolgo rmosolgo added this to the 2.0.15 milestone Oct 6, 2022
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.

None yet

1 participant