Skip to content

Commit

Permalink
refactor: more startup performance improvements (#327)
Browse files Browse the repository at this point in the history
* Faster introspection

* Massively improve performance of extend
  • Loading branch information
benjie authored Oct 25, 2018
1 parent d481ab9 commit e7f956a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 47 deletions.
31 changes: 14 additions & 17 deletions packages/graphile-build-pg/src/plugins/PgIntrospectionPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,22 +238,19 @@ export default (async function PgIntrospectionPlugin(
pgIncludeExtensionResources,
]);

const result = rows.reduce(
(memo, { object }) => {
memo[object.kind].push(object);
return memo;
},
{
namespace: [],
class: [],
attribute: [],
type: [],
constraint: [],
procedure: [],
extension: [],
index: [],
}
);
const result = {
namespace: [],
class: [],
attribute: [],
type: [],
constraint: [],
procedure: [],
extension: [],
index: [],
};
for (const { object } of rows) {
result[object.kind].push(object);
}

// Parse tags from comments
[
Expand Down Expand Up @@ -289,7 +286,7 @@ export default (async function PgIntrospectionPlugin(
});

for (const k in result) {
result[k].map(Object.freeze);
result[k].forEach(Object.freeze);
}
return Object.freeze(result);
})
Expand Down
42 changes: 14 additions & 28 deletions packages/graphile-build/src/extend.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// @flow
import chalk from "chalk";

const aExtendedB = new WeakMap();
const INDENT = " ";
const $$hints = Symbol("hints");

export function indent(text: string) {
return (
Expand All @@ -15,24 +15,17 @@ export default function extend<Obj1: *, Obj2: *>(
extra: Obj2,
hint?: string
): Obj1 & Obj2 {
const keysA = Object.keys(base);
const keysB = Object.keys(extra);
const hints = Object.create(null);
for (const key of keysA) {
const hintKey = `_source__${key}`;
if (base[hintKey]) {
hints[hintKey] = base[hintKey];
}
}
// $FlowFixMe
const hints = base[$$hints] || {};

const keysB = Object.keys(extra);
const extraHints = extra[$$hints] || {};
for (const key of keysB) {
const newValue = extra[key];
const oldValue = base[key];
const hintKey = `_source__${key}`;
const hintB = extra[hintKey] || hint;
if (aExtendedB.get(newValue) !== oldValue && keysA.indexOf(key) >= 0) {
const hintB = extraHints[key] || hint;
if (key in base && base[key] !== newValue) {
// $FlowFixMe
const hintA: ?string = base[hintKey];
const hintA: ?string = hints[key];
const firstEntityDetails = !hintA
? "We don't have any information about the first entity."
: `The first entity was:\n\n${indent(chalk.magenta(hintA))}`;
Expand All @@ -45,19 +38,12 @@ export default function extend<Obj1: *, Obj2: *>(
)}'.\n\n${indent(firstEntityDetails)}\n\n${indent(secondEntityDetails)}`
);
}
hints[hintKey] = hints[hintKey] || hintB || base[hintKey];
}
const obj = Object.assign({}, base, extra);
aExtendedB.set(obj, base);
for (const hintKey in hints) {
if (hints[hintKey]) {
Object.defineProperty(obj, hintKey, {
configurable: false,
enumerable: false,
value: hints[hintKey],
writable: false,
});
if (hintB) {
hints[key] = hintB;
}
}
return obj;
return Object.assign(base, extra, {
// $FlowFixMe
[$$hints]: hints,
});
}
4 changes: 2 additions & 2 deletions packages/graphile-build/src/makeNewBuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ export default function makeNewBuild(builder: SchemaBuilder): { ...Build } {
},
scope: extend(
extend(
scope,
{ ...scope },
{
fieldName,
},
Expand Down Expand Up @@ -667,7 +667,7 @@ export default function makeNewBuild(builder: SchemaBuilder): { ...Build } {
Self,
scope: extend(
extend(
scope,
{ ...scope },
{
fieldName,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ Object {
"isRootQuery": true,
"nullTest": null,
"stringTest": "THIS_IS_A_STRING",
Symbol(hints): Object {
"fieldDirectives": "Extending scope for field 'echo' within context for GraphQLObjectType 'Query'",
"fieldName": "Within context for GraphQLObjectType 'Query'",
"floatTest": "Extending scope for field 'echo' within context for GraphQLObjectType 'Query'",
"intTest": "Extending scope for field 'echo' within context for GraphQLObjectType 'Query'",
"isEchoField": "Extending scope for field 'echo' within context for GraphQLObjectType 'Query'",
"nullTest": "Extending scope for field 'echo' within context for GraphQLObjectType 'Query'",
"stringTest": "Extending scope for field 'echo' within context for GraphQLObjectType 'Query'",
},
}
`;

Expand Down Expand Up @@ -159,6 +168,16 @@ Object {
"isRootQuery": true,
"nullTest": null,
"stringTest": "THIS_IS_A_STRING",
Symbol(hints): Object {
"embedTest": "Extending scope for field 'echo' within context for GraphQLObjectType 'Query'",
"fieldDirectives": "Extending scope for field 'echo' within context for GraphQLObjectType 'Query'",
"fieldName": "Within context for GraphQLObjectType 'Query'",
"floatTest": "Extending scope for field 'echo' within context for GraphQLObjectType 'Query'",
"intTest": "Extending scope for field 'echo' within context for GraphQLObjectType 'Query'",
"isEchoField": "Extending scope for field 'echo' within context for GraphQLObjectType 'Query'",
"nullTest": "Extending scope for field 'echo' within context for GraphQLObjectType 'Query'",
"stringTest": "Extending scope for field 'echo' within context for GraphQLObjectType 'Query'",
},
}
`;

Expand Down

0 comments on commit e7f956a

Please sign in to comment.