![]() |
![]() |
The images above show two different Cornell boxes that I want to speed up. In my experience, premature optimization can be a problem: you keep shaving off time here and there and never get anything done. Don't optimize until you can see it's a problem. Well, it officially became one: I'm trying to shoot 20,000 samples per pixel. Now I need to make it fast.
The result

| Step |
cornell_blocks this step |
cornell_blocks total |
cornell_vehicles this step |
cornell_vehicles total |
|---|---|---|---|---|
| 1 thread | 0.107× | 0.0986× | ||
| 20 threads | 9.37× | 1× | 10.1× | 1× |
| mesh BVH | 1.01× | 1.01× | 323× | 323× |
| SAH | 1.02× | ≈1.02× | 1.06× | ≈342× |
| cull | 2.7× | ≈2.77× | 1.74× | ≈593× |
| identity skip | 1.17× | ≈3.24× | 1.08× | ≈638× |
| top-level tree | 0.842× | ≈2.72× | 0.96× | ≈612× |
| tree at 16+ only | 1.09× | ≈2.98× | 0.986× | ≈604× |
| box primitive | 0.966× | ≈2.88× | 1.08× | ≈650× |
| small-node scan | 1.08× | ≈3.12× | 0.999× | ≈649× |
| last step, timed directly against the start | 3.1× | 705× |
Table 1: Each step's own gain, and the running total measured against 20 threads with no other optimizations.
A few things stand out straight away
- Threads help both scenes about equally: 9.37× for the blocks and 10.1× for the vehicles.
-
The mesh BVH only matters where there are meshes. It speeds
cornell_vehiclesup 323×, because the vehicles are built from triangles, and leavescornell_blocksunchanged at 1.01×, because the blocks have no triangles to search. - Culling is the other clear gain: 2.7× for the blocks and 1.74× for the vehicles.
- Every other step moves things by less than 20%, and the top-level tree actually slows both scenes down. The rest of the article looks at why.
The cost


A render's time is the number of pixels, times the samples per pixel, times what each sample costs. The two final images above push the first two factors as far as I took them: 20,000 samples per pixel, at 600² for the blocks and 1200² for the vehicles. Here is what they took with every optimization, and what they would have taken with none:
cornell_blocks |
cornell_vehicles |
|
|---|---|---|
| Image | 600², 20,000 spp | 1200², 20,000 spp |
| Samples traced | 7.2 billion | 28.8 billion |
| No optimizations, 1 thread (estimate) | ≈2.9 hours | ≈250 days |
| All optimizations, 20 threads | 6 min 41 s | ≈57 min |
| Gain | ≈26× | ≈6,400× |
Table 2: What the two final renders took with every optimization, against an estimate for one thread with none.
The gains in Figure 2 come from small 300² renders, which keep each run to seconds so I can repeat it many times. Because time grows with pixels × samples, a step that makes the small image 2× faster makes the big one 2× faster too. So once each step had proven itself on the small images, I spent the gains on the big ones and rendered as much as I could. For a CPU, these numbers aren't bad, but the next big jump is probably porting to a GPU.
From the estimates, I'd say the optimizations came at the right time. Nobody waits 250 days for a picture, but 57 minutes is a lunch break. For the rest of the article, I'll explore what each optimization is and how much time it saved.
Threads
The first thing I can change is the number of threads. Until now, everything ran on one thread, so one core did all the work while the rest of the CPU sat idle. My laptop can run up to 20 threads at once.
A ray tracer is about as easy to split as work gets. Every pixel is computed on its own: no pixel waits for its neighbour. So I cut the image into horizontal bands, one band of rows per thread, and let every thread work on its own band at the same time. Think of Jason (yes, I have played some GTA 6) painting the walls for Lucia. By himself, he gets it done eventually. With 20 Jasons (lucky Lucia), each with their own stretch of wall, the job finishes far sooner, as long as nobody has to wait for anyone else.
However, some Jasons finish earlier than others. That's another opportunity for optimization, but it's easy to fall into the never-done trap, so it will have to wait until much later.
I rendered both boxes on 1 thread, then 2, 4, 8, 12, 14 and 20, and timed each one. These runs use the final build, with every optimization in this article turned on, so the 20-thread numbers below differ from the 9.37× and 10.1× in Table 1, which were measured on the build with none:
| Threads | 1 | 2 | 4 | 8 | 12 | 14 | 20 |
|---|---|---|---|---|---|---|---|
cornell_blocks (64 spp) |
1× | ≈2.10× | ≈3.76× | ≈5.71× | ≈6.97× | ≈7.88× | 8.85× |
cornell_vehicles (16 spp) |
1× | ≈1.81× | ≈3.45× | ≈5.09× | ≈5.35× | ≈6.67× | 7.79× |
Table 3: The final build's speed-up over one thread, at each thread count.

Notice the sample counts: 64 and 16 per pixel on a 300² image, nowhere near the 20,000 per pixel at 600² and 1200² in Figure 3 and Figure 4. As earlier mentioned, I kept both samples and pixels small because the gain will translate and indeed it did.
Why it is not ideal
Not all 20 threads are equal. My CPU is an i7-12700H, with 6 fast performance cores and 8 slower efficiency cores. Each performance core runs two threads, while each efficiency core runs one. That gives 12 threads across 6 cores and 8 threads across 8 cores. So 20 threads is really 14 cores of two different speeds, and the threads I add last are the weakest.
One core alone runs faster. With only one core busy, the chip can push that core to its top speed. With every core busy, the power and heat are shared across all of them, so each core runs slower, even though together they get more done.
The laptop throttles. When the chip gets too hot, it lowers its own clock speed to protect itself, so every core does less work each second. With all 20 threads busy, it heats up faster and slows down sooner. I also had other applications open, which take a share of the cores and add their own heat.
Ultimately, being about 8 times faster may not be ideal, but it is an overall improvement.
The mesh BVH
This gave us the biggest gain of the whole article: 323× on cornell_vehicles, and 1.01× on cornell_blocks, which is no change at all (Table 1). On 20 threads, one pass over the small vehicles image went from 6.7 s to about 21 ms. That one step is most of the ≈6,400× in Table 2.
That isn't surprising once you see what a BVH does.
What a BVH is. To find what a ray hits, the simplest approach is to test it against everything in the scene, one by one. A bounding volume hierarchy (BVH) avoids that by putting things in boxes. Nearby objects share a box, nearby boxes share a bigger box, and so on, up to one box around the whole scene. A ray tests the big box first. If it misses, everything inside is skipped with a single test. If it hits, the ray checks the smaller boxes inside and only goes further into the ones it hits. It works like looking up a word in a dictionary: you jump to the right letter instead of reading every page.
Why it needs to reach inside meshes. Ray Tracing: The Next Week builds its BVH over whole objects. A mesh like the car in Figure 4 counts as one object, even though it is made of thousands of triangles. So the tree could lead a ray to the car, but then the ray still tested every triangle in it, one by one. The mesh BVH builds a second tree inside each mesh, so a ray that reaches the car only tests the few triangles near where it lands.
Why the vehicles gained so much and the blocks so little. The vehicles are made of meshes. Without the mesh BVH, each ray tested 21,024 triangles; with it, about one. The frame got 323× faster rather than thousands of times, because once the triangle tests were gone, shading and bouncing took up most of the time. The blocks have no meshes, only flat quads, so the mesh BVH had nothing to speed up.
SAH
The first tree split each box's triangles at the middle one, so each half got the same number. That's simple, but it ignores how big the boxes end up. A ray is more likely to hit a big box than a small one, so a split that leaves one huge box and one tiny one makes rays do more work. The surface area heuristic (SAH) weighs that: it tries several places to cut and keeps the one where each side's surface area times its number of triangles is lowest. That lowers the overall cost of walking the BVH.
On the vehicles it cut triangle tests per ray by a third, but the frame only got 1.06× faster (Table 1). That's why I stopped there. After the mesh BVH a ray tests about one triangle, so making that one test rarer can't save much. The time had moved somewhere else: the very next step, culling, gave the vehicles 1.74×, far more than SAH did.
Why the rest were small
After the mesh BVH, most of what was left wasn't triangles. So each step after it could only speed up a small slice of the time, and a big gain on a small slice is a small gain overall. Culling is the one exception, because it went after where the time actually was: the scene graph.
Culling
A scene in my ray tracer is a tree of nodes. Each node has its own position, rotation and scale (a matrix), and holds shapes or more nodes. Each block in cornell_blocks is one node with six quads under it. Before culling, a ray reaching a node was moved into that node's space with the matrix and then tested against every shape inside, whether it came anywhere near them or not.
Culling puts a box around each child and tests the ray against that box first. If the ray misses the box, it skips the child entirely, so the matrix and everything inside are never touched. For a block, one box test now replaces a matrix and six quads for every ray that misses it, and most rays miss most blocks.
That gave both scenes a big jump: 2.7× for the blocks and 1.74× for the vehicles.
The rest
The other steps each moved things by less than 20%. Skipping the matrix maths for nodes that aren't moved gave a little. A tree over each node's children made the blocks slower, since a handful of children is quicker to test in order, so it now only kicks in at 16 or more. The rest landed within noise: on these two scenes there just wasn't much time left to save.
What's next
There is still plenty left to try on the CPU:
- Better work sharing between threads. Some threads finish their band early and sit idle. Cutting the image into small tiles instead of bands should keep every thread busy to the end.
- A tighter BVH layout. Smaller nodes that fit the CPU's cache better, and wider nodes with four or eight children tested at once.
- Fewer samples for the same picture. Sampling the lights directly and adding a denoiser would get a clean image with far fewer samples per pixel.
- The GPU. Once the CPU work is done, porting to the GPU is the next big jump.
What I do now
Making it fast taught me as much about measuring as about optimizing. These are the habits I keep:
- Measure the before and the after together, in the same session, alternating the two, because the laptop's speed drifts.
- The picture must not change. A speed-only change has to produce a byte-identical image, or it isn't a speed-only change.
- Count as well as time. Counts like triangle tests per ray come out the same on every run, so they catch changes the clock is too noisy to see.
- Write the prediction down first. I was wrong several times, and each time that was the most useful thing I learned.
- Keep runs short. Small images and few samples, so every timing can be repeated and the laptop doesn't heat up mid-run.
References
- Peter Shirley, Trevor David Black, Steve Hollasch, Ray Tracing: The Next Week, https://raytracing.github.io/.
- Matt Pharr, Wenzel Jakob, Greg Humphreys, Physically Based Rendering, 4th ed., the BVH chapter (binned SAH,
LinearBVHNode), https://pbr-book.org/.
Top comments (0)