Causal is a cloud-based spreadsheet that lets you build and collaborate on complex financial models and data-heavy projects. Sometimes, Causal models get huge, which creates many challenges in keeping them fast â on both the backend and frontend.
One key challenge is UI interactions. As you can see in the video below, when we open the categories tab and try filling in a row, the UIÂ gets quite laggy.
It might be hard to see when the app is laggy in this video. To make it easier, keep an eye on the âFrame Rateâ popup in the top left corner â when itâs red or yellow, the main thread is frozen. (By the way, this is a built-in Chrome DevTools feature! You could enable it in DevTools â More Tools â Rendering â Frame Rendering Stats.
Hereâs how Causal managed to speed up this interaction by almost 4Ă â with nearly every optimization changing just a few lines.
Profiling The Interaction
To optimize the interaction, we need to figure out what makes it slow. Our go-to tool for that is Chrome DevTools:
Thereâs a lot of stuff in the recording, so it might be confusing if youâre seeing it for the first time. But thatâs okay! What we need to pay attention to is just two areas:
So whatâs going on here? If you go through the recording and click through a bunch of rectangles, youâll notice some patterns:â
âThere are a lot of React renders. Specifically, every rectangle called <span class="inline-code">performSyncWorkOnRoot</span> is (roughly) a function that starts a React render cycle. And there are lots of them (precisely, 1325).
âMost of these React renders are caused by AG Grid. AG Grid is a library that Causal uses to render tables/grids:
â
If you find any <span class="inline-code">performSyncWorkOnRoot</span> rectangle and then scroll up, youâll see what caused that function to run (meaning, caused that React render). Most of the time, that will be some AG Grid code:
âSome of the code runs several times. E.g., at the beginning of the recording, we call <span class="inline-code">GridApi.refreshServerSide</span> and <span class="inline-code">GridApi.refreshCells</span> two times in a row:
Later, some code seems to call <span class="inline-code">getRows</span> over and over and over again:
This is good! When we have some code that runs ten times in a row, we can improve a single run â and get a 10Ă improvement. And if some of these runs end up being unnecessary, weâll be able to remove them altogether.
Letâs dive in.
AG Grid: Fixing An Extra Render
Across the recording, four chunks of JavaScript start with <span class="inline-code">GridApi.refreshServerSide</span>
Down the flame chart, these chunks of JavaScript cause many React rerenders. To figure out which components are being rendered, letâs scroll down and hunt for component names. (This works because to render a component, React calls it  â or its <span class="inline-code">.render()</span> method if itâs a class component.)
Why not use React Profiler? Another way to see which components are rendered is to record a trace in React Profiler. However, when you have a lot of rerenders, matching that trace with the DevTools Performance trace is hard â and if you make a mistake, youâll end up optimizing the wrong rerenders.
If you click through the component names in the recording, youâll realize every component is <span class="inline-code">RowContainerComp</span>. This is a component from AG Grid:
Why do these components render? To answer that, letâs switch to React Profiler and find these components there:
Why use React Profiler this time? This time, we are using React Profiler. Weâve learned the component names, so we donât need to match the trace with the DevTools performance pane anymore. A couple of other great ways to learn why a component rerenders are why-did-you-render and useWhyDidYouUpdate. They work great with first-party code but are harder to enable for third-party one (like AG Grid components).
As we see, <span class="inline-code">RowContainerComp</span> components rerender because their hook 2 changed. To find that hook, letâs switch from the Profiler to the Components pane â and match that hook with the source code:
â
Why wonât we just count hooks in the component? Thatâs the most obvious approach, but it rarely works. Thatâs because React skips <span class="inline-code">useContexâ </span> when counting hooks. (This is probably because <span class="inline-code">useContext</span> is implemented differently from other hooks.) Also, React doesnât keep track of custom hooks. Instead, it counts every built-in hook (except <span class="inline-code">useContext</span>) inside. For example, if a component calls useSelector from Redux, and <span class="inline-code">useSelector</span> uses four React hooks inside, React profiler might show you "Hook 3 changed" when you should look for "custom hook 1".
Okay, so we figured out that <span class="inline-code">GridApi.refreshServerSide</span> renders a bunch of <span class="inline-code">RowContainerComp</span> components, and these components rerender because their hook 2 change.
Now, if you look through the componentâs source code, youâll notice that hook 2 is updated inside a <span class="inline-code">useEffect</span>.
And that <span class="inline-code">useEffect</span> triggers when the <span class="inline-code">rowCtrls</span> or the <span class="inline-code">domOrder</span> state changes:
This is not optimal! AG Grid is setting state from inside a <span class="inline-code">useEffect</span>. This means it schedules a new update right after another one has happened. Hereâs the entire order of events:
- When the component mounts, it exposes several functions to the AG Grid core
- Later, AG Grid calls <span class="inline-code">compProxy.setRowCtrls</span>
- <span class="inline-code">compProxy.setRowCtrls</span> updates the <span class="inline-code">RowCtrls</span> state
- Because the state changed, the component rerenders
- The <span class="inline-code">RowCtrls</span> state state got updated, so React runs <span class="inline-code">useEffect</span> state
- Inside <span class="inline-code">useEffect</span> state, React calls <span class="inline-code">setRowCtrlsOrdered</span> state and updates our hook 2
- Because the state changed, the component rerenders again đ„
As you can see, weâre rerendering the component twice just to update our hook 2! This isnât great. If AG Grid called <span class="inline-code">setRowCtrlsOrdered</span> immediately at step 2 instead of 5, weâd be able to avoid an extra render.
So why donât we make AG Grid do this? Using yarn patch, letâs patch the <span class="inline-code">@ag-grid-community/react</span> package to eliminate the extra render:
This alone cuts the number of rerenders in half â and, because <span class="inline-code">RowContainerComp</span> is rendered outside <span class="inline-code">GridApi.refreshServerSide()</span> calls too, shaves off around 15-20% of the execution time.
But weâre not done with AG Grid yet.
AG Grid: Removing The Renders
The <span class="inline-code">RowContainerComp</span> components are containers for different parts of the grid:
These components render every time we type into the editor. We just removed half of these renders. But thereâs still another half, and itâs probably unnecessary â as nothing in these components changes visually.
Whatâs causing these renders? As we learned in the previous section, <span class="inline-code">RowContainerComps</span> rerender when AG Grid calls <span class="inline-code">compProxy.setRowCtrls</span>. In every call, AG Grid passes a new <span class="inline-code">rowCtrls</span> array. Letâs add a logpoint to see how the array looks:
and check the console output:
Woah, doesnât every log look the same?
And indeed. If you debug this a bit, youâll realize that:
- The <span class="inline-code">compProxy.setRowCtrls</span> array that the component receives is always different (this is because AG Grid is re-creating it with .filter() before passing it in.
- All items in that array are identical <span class="inline-code">===</span> across rerenders
Inside <span class="inline-code">RowContainerComp</span>, AG Grid never touches the <span class="inline-code">rowCtrls</span> array â it only maps its items. So, if the array items donât change, why should <span class="inline-code">RowContainerComp</span> rerender either?
We can prevent this extra render by doing a shallow equality check:
This saves a lot of time. Because on every cell update, <span class="inline-code">RowContainerComp</span> components rerender 1568 times (!), eliminating all renders cuts off another 15-30% of the total JS cost.
Running <span class="inline-code">useEffect</span> Less Often
Here are a few other parts of the recording:
In these parts, we call a function called <span class="inline-code">gridApi.refreshCells()</span>. This function gets called four times and, in total, takes around 5-10% of the JavaScript cost.
Hereâs the Causal code that calls <span class="inline-code">gridApi.refreshCells()</span>
â
This is an unfortunate hack (one of the few which every codebase has that works around an issue with code editor autocomplete occasionally not picking up new variables.
The workaround is supposed to run every time a new variable gets added or removed. However, currently, it runs way more frequently. Thatâs because <span class="inline-code">autocompleteVariables</span> is a deeply nested object with a bunch of other information about variables, including their values:
â
When you type in the cell, a few variables update their values. That causes <span class="inline-code">autocompleteVariables</span> to update a few times â and triggers a <span class="inline-code">gridApi.refreshCells()</span> call every time.
We donât need these <span class="inline-code">gridApi.refreshCells()</span> calls. How can we avoid them?
Okay, so we need a way to call a <span class="inline-code">gridApi.refreshCells()</span> only when a new variable is added or removed.
A naive way to do that would be to rewrite useEffect dependencies like this:
â
â
This will work in most cases. However, if we add one variable and remove another one simultaneously, the workaround wonât run.
A proper way to do that would be to move <span class="inline-code">gridApi.refreshCells()</span> to the code that adds or removes a variable â e.g., to a Redux saga that handles the corresponding action. However, this isnât a simple change. The logic that uses gridApi is concentrated in a single component. Exposing <span class="inline-code">gridApi()</span> to the Redux code would require us to break/change several abstractions. Weâre working on this, but this will take time.
Instead, while weâre working on a proper solution, why donât we hack a bit more? đ
â
â
With this change, <span class="inline-code">useEffect</span> will depend only on concrete variable IDs inside <span class="inline-code">autocompleteVariables</span>. Unless any variable ids get added or removed, the <span class="inline-code">useEffect</span> shouldnât run anymore. (This assumes none of the variable ids include a , character, which is true in our case.).
Terrible? Yes. Temporary, contained, and easy to delete, bearing the minimal technical debt? Also yes. Solves the real issue? Absolutely yes. The real world is about tradeoffs, and sometimes you have to write less-than-optimal code if it makes your usersâ life better.
Just like that, we save another 5-10% of the JavaScript execution time.
Deep <span class="inline-code">areEqual</span>
There are a few bits in the performance trace of a cell update that look like this:
What happens here is we have a function called <span class="inline-code">areEqual</span>. This function calls a function called <span class="inline-code">areEquivalent</span>â and then <span class="inline-code">areEquivalent</span>calls itself multiple times, over and over again. Does this look like anything to you?
Yeah, itâs a deep equality comparison. And on a 2020 MacBook Pro, it takes ~90 ms.
The <span class="inline-code">areEqual</span> function comes from AG Grid. Hereâs how itâs called:
- React calls componentDidUpdate() in AG Grid whenever the component rerenders:
- <span class="inline-code">componentDidUpdate()</span> invokes <span class="inline-code">processPropChanges():</span>
- Among other things, <span class="inline-code">processPropChanges()</span> calls a function called <span class="inline-code">extractGridPropertyChanges()</span>
- <span class="inline-code">extractGridPropertyChanges()</span> then performs a deep comparison on every prop passed into <span class="inline-code">AgGridReactUi</span>
If some of these props are huge and change significantly, the deep comparison will take a lot of time. Unfortunately, this is precisely whatâs happening here.
With a bit of debugging and <span class="inline-code">console.time()</span>, we find out that the expensive prop is context. context is an object holding a bunch of variables that we need to pass down to grid components. The object changes for good.
â
However, using a deep comparison on such a massive object is bad and unnecessary. The object is memoized, so we can just <span class="inline-code">===</span>Â it to figure out whether it changed. But how do we do that?
AG Grid supports several comparison strategies for props. One of them implements a <span class="inline-code">===</span> comparison:
â
However, based on the source code, we can change the strategy only for the <span class="inline-code">rowData</span> prop
â
But nothing is preventing us from patching AG Grid, right? Using yarn patch, like we did above, letâs add a few lines into the <span class="inline-code">getStrategyTypeForProp()</span> function.
â
With this change, we can specify a custom comparison strategy for the <span class="inline-code">context</span> prop
And, just like that, we save another 3-5% of the JavaScript cost.
âWhatâs Still In The Works
More Granular Updates
You mightâve noticed that for one update of a category value, we rerender the data grid four times:
Four renders are three renders too many. The UI should update only once if a user makes a single change. However, solving this is challenging.
Hereâs a <span class="inline-code">useEffect</span> that rerenders the data grid:
â
To figure out whatâs causing this <span class="inline-code">useEffect</span> Â to re-run, letâs use<span class="inline-code">useWhyDidYouUpdate</span>
This tells us <span class="inline-code">useEffect</span> Â re-runs because <span class="inline-code">EditorModel</span> Â and <span class="inline-code">variableDimensionsLookup</span>Â objects change. But how? With a little custom deepCompare function, we can figure this out:
This is how <span class="inline-code">EditorModel</span> changes if you update a single category value from <span class="inline-code">69210</span>to <span class="inline-code">5</span>. As you see, a single change causes four consecutive updates. variableDimensionsLookup changes similarly (not shown).
One category update causes four <span class="inline-code">EditorModel</span> updates. Some of these updates are caused by suboptimal Redux sagas (which weâre fixing). Others (like update 4, which rebuilds the model but doesnât change anything) may be fixed by adding extra memoized selectors or comparison checks.
But thereâs also a deeper, fundamental issue that is harder to fix. With React and Redux, the code we write by default is not performant. React and Redux donât help us to fall into a pit of success.
To make the code fast, we need to remember to memoize most computations â both in components (with <span class="inline-code">UseMemo</span> and <span class="inline-code">UseCallback</span>) and in Redux selectors (with <span class="inline-code">reselect</span>). If we donât do that, some components will rerender unnecessarily. Thatâs cheap in smaller apps but scales really, really poorly as your app grows.
And some of these computations are not really memorizable:
â
This also affects the <span class="inline-code">useEffect</span> we saw above:
â
Weâre working on solving these extra renders (e.g., by moving logic away from useEffects). In our tests, this should cut another 10-30% off the JavaScript cost. But this will take some time.
To be fair, React is also working on an auto-memoizing compiler which should reduce recalculations.
<span class="inline-code">useSelector</span> vs. <span class="inline-code">useStore</span>
If you have some data in a Redux store, and you want to access that data in an <span class="inline-code">onChange</span> callback, how would you do that?
Hereâs the most straightforward way:
â
If <span class="inline-code">Cell</span> is expensive to rerender, and you want to avoid rerendering it unnecessarily, you might wrap onChange with <span class="inline-code">useCallback</span>
â
However, what will happen if <span class="inline-code">editorModel</span> changes very often? Right â <span class="inline-code">useCallback</span> will regenerate <span class="inline-code">onChange</span> whenever <span class="inline-code">editorModel</span> changes, and <span class="inline-code">Cell</span> will rerender every time.
Hereâs an alternative approach that doesnât have this issue:
â
This approach relies on Reduxâs <span class="inline-code">useStore(</span>) hook.
- Unlike <span class="inline-code">useSelector()</span>, <span class="inline-code">useStore()</span> returns the full store object
- Also, unlike <span class="inline-code">useSelector()</span>, <span class="inline-code">useStore()</span> canât trigger a component render. But we donât need to, either! The component output doesnât rely on the <span class="inline-code">EditorModel</span>state. Only the <span class="inline-code">onChange</span> callback needs it â and we can safely delay the editorModel read until then.
Causal has a bunch of components using <span class="inline-code">useCallback</span> and <span class="inline-code">useSelector</span> like above. They would benefit from this optimization, so weâre gradually implementing it. We didnât see any immediate improvements in the interaction we were optimizing, but we expect this to reduce rerenders in a few other places.
In the future, wrapping the callback with <span class="inline-code">useEvent</span> instead of <span class="inline-code">useCallback</span> might help solve this issue too.
What Didn't Work
Hereâs another bit of the performance trace:
In this part of the trace, we receive a new binary-encoded model from the server and parse it using protobuf. This is a self-contained operation (you call a single function, and it returns 400-800 ms later), and it doesnât need to access DOM. This makes it a perfect candidate for a Web Worker.
Web What? Web Workers are a way to run some expensive JavaScript in a separate thread. This allows keeping the page responsive while that JavaScript is running.
The easiest way to move a function into a web worker is to wrap it with comlink:
â
â
This relies on webpack 5âs built-in worker support
If we do that and record a new trace, weâll discover that parsing was successfully moved to the worker thread:
However, weirdly, the overall JS cost will increase. If we investigate this, weâll discover that we now have two additional flat 400-1200ms long chunks of JavaScript:
It turns out that moving stuff to a web worker isnât free. Whenever you pass data from and to a web worker, the browser has to serialize and deserialize it. Typically, this is cheap; however, for large objects, this may take some time. For us, because our model is large, this takes a lot of time. Serializing and deserializing end up longer than the actual parsing operation!
Unfortunately, this optimization didnât work for us. Instead, as an experiment, weâre currently working on selective data loading (fetching only visible rows instead of the whole model). It dramatically reduces the parsing cost:
Results
So, how much did these optimizations help? In our test model with ~100 categories, implementing the optimizations (and enabling selective data loading) reduces the JavaScript cost by almost four times đ€Ż
With these optimizations, updating category cells becomes a much smoother experience:
We still have chunks of yellow/red in the recording, but theyâre much smaller â and intertwined with blue!
Weâve managed to dramatically lower the interaction responsiveness with just a few precise changes â but weâre still far from done. Thanks to Ivan from 3Perf for helping us with this investigation. If youâre interested in helping us get to sub-100ms interaction times, you should consider joining the Causal team â email lukas@causal.app!