Skip to content

Commit

Permalink
Render loop optimisations (#2722)
Browse files Browse the repository at this point in the history
* Unrolling renderloop

* Improving render loop performance
  • Loading branch information
mattgperry committed Jul 9, 2024
1 parent e83d425 commit 20a64f5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
4 changes: 2 additions & 2 deletions packages/framer-motion/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@
"bundlesize": [
{
"path": "./dist/size-rollup-motion.js",
"maxSize": "33.4 kB"
"maxSize": "33.45 kB"
},
{
"path": "./dist/size-rollup-m.js",
"maxSize": "5.8 kB"
"maxSize": "5.85 kB"
},
{
"path": "./dist/size-rollup-dom-animation.js",
Expand Down
26 changes: 19 additions & 7 deletions packages/framer-motion/src/frameloop/batcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ export function createRenderBatcher(
isProcessing: false,
}

const flagRunNextFrame = () => (runNextFrame = true)

const steps = stepsOrder.reduce((acc, key) => {
acc[key] = createRenderStep(() => (runNextFrame = true))
acc[key] = createRenderStep(flagRunNextFrame)
return acc
}, {} as Steps)

const processStep = (stepId: StepId) => {
steps[stepId].process(state)
}
const { read, resolveKeyframes, update, preRender, render, postRender } =
steps

const processBatch = () => {
const timestamp = MotionGlobalConfig.useManualTiming
Expand All @@ -47,7 +48,15 @@ export function createRenderBatcher(

state.timestamp = timestamp
state.isProcessing = true
stepsOrder.forEach(processStep)

// Unrolled render loop for better per-frame performance
read.process(state)
resolveKeyframes.process(state)
update.process(state)
preRender.process(state)
render.process(state)
postRender.process(state)

state.isProcessing = false

if (runNextFrame && allowKeepAlive) {
Expand Down Expand Up @@ -75,8 +84,11 @@ export function createRenderBatcher(
return acc
}, {} as Batcher)

const cancel = (process: Process) =>
stepsOrder.forEach((key) => steps[key].cancel(process))
const cancel = (process: Process) => {
for (let i = 0; i < stepsOrder.length; i++) {
steps[stepsOrder[i]].cancel(process)
}
}

return { schedule, cancel, state, steps }
}

0 comments on commit 20a64f5

Please sign in to comment.