Skip to content

Commit

Permalink
feat(gatsby): cache/memoize hot functions in runAPI (#29240)
Browse files Browse the repository at this point in the history
* feat(gatsby): cache/memoize hot functions in runAPI

* Add dependency
  • Loading branch information
KyleAMathews authored Jan 28, 2021
1 parent 552afa9 commit e1c899f
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/gatsby/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
"lodash": "^4.17.20",
"md5-file": "^5.0.0",
"meant": "^1.0.1",
"memoizee": "^0.4.15",
"micromatch": "^4.0.2",
"mime": "^2.4.6",
"mini-css-extract-plugin": "^0.11.2",
Expand Down
28 changes: 22 additions & 6 deletions packages/gatsby/src/utils/api-runner-node.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const Promise = require(`bluebird`)
const _ = require(`lodash`)
const chalk = require(`chalk`)
const { bindActionCreators } = require(`redux`)
const { bindActionCreators: origBindActionCreators } = require(`redux`)
const memoize = require(`memoizee`)

const bindActionCreators = memoize(origBindActionCreators)

const tracer = require(`opentracing`).globalTracer()
const reporter = require(`gatsby-cli/lib/reporter`)
Expand Down Expand Up @@ -296,6 +299,8 @@ const getUninitializedCache = plugin => {

const pluginNodeCache = new Map()

const availableActionsCache = new Map()
let publicPath
const runAPI = async (plugin, api, args, activity) => {
let gatsbyNode = pluginNodeCache.get(plugin.name)
if (!gatsbyNode) {
Expand All @@ -310,15 +315,23 @@ const runAPI = async (plugin, api, args, activity) => {

pluginSpan.setTag(`api`, api)
pluginSpan.setTag(`plugin`, plugin.name)

const {
publicActions,
restrictedActionsAvailableInAPI,
} = require(`../redux/actions`)
const availableActions = {
...publicActions,
...(restrictedActionsAvailableInAPI[api] || {}),

let availableActions
if (availableActionsCache.has(api)) {
availableActions = availableActionsCache.get(api)
} else {
availableActions = {
...publicActions,
...(restrictedActionsAvailableInAPI[api] || {}),
}

availableActionsCache.set(api, availableActions)
}

let boundActionCreators = bindActionCreators(
availableActions,
store.dispatch
Expand All @@ -338,7 +351,10 @@ const runAPI = async (plugin, api, args, activity) => {
const { config, program } = store.getState()

const pathPrefix = (program.prefixPaths && config.pathPrefix) || ``
const publicPath = getPublicPath({ ...config, ...program }, ``)

if (typeof publicPath === `undefined`) {
publicPath = getPublicPath({ ...config, ...program }, ``)
}

const namespacedCreateNodeId = id => createNodeId(id, plugin.name)

Expand Down
61 changes: 59 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9962,7 +9962,7 @@ es-to-primitive@^1.2.1:
is-date-object "^1.0.1"
is-symbol "^1.0.2"

es5-ext@^0.10.14, es5-ext@^0.10.35, es5-ext@^0.10.50:
es5-ext@^0.10.14, es5-ext@^0.10.35, es5-ext@^0.10.46, es5-ext@^0.10.50, es5-ext@^0.10.53, es5-ext@~0.10.14, es5-ext@~0.10.2, es5-ext@~0.10.46:
version "0.10.53"
resolved "https://registry.yarnpkg.com/es5-ext/-/es5-ext-0.10.53.tgz#93c5a3acfdbef275220ad72644ad02ee18368de1"
integrity sha512-Xs2Stw6NiNHWypzRTY1MtaG/uJlwCk8kH81920ma8mvN8Xq1gsfhZvpkImLQArw8AHnv8MT2I45J3c0R8slE+Q==
Expand All @@ -9971,7 +9971,7 @@ es5-ext@^0.10.14, es5-ext@^0.10.35, es5-ext@^0.10.50:
es6-symbol "~3.1.3"
next-tick "~1.0.0"

es6-iterator@^2.0.1, es6-iterator@~2.0.3:
es6-iterator@^2.0.1, es6-iterator@^2.0.3, es6-iterator@~2.0.3:
version "2.0.3"
resolved "https://registry.yarnpkg.com/es6-iterator/-/es6-iterator-2.0.3.tgz#a7de889141a05a94b0854403b2d0a0fbfa98f3b7"
dependencies:
Expand Down Expand Up @@ -10011,6 +10011,16 @@ es6-weak-map@^2.0.1, es6-weak-map@^2.0.2:
es6-iterator "^2.0.1"
es6-symbol "^3.1.1"

es6-weak-map@^2.0.3:
version "2.0.3"
resolved "https://registry.yarnpkg.com/es6-weak-map/-/es6-weak-map-2.0.3.tgz#b6da1f16cc2cc0d9be43e6bdbfc5e7dfcdf31d53"
integrity sha512-p5um32HOTO1kP+w7PRnB+5lQ43Z6muuMuIMffvDN8ZB4GcnjLBV6zGStpbASIMk4DCAvEaamhe2zhyCb/QXXsA==
dependencies:
d "1"
es5-ext "^0.10.46"
es6-iterator "^2.0.3"
es6-symbol "^3.1.1"

esanimate@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/esanimate/-/esanimate-1.1.0.tgz#e71712b6d8b2bb2e6d5d40a6392a7bf9c7c20857"
Expand Down Expand Up @@ -10507,6 +10517,14 @@ eval@^0.1.0, eval@^0.1.4:
dependencies:
require-like ">= 0.1.1"

event-emitter@^0.3.5:
version "0.3.5"
resolved "https://registry.yarnpkg.com/event-emitter/-/event-emitter-0.3.5.tgz#df8c69eef1647923c7157b9ce83840610b02cc39"
integrity sha1-34xp7vFkeSPHFXuc6DhAYQsCzDk=
dependencies:
d "1"
es5-ext "~0.10.14"

event-source-polyfill@^1.0.15:
version "1.0.15"
resolved "https://registry.yarnpkg.com/event-source-polyfill/-/event-source-polyfill-1.0.15.tgz#a28e116281be677af4b055b67d95517e35c92435"
Expand Down Expand Up @@ -14206,6 +14224,11 @@ is-promise@^2.1.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/is-promise/-/is-promise-2.1.0.tgz#79a2a9ece7f096e80f36d2b2f3bc16c1ff4bf3fa"

is-promise@^2.2.2:
version "2.2.2"
resolved "https://registry.yarnpkg.com/is-promise/-/is-promise-2.2.2.tgz#39ab959ccbf9a774cf079f7b40c7a26f763135f1"
integrity sha512-+lP4/6lKUBfQjZ2pdxThZvLUAafmZb8OAxFb8XXtiQmS35INgr85hdOGoEs124ez1FCnZJt6jau/T+alh58QFQ==

is-redirect@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/is-redirect/-/is-redirect-1.0.0.tgz#1d03dded53bd8db0f30c26e4f95d36fc7c87dc24"
Expand Down Expand Up @@ -16413,6 +16436,13 @@ lru-cache@^6.0.0:
dependencies:
yallist "^4.0.0"

lru-queue@^0.1.0:
version "0.1.0"
resolved "https://registry.yarnpkg.com/lru-queue/-/lru-queue-0.1.0.tgz#2738bd9f0d3cf4f84490c5736c48699ac632cda3"
integrity sha1-Jzi9nw089PhEkMVzbEhpmsYyzaM=
dependencies:
es5-ext "~0.10.2"

ltgt@^2.1.2:
version "2.2.1"
resolved "https://registry.yarnpkg.com/ltgt/-/ltgt-2.2.1.tgz#f35ca91c493f7b73da0e07495304f17b31f87ee5"
Expand Down Expand Up @@ -16837,6 +16867,20 @@ memfs-or-file-map-to-github-branch@^1.1.0:
resolved "https://registry.yarnpkg.com/memfs-or-file-map-to-github-branch/-/memfs-or-file-map-to-github-branch-1.1.2.tgz#9d46c02481b7eca8e5ee8a94f170b7e0138cad67"
integrity sha512-D2JKK2DTuVYQqquBWco3K6UfSVyVwmd58dgNqh+TgxHOZdTmR8I130gjMbVCkemDl/EzqDA62417cJxKL3/FFA==

memoizee@^0.4.15:
version "0.4.15"
resolved "https://registry.yarnpkg.com/memoizee/-/memoizee-0.4.15.tgz#e6f3d2da863f318d02225391829a6c5956555b72"
integrity sha512-UBWmJpLZd5STPm7PMUlOw/TSy972M+z8gcyQ5veOnSDRREz/0bmpyTfKt3/51DhEBqCZQn1udM/5flcSPYhkdQ==
dependencies:
d "^1.0.1"
es5-ext "^0.10.53"
es6-weak-map "^2.0.3"
event-emitter "^0.3.5"
is-promise "^2.2.2"
lru-queue "^0.1.0"
next-tick "^1.1.0"
timers-ext "^0.1.7"

memoizesync@1.1.1, memoizesync@^1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/memoizesync/-/memoizesync-1.1.1.tgz#01c09f0e2cf20a6349163eab05e51f9bd1e13fe1"
Expand Down Expand Up @@ -17513,6 +17557,11 @@ netlify-identity-widget@^1.9.1:
resolved "https://registry.yarnpkg.com/netlify-identity-widget/-/netlify-identity-widget-1.9.1.tgz#9e716c4b92b9f0cc041074eb86fc962f35295b46"
integrity sha512-9oIWjwUSdRk3SkREcZNjZaVuDDx9T/wSIXZNQsQeY4qoXic/FiXVEGgu2RU3IuA4OI3L2652xY1o+PpS03Ugaw==

next-tick@1, next-tick@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/next-tick/-/next-tick-1.1.0.tgz#1836ee30ad56d67ef281b22bd199f709449b35eb"
integrity sha512-CXdUiJembsNjuToQvxayPZF9Vqht7hewsvy2sOWafLvi2awflj9mOC6bHIg50orX8IJvWKY9wYQ/zB2kogPslQ==

next-tick@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/next-tick/-/next-tick-1.0.0.tgz#ca86d1fe8828169b0120208e3dc8424b9db8342c"
Expand Down Expand Up @@ -24517,6 +24566,14 @@ timers-browserify@^2.0.4:
dependencies:
setimmediate "^1.0.4"

timers-ext@^0.1.7:
version "0.1.7"
resolved "https://registry.yarnpkg.com/timers-ext/-/timers-ext-0.1.7.tgz#6f57ad8578e07a3fb9f91d9387d65647555e25c6"
integrity sha512-b85NUNzTSdodShTIbky6ZF02e8STtVVfD+fu4aXXShEELpozH+bCpJLYMPZbsABN2wDH7fJpqIoXxJpzbf0NqQ==
dependencies:
es5-ext "~0.10.46"
next-tick "1"

timm@^1.6.1:
version "1.6.2"
resolved "https://registry.yarnpkg.com/timm/-/timm-1.6.2.tgz#dfd8c6719f7ba1fcfc6295a32670a1c6d166c0bd"
Expand Down

0 comments on commit e1c899f

Please sign in to comment.