-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
feat(gatsby): Optimize creating many child nodes from one parent #35504
Open
KyleAMathews
wants to merge
21
commits into
master
Choose a base branch
from
faster-transformer-json
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 20 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
7a45d61
feat(gatsby-transformer-json): Speed up creating nodes for arrays
KyleAMathews f85f07b
100 is about 33% faster than 1000
KyleAMathews c409697
Add type cache
KyleAMathews a761e9c
Merge branch 'master' into faster-transformer-json
KyleAMathews 880951e
Fix creating IDs for nodes
KyleAMathews 7ceceea
Debounce writing updated parent node
KyleAMathews cbfe597
Invoke on leading and trailing to ensure the parent node's children a…
KyleAMathews 74e12dc
Fix key for debounce fn
KyleAMathews 6fa9892
batch actions instead of timeout on writing
KyleAMathews 27ff437
Keep old behavior for tests
KyleAMathews 81e96b6
Setting batch count of 1 seems to work
KyleAMathews bc76325
Merge branch 'master' into faster-transformer-json
KyleAMathews cc04b88
Merge branch 'master' into faster-transformer-json
KyleAMathews ce78af4
Merge branch 'master' into faster-transformer-json
KyleAMathews 62f56da
Merge branch 'master' into faster-transformer-json
KyleAMathews e579e74
Merge branch 'master' into faster-transformer-json
KyleAMathews 80b449f
Merge branch 'master' into faster-transformer-json
LekoArts f2dd1ff
fix csv transformer
LekoArts f8dcb3c
remove unnecessary await
LekoArts 9485ac0
correct typescript
LekoArts 8e9b755
fix csv tests
LekoArts File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
49 changes: 49 additions & 0 deletions
49
packages/gatsby/src/redux/actions/create-parent-child-link.ts
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,49 @@ | ||
import type { IGatsbyNode, IAddChildNodeToParentNodeAction } from "../types" | ||
import Batcher from "../../utils/batcher" | ||
import { store } from "../" | ||
import { getNode } from "../../datastore" | ||
|
||
const isTestEnv = process.env.NODE_ENV === `test` | ||
const batchCount = isTestEnv ? 1 : 1000 | ||
|
||
type CreateParentChildLinkFn = ( | ||
payload: { | ||
parent: IGatsbyNode | ||
child: IGatsbyNode | ||
}, | ||
plugin?: string | ||
) => IAddChildNodeToParentNodeAction | ||
|
||
export const createParentChildLinkBatcher = | ||
new Batcher<CreateParentChildLinkFn>(batchCount) | ||
|
||
createParentChildLinkBatcher.bulkCall(createCalls => { | ||
const nodesMap = new Map() | ||
|
||
// Add children to parent node(s) and dispatch. | ||
createCalls.forEach(call => { | ||
const { parent, child } = call[0] | ||
const parentId = parent.id | ||
|
||
let parentNode | ||
if (nodesMap.has(parentId)) { | ||
parentNode = nodesMap.get(parentId) | ||
} else { | ||
parentNode = getNode(parentId) | ||
|
||
if (!parentNode.children.includes(child.id)) { | ||
parentNode.children.push(child.id) | ||
} | ||
|
||
nodesMap.set(parentId, parentNode) | ||
} | ||
}) | ||
|
||
nodesMap.forEach(parentNode => { | ||
const payload: IAddChildNodeToParentNodeAction = { | ||
type: `ADD_CHILD_NODE_TO_PARENT_NODE`, | ||
payload: parentNode, | ||
} | ||
store.dispatch(payload) | ||
}) | ||
}) |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we force
flush
batcher just aftersourceNodes
lifecycle and while this is where we expect most ofcreateParentChildLink
calls to happen they will not always be just there (for example editing files while dev server is running will randomly updateFile
nodes + runonCreateNode
for them outside ofsourceNodes
). This makes it possible to not create actual link until batch grow large enough to be processed or wesourceNodes
is triggered for different reason.Is it possible to only do batching while during
sourceNodes
and use previous setup for anything else?