Skip to content

🎥 Make videos programmatically with React

`@remotion/studio-server`: Speed up batched sequence prop subscriptions (#10584)

Jonny Burgere38a5d2f2450
diff --git a/packages/studio-server/src/preview-server/routes/can-update-sequence-props.ts b/packages/studio-server/src/preview-server/routes/can-update-sequence-props.ts
index b46914e5ba..8d6432148a 100644
--- a/packages/studio-server/src/preview-server/routes/can-update-sequence-props.ts
+++ b/packages/studio-server/src/preview-server/routes/can-update-sequence-props.ts
@@ -872,19 +872,12 @@ const getFrameDisplayOffsetAdjustmentBetweenPaths = ({
};
const getDefaultFrameDisplayOffsetAdjustment = ({
- jsxElement,
- ast,
+ jsxPath,
videoConfigValues,
}: {
- jsxElement: JSXOpeningElement;
- ast: File;
+ jsxPath: recast.types.NodePath;
videoConfigValues: VideoConfigIdentifierValues;
}): number | null => {
- const jsxPath = findNodePath(ast, jsxElement);
- if (!jsxPath) {
- return null;
- }
-
let functionPath: recast.types.NodePath | null = jsxPath.parentPath;
while (functionPath) {
const node = functionPath.value as Node;
@@ -1110,20 +1103,22 @@ export const findJsxElementAtNodePath = (
return current ? (current.value as unknown as JSXOpeningElement) : null;
};
+const getJsxElementNodeFromJsxPath = (
+ jsxPath: recast.types.NodePath,
+): JSXElement | null => {
+ if (recast.types.namedTypes.JSXElement.check(jsxPath.parentPath?.value)) {
+ return jsxPath.parentPath.value as unknown as JSXElement;
+ }
+
+ return null;
+};
+
export const findJsxElementNodeAtNodePath = (
ast: File,
nodePath: SequenceNodePath,
): JSXElement | null => {
const current = findJsxElementPathAtNodePath(ast, nodePath);
- if (!current) {
- return null;
- }
-
- if (recast.types.namedTypes.JSXElement.check(current.parentPath?.value)) {
- return current.parentPath.value as unknown as JSXElement;
- }
-
- return null;
+ return current ? getJsxElementNodeFromJsxPath(current) : null;
};
export type StaticJsxTextContent =
@@ -1726,10 +1721,11 @@ const computeSequencePropsStatusFromAstAndIdentifiers = ({
effects: string[][];
videoConfigIdentifierValues: VideoConfigIdentifierValues;
}): CanUpdateSequencePropsResponseTrue => {
- const jsxElementNode = findJsxElementNodeAtNodePath(ast, nodePath);
+ const jsxPath = findJsxElementPathAtNodePath(ast, nodePath);
+ const jsxElementNode = jsxPath ? getJsxElementNodeFromJsxPath(jsxPath) : null;
const jsxElement = jsxElementNode?.openingElement ?? null;
- if (!jsxElement || !jsxElementNode) {
+ if (!jsxPath || !jsxElement || !jsxElementNode) {
throw new JsxElementNotFoundAtLocationError();
}
@@ -1758,8 +1754,7 @@ const computeSequencePropsStatusFromAstAndIdentifiers = ({
});
const defaultKeyframeDisplayOffsetAdjustment =
getDefaultFrameDisplayOffsetAdjustment({
- jsxElement,
- ast,
+ jsxPath,
videoConfigValues: videoConfigIdentifierValues,
});
const addDefaultKeyframeDisplayOffsetAdjustment = (
diff --git a/packages/studio-server/src/test/subscribe-to-sequence-props.test.ts b/packages/studio-server/src/test/subscribe-to-sequence-props.test.ts
index 2cc5298afd..e374e7271e 100644
--- a/packages/studio-server/src/test/subscribe-to-sequence-props.test.ts
+++ b/packages/studio-server/src/test/subscribe-to-sequence-props.test.ts
@@ -129,3 +129,139 @@ export const Second = () => {
rmSync(remotionRoot, {recursive: true, force: true});
}
});
+
+// Mirrors the payload that Remotion Studio sends when mounting a composition
+// with many interactive elements: one batched request per rendered element.
+test('handles a batch of 1000 subscriptions from a large file quickly', async () => {
+ const spanCount = 1000;
+ const spans = Array.from(
+ {length: spanCount},
+ (_, i) =>
+ `\t\t\t<Interactive.Span name="Line ${i + 1}">Line ${i + 1}</Interactive.Span>`,
+ ).join('\n');
+ const input = `import {Interactive} from 'remotion';
+
+const InteractiveDivStressTest: React.FC = () => {
+ return (
+ <Interactive.Div style={{backgroundColor: 'white', overflow: 'auto'}}>
+${spans}
+ </Interactive.Div>
+ );
+};
+
+export default InteractiveDivStressTest;
+`;
+ const cleanupFileWatcher = setFileWatcherRegistry(
+ createFileWatcherRegistry(),
+ );
+ const remotionRoot = mkdtempSync(join(tmpdir(), 'remotion-subscriptions-'));
+ const uniqueSuffix = basename(remotionRoot);
+ const fileName = `StressTest-${uniqueSuffix}.tsx`;
+ const clientId = `subscription-stress-test-${uniqueSuffix}`;
+ writeFileSync(join(remotionRoot, fileName), input);
+ // The keys the Studio timeline subscribes to for Interactive.Span
+ const keys = [
+ 'name',
+ 'durationInFrames',
+ 'from',
+ 'trimBefore',
+ 'freeze',
+ 'hidden',
+ 'showInTimeline',
+ 'style.transformOrigin',
+ 'style.translate',
+ 'style.scale',
+ 'style.rotate',
+ 'style.opacity',
+ 'cropLeft',
+ 'cropRight',
+ 'cropTop',
+ 'cropBottom',
+ 'style.backgroundColor',
+ 'style.borderWidth',
+ 'style.borderStyle',
+ 'style.borderColor',
+ 'style.borderRadius',
+ 'style.borderTopLeftRadius',
+ 'style.borderTopRightRadius',
+ 'style.borderBottomRightRadius',
+ 'style.borderBottomLeftRadius',
+ 'style.color',
+ 'style.fontFamily',
+ 'style.fontSize',
+ 'style.lineHeight',
+ 'style.fontWeight',
+ 'style.fontStyle',
+ 'style.textAlign',
+ 'style.letterSpacing',
+ 'children',
+ ];
+ const requests: SubscribeToSequencePropsRequest[] = Array.from(
+ {length: spanCount},
+ (_, i) => ({
+ fileName,
+ // Spans start at line 6 of the generated file
+ line: 6 + i,
+ column: 3,
+ nodePath: null,
+ componentIdentity: 'dev.remotion.remotion.Interactive.Span',
+ keys,
+ assetKeys: [],
+ effects: [],
+ clientId,
+ videoConfigValues: {
+ durationInFrames: 120,
+ fps: 30,
+ height: 1080,
+ width: 1080,
+ },
+ }),
+ );
+
+ try {
+ const start = performance.now();
+ const response = await subscribeToSequenceProps({
+ binariesDirectory: null,
+ configFile: null,
+ entryPoint: join(remotionRoot, fileName),
+ getDefaultCodingAgent: () => null,
+ getDefaultEditor: () => null,
+ input: {...requests[0], requests},
+ logLevel: 'error',
+ methods: {
+ addJob: () => undefined,
+ cancelJob: () => undefined,
+ removeJob: () => undefined,
+ },
+ publicDir: remotionRoot,
+ remotionRoot,
+ request: {} as never,
+ response: {} as never,
+ });
+ const durationMs = performance.now() - start;
+
+ expect(response.results).toHaveLength(spanCount);
+ for (const [index, result] of response.results.entries()) {
+ if (!result.success) {
+ throw new Error(`Subscription ${index} failed`);
+ }
+
+ expect(result.status.props.name).toEqual({
+ status: 'static',
+ keyframeDisplayOffsetAdjustment: null,
+ codeValue: `Line ${index + 1}`,
+ });
+ expect(result.status.props.children).toEqual({
+ status: 'static',
+ keyframeDisplayOffsetAdjustment: null,
+ codeValue: `Line ${index + 1}`,
+ });
+ }
+
+ expect(durationMs).toBeLessThan(3000);
+ } finally {
+ unsubscribeClientSequencePropsWatchers(clientId);
+ cleanupFileWatcher();
+ rmSync(remotionRoot, {recursive: true, force: true});
+ }
+});