Welcome to the final chapter of the HighlightedCode tutorial!
In the previous chapter, Global Highlight Cache, we optimized the heavy mathematics of syntax highlighting. We ensured that if we calculated the colors for a file once, we never had to do the math again.
However, there is still one layer of "waste" left. Even if the math is cached, React still has to run the component function, create new objects, and generate new Virtual DOM elements every time something updates.
In this chapter, we explore React Compiler Memoization.
Imagine a factory worker building toy cars.
In our terminal application, creating new objects and JSX elements (<div />, <Ansi />) costs money (CPU resources). We want to stop rebuilding things that haven't changed.
We use the React Compiler. This is a tool that transforms our code automatically.
It gives the factory worker a Checklist (The Cache Array $) and a set of Rules.
$[0]) the same as before?"This is called Fine-Grained Reactivity. We don't just cache the final result; we cache every tiny step along the way.
There are two strange-looking symbols you will see in the code. These are generated by the compiler.
_c)This stands for "Cache." It asks React to reserve a specific number of "memory slots" for this component.
const $ = _c(20); // "Give me 20 memory slots for this component"
$)This is the array of memory slots.
$[0]: Stores a dependency (like a variable code).$[1]: Stores the result (like the formatted string).
Instead of standard useMemo, which developers write manually, the compiler automates this logic for everythingβeven creating JSX elements.
Let's visualize a component deciding whether to reformat a string.
Let's look at the generated code in Fallback.tsx. It might look intimidating at first because it is machine-generated, but it is actually very simple logic repeated over and over.
At the very top of HighlightedCodeFallback, we initialize the cache.
// Inside HighlightedCodeFallback
import { c as _c } from "react/compiler-runtime";
export function HighlightedCodeFallback(t0) {
// Reserve 20 slots of memory for this component
const $ = _c(20);
const { code, filePath } = t0;
// ...
}
We want to convert tabs to spaces. But we only want to do this if the code string actually changed.
let t3; // Temporary variable for the result
// If the code in Slot 0 is NOT the same as the current code...
if ($[0] !== code) {
// 1. Do the work
t3 = convertLeadingTabsToSpaces(code);
// 2. Save the inputs and outputs for next time
$[0] = code;
$[1] = t3;
} else {
// If inputs are the same, just read the saved result
t3 = $[1];
}
Explanation: This is exactly what useMemo does, but "unrolled" into raw if statements for maximum speed.
This is where the React Compiler shines. It even caches the UI elements themselves!
let t5;
// Check if 'codeWithSpaces' ($[9]) changed
if ($[9] !== codeWithSpaces) {
// If it changed, create a new JSX element
t5 = <Ansi>{codeWithSpaces}</Ansi>;
// Save it
$[9] = codeWithSpaces;
$[10] = t5;
} else {
// Reuse the existing JSX element instance
t5 = $[10];
}
Explanation: Standard React re-creates <Ansi>...</Ansi> object every render. This code reuses the exact same object in memory if the text hasn't changed. This relieves pressure on the Garbage Collector.
Finally, the component combines these cached parts.
let t6;
// Check if either input changed
if ($[11] !== codeWithSpaces || $[12] !== language) {
// Re-create the Child Component
t6 = <Highlighted codeWithSpaces={codeWithSpaces} language={language} />;
// Update cache
$[11] = codeWithSpaces;
$[12] = language;
$[13] = t6;
} else {
t6 = $[13];
}
useMemo?
You might ask: "Why don't we just write useMemo manually?"
useMemo makes code hard to read.[]. The compiler never forgets.Congratulations! You have completed the HighlightedCode tutorial.
Let's review the journey we took to build the ultimate code renderer:
By combining these layers, we have created a component that is Safe, Fast, Efficient, and Beautiful.
You are now ready to dig into the codebase and start building your own high-performance terminal tools!
Generated by Code IQ