-
-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: a number of performance optimisations (#458)
* Precompute for performance * Use prepared statements * Cache context * More efficent 'beforeLocks' * Fast loop * Debug branching * Cache hOP * Marginally faster checks * Faster loops * Memo of size one * FieldNode always has a name * Make prepared statement cache configurable * graphql-parse-resolve-info turbo * One fewer closure * Upgrade pg-sql2 * Avoid ||= * Turbo entry point
- Loading branch information
Showing
20 changed files
with
337 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
node_modules | ||
node7minus | ||
node8plus | ||
build-turbo | ||
examples | ||
dist |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,32 @@ | ||
module.exports = { | ||
"plugins": ["@babel/plugin-transform-modules-commonjs", "@babel/plugin-syntax-object-rest-spread"], | ||
"presets": [ | ||
["@babel/env", { | ||
"targets": { | ||
"node": "8.6" | ||
} | ||
}], | ||
"@babel/flow" | ||
plugins: [ | ||
"@babel/plugin-transform-modules-commonjs", | ||
"@babel/plugin-syntax-object-rest-spread", | ||
], | ||
"env": { | ||
"test": { | ||
"presets": [ | ||
["@babel/env", { | ||
"targets": { | ||
"node": "current" | ||
} | ||
}], | ||
"@babel/flow" | ||
] | ||
} | ||
} | ||
} | ||
presets: [ | ||
[ | ||
"@babel/env", | ||
{ | ||
targets: { | ||
node: "8.6", | ||
}, | ||
}, | ||
], | ||
"@babel/flow", | ||
], | ||
env: { | ||
test: { | ||
presets: [ | ||
[ | ||
"@babel/env", | ||
{ | ||
targets: { | ||
node: "current", | ||
}, | ||
}, | ||
], | ||
"@babel/flow", | ||
], | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
//@flow | ||
import { createHash } from "crypto"; | ||
import LRU from "lru-cache"; | ||
import type { PoolClient } from "pg"; | ||
|
||
const POSTGRAPHILE_PREPARED_STATEMENT_CACHE_SIZE = | ||
parseInt(process.env.POSTGRAPHILE_PREPARED_STATEMENT_CACHE_SIZE, 10) || 100; | ||
|
||
let lastString: string; | ||
let lastHash: string; | ||
const hash = (str: string): string => { | ||
if (str !== lastString) { | ||
lastString = str; | ||
lastHash = createHash("sha1") | ||
.update(str) | ||
.digest("base64"); | ||
} | ||
return lastHash; | ||
}; | ||
|
||
export default function pgPrepareAndRun( | ||
pgClient: PoolClient, | ||
text: string, | ||
// eslint-disable-next-line flowtype/no-weak-types | ||
values: any | ||
) { | ||
const connection = pgClient.connection; | ||
if ( | ||
!values || | ||
POSTGRAPHILE_PREPARED_STATEMENT_CACHE_SIZE < 1 || | ||
!connection || | ||
!connection.parsedStatements | ||
) { | ||
return pgClient.query(text, values); | ||
} else { | ||
const name = hash(text); | ||
if (!connection._graphilePreparedStatementCache) { | ||
connection._graphilePreparedStatementCache = LRU({ | ||
max: POSTGRAPHILE_PREPARED_STATEMENT_CACHE_SIZE, | ||
dispose(key) { | ||
if (connection.parsedStatements[key]) { | ||
pgClient | ||
.query(`deallocate ${pgClient.escapeIdentifier(key)}`) | ||
.then(() => { | ||
delete connection.parsedStatements[key]; | ||
}) | ||
.catch(e => { | ||
// eslint-disable-next-line no-console | ||
console.error("Error releasing prepared query", e); | ||
}); | ||
} | ||
}, | ||
}); | ||
} | ||
if (!connection._graphilePreparedStatementCache.get(name)) { | ||
// We're relying on dispose to clear out the old ones. | ||
connection._graphilePreparedStatementCache.set(name, true); | ||
} | ||
return pgClient.query({ | ||
name, | ||
text, | ||
values, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.