Skip to content
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

JSX Outlining #30956

Merged
merged 1 commit into from
Oct 17, 2024
Merged

JSX Outlining #30956

merged 1 commit into from
Oct 17, 2024

Conversation

gsathya
Copy link
Member

@gsathya gsathya commented Sep 13, 2024

Currently, the react compiler can not compile within callbacks which can potentially cause over rendering. Consider this example:

function Component(countries, onDelete) {
  const name = useFoo();
  return countries.map(() => {
    return (
      <Foo>
        <Bar name={name}/>
        <Baz onclick={onDelete} />
      </Foo>
    );
  });
}

In this case, there's no memoization of the nested jsx elements. But instead if we were to manually refactor the nested jsx into separate component like this:

function Component(countries, onDelete) {
  const name = useFoo();
  return countries.map(() => {
    return <Temp name={name} onDelete={onDelete} />;
  });
}

function Temp({ name, onDelete }) {
  return (
    <Foo>
      <Bar name={name} />
      <Baz onclick={onDelete} />
    </Foo>
  );
}

The compiler can now optimise both these components:

function Component(countries, onDelete) {
  const $ = _c(4);
  const name = useFoo();
  let t0;
  if ($[0] !== name || $[1] !== onDelete || $[2] !== countries) {
    t0 = countries.map(() => <Temp name={name} onDelete={onDelete} />);
    $[0] = name;
    $[1] = onDelete;
    $[2] = countries;
    $[3] = t0;
  } else {
    t0 = $[3];
  }
  return t0;
}

function Temp(t0) {
  const $ = _c(7);
  const { name, onDelete } = t0;
  let t1;
  if ($[0] !== name) {
    t1 = <Bar name={name} />;
    $[0] = name;
    $[1] = t1;
  } else {
    t1 = $[1];
  }
  let t2;
  if ($[2] !== onDelete) {
    t2 = <Baz onclick={onDelete} />;
    $[2] = onDelete;
    $[3] = t2;
  } else {
    t2 = $[3];
  }
  let t3;
  if ($[4] !== t1 || $[5] !== t2) {
    t3 = (
      <Foo>
        {t1}
        {t2}
      </Foo>
    );
    $[4] = t1;
    $[5] = t2;
    $[6] = t3;
  } else {
    t3 = $[6];
  }
  return t3;
}

Now, when countries is updated by adding one single value, only the newly added value is re-rendered and not the entire list. Rather than having to do this manually, this PR teaches the react compiler to do this transformation.

This PR adds a new pass (OutlineJsx) to capture nested jsx statements and outline them in a separate component. This newly outlined component can then by memoized by the compiler, giving us more fine grained rendering.

There's a lot of improvements we can do in the future:

  • For now, we only outline nested jsx inside callbacks, but this can be extended in the future (for ex, to outline nested jsx inside loops).
function Component(arr) {
  const jsx = [];
  for (const i of arr) {
    jsx.push(
      // this nested jsx can be outlined
      <Bar>
        <Baz i={i}></Baz>
      </Bar>,
    );
  }
  return jsx;
}
  • Only the JSX expression statements are outlined, none of the other statements that flow into the jsx are outlined. This is a bit tricky as we must only outline non-mutating statements using our effects analysis.
function Component(arr) {
  return arr.map((i) => {
    // this statement should not be outlined
    const y = mutate(i);
    // this statement can be outlined
    const x = { i };
    return (
      <Bar>
        <Baz x={x} y={y}></Baz>
      </Bar>
    );
  });
}

Copy link

vercel bot commented Sep 13, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
react-compiler-playground ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 17, 2024 5:13pm

@josephsavona
Copy link
Contributor

Awesome! Looking forward to reading through this. Quick question:

This PR adds a new HIR node (StartJsx) to track when the jsx statement starts, which lets us start capturing nested jsx expressions.

Why do we need this as an instruction, vs doing a quick scan through the instructions to find the outermost JSX instruction?

@gsathya
Copy link
Member Author

gsathya commented Oct 7, 2024

Why do we need this as an instruction, vs doing a quick scan through the instructions to find the outermost JSX instruction?

Yeah, this is a good question! I initially started with the scan approach but then hit a roadblock for this case:

// @enableJsxOutlining
function T({ t, a, b }) {
  return t.map(() => {
    let x = <Foo a={a} />
    let y = <Baz b={b} />
    // ... potential shenanigans ...
    return <Bar>{y}{x}</Bar>
  });
}

function B({t, a, b }) {
  return t.map(() => <Bar><Foo a={com(a)} /><Baz b={b} /></Bar>)
}

Here we want to differentiate between the two cases and not outline the first example. The jsx expressions, in either case, aren't in a consecutive sequence to make the quick scan work. For the second case, the children are read as jsx expressions and not from a local. This is potentially a way to differentiate but doesn't seem future proof as lowering to a local shouldn't have any semantic change, so I can see us doing this as part of some future change. I ended up going with the StartJsx (similar to StartMemoize) to be sure that we're inside a jsx expression.

@josephsavona
Copy link
Contributor

josephsavona commented Oct 7, 2024

Hmm, it seems pretty reasonable to just use a scan and exclude StoreLocal, which would reject the first example and allow the second. We could always expand to allow StoreLocal if we determined that it was only used once, in the subsequent JSX code. This could be a backwards traversal with a narrow allowlist of instructions like jsx, primitives, globals.

It's really really nice to avoid adding instruction variants.

Comment on lines 33 to 35
options: {eliminateStartJsx: boolean} = {
eliminateStartJsx: false,
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is why it's so nice to avoid adding instructions :-)

Comment on lines +495 to +497
queue.push({
kind: 'outlined',
fn,
fnType: outlined.type,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yay i'm so glad this worked!

@gsathya gsathya force-pushed the outline-jsx-final branch 2 times, most recently from fd7ca51 to 5192ae9 Compare October 10, 2024 15:01
@gsathya
Copy link
Member Author

gsathya commented Oct 10, 2024

This could be a backwards traversal with a narrow allowlist of instructions like jsx, primitives, globals.

Oh good call! I've updated the PR to do this instead

Copy link
Contributor

@josephsavona josephsavona left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really exciting! Seems pretty close, see comments about duplicate attribute names

Copy link
Contributor

@josephsavona josephsavona left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requesting changes for handling intermediate instructions and duplicate attribute names

Copy link
Contributor

@josephsavona josephsavona left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

Adds a new pass to outline nested jsx to a separate function.

For now, we only outline nested jsx inside callbacks, but this can be
extended in the future (for ex, to outline nested jsx inside loops).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed React Core Team Opened by a member of the React Core Team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants