Skip to content

Commit

Permalink
Increase print/visit performance (#4312)
Browse files Browse the repository at this point in the history
This replaces our expensive method that changes the underlying V8 shape
multiple times with a loop that preserves the identity as much as
possible.

```
⏱   Print kitchen sink document
  1 tests completed.
  2 tests completed.

  HEAD x 9,290 ops/sec ±0.21% x 1.51 KB/op (24 runs sampled)
  BASE x 2,645 ops/sec ±0.18% x 2.18 KB/op (11 runs sampled)
```

---------

Co-authored-by: Benjie <[email protected]>
  • Loading branch information
JoviDeCroock and benjie authored Jan 29, 2025
1 parent 90ebe5a commit 31bf28f
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 4 deletions.
5 changes: 5 additions & 0 deletions benchmark/fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ exports.bigSchemaSDL = fs.readFileSync(
'utf8',
);

exports.bigDocumentSDL = fs.readFileSync(
path.join(__dirname, 'kitchen-sink.graphql'),
'utf8',
);

exports.bigSchemaIntrospectionResult = JSON.parse(
fs.readFileSync(path.join(__dirname, 'github-schema.json'), 'utf8'),
);
65 changes: 65 additions & 0 deletions benchmark/kitchen-sink.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
query queryName($foo: ComplexType, $site: Site = MOBILE) @onQuery {
whoever123is: node(id: [123, 456]) {
id
... on User @onInlineFragment {
field2 {
id
alias: field1(first: 10, after: $foo) @include(if: $foo) {
id
...frag @onFragmentSpread
}
}
}
... @skip(unless: $foo) {
id
}
... {
id
}
}
}

mutation likeStory @onMutation {
like(story: 123) @onField {
story {
id @onField
}
}
}

subscription StoryLikeSubscription(
$input: StoryLikeSubscribeInput @onVariableDefinition
) @onSubscription {
storyLikeSubscribe(input: $input) {
story {
likers {
count
}
likeSentence {
text
}
}
}
}

fragment frag on Friend @onFragmentDefinition {
foo(
size: $size
bar: $b
obj: {
key: "value"
block: """
block string uses \"""
"""
}
)
}
{
unnamed(truthy: true, falsy: false, nullish: null)
query
}
query {
__typename
}
16 changes: 16 additions & 0 deletions benchmark/printer-benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

const { parse } = require('graphql/language/parser.js');
const { print } = require('graphql/language/printer.js');

const { bigDocumentSDL } = require('./fixtures.js');

const document = parse(bigDocumentSDL);

module.exports = {
name: 'Print kitchen sink document',
count: 1000,
measure() {
print(document);
},
};
5 changes: 1 addition & 4 deletions src/language/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,7 @@ export function visit(
}
}
} else {
node = Object.defineProperties(
{},
Object.getOwnPropertyDescriptors(node),
);
node = { ...node };
for (const [editKey, editValue] of edits) {
node[editKey] = editValue;
}
Expand Down

0 comments on commit 31bf28f

Please sign in to comment.