·5 min read

Rewrite Python in Rust? Profile it first

Our Python asset builder ran 385 seconds against a 204 second budget. A profile showed the time was in call counts, not language speed. Here are the numbers.

gamedevprocedural-generationworkflow

Contents

Our Python build for one 3D model took 385 seconds. The time budget was 204. The obvious thought was that Python is slow and a Rust rewrite would fix it. We profiled one build before deciding, and the profile said something else: the slow part was how often we called things, and a faster language would have called them just as often.

This post has the numbers, and the order of questions we asked, so you can run the same check before you start a rewrite.

The first answer: most of it is already native

Before any measuring, we listed what the pipeline does and what runs it. Booleans, UV unwrapping, mesh decimation, ray casting for ambient occlusion, USD export: each of those is a C++ library with a Python wrapper. The array work is numpy and scipy, which is C and Fortran.

A Rust rewrite would call the same C++ libraries. Their share of the runtime would not change. So a rewrite can only speed up our own Python, and the first question is how much of the time that is.

The honest answer to that is a profile, not a guess. We used cProfile on one build of our biggest model, on one thread:

python -m cProfile -o tank.prof -m our_tool build assets/tank_01 --profile mobile
python -c "import pstats; pstats.Stats('tank.prof').sort_stats('cumtime').print_stats(25)"

What the profile showed

Three facts, in the order they mattered.

One stage owned the build. The LOD tier search took 324 of the 385 seconds, 84 percent. The rest of the pipeline was small by comparison. If we had rewritten everything in Rust and made it ten times faster, the other 16 percent would have shrunk and the search would still have been the build.

Only about 23 percent of time was inside native or built-in calls. That is 89 of 385 seconds. So most of the time was in our own Python, which is the part a rewrite could change. That is the case for a rewrite. Keep reading, because the next fact weakens it.

The functions were called far too often. One mesh-building function ran 59,845 times. It built 180,000 extrusions and intersected them in threes. Most of its 51 seconds of own time was probably the boolean itself, which cProfile cannot see inside. Faster code around it would not help. Another function, a collapse checker used when simplifying meshes, called np.linalg.det 9.35 million times, once per triangle, and spent 63 seconds doing it.

A search that runs the same expensive step 60,000 times is slow in any language. Rust would make each call a bit cheaper and leave the count alone.

Fixes, ranked by how much they were worth

Hotspot Time Fix
LOD tier search, mesh builder 51 s self, 70 s total Call it less: cache the per-view shapes, which do not change between steps, and prune the search
Collapse checker setup 63 s Compute every volume in one numpy expression instead of 9.35 million small calls
Raster loops over pixels about 32 s Vectorise, then Numba if needed
String check in the cache hash 7 s Check the array dtype once, instead of every element

A caution on the numbers. The times above are measured. The estimates in the fix column, for example that the collapse checker setup should drop from about 63 seconds to under one, were predictions when we wrote them, not results. Treat them as the reason to try, not as proof.

The measured result came from a different change. We built a prototype that stopped searching for the tiers and generated them per part instead. LOD generation went from about 245 seconds to 56. A turret went from 141 seconds to 33, and a track from 43 to 9. No language change was involved.

The order we would use again

  1. List what is already native. If the heavy work is in C++ libraries, a rewrite cannot touch it.
  2. Profile one slow run, on one thread. Sort by cumulative time and look at the top stage first, then at call counts.
  3. Look at call counts before per-call cost. Nine million calls to a cheap function, or sixty thousand to an expensive one, usually means the algorithm is asking the question too many times.
  4. Then vectorise. A loop over triangles or pixels written in plain Python can often become one numpy expression. That is hours of work, not months.
  5. Only then think about native code. A tight loop that cannot be vectorised is a good target for Numba, or a small Rust extension, not a full rewrite.

What this does not say

It does not say Rust is a bad choice. If we had started the project today with a speed target and no existing code, a compiled language is a fair option. The claim is narrower: for an existing pipeline whose heavy work is already native, a rewrite is the most expensive experiment you can run, and the profile is the cheapest. Run the cheap one first.

One more caveat. cProfile adds its own overhead, and it inflates the share of time spent in many small Python calls. Its ranking is trustworthy; its absolute numbers are a little pessimistic. We checked our big claim, that the search stage dominates, by timing the stage directly before we changed anything.

More posts

New writing when there is something worth saying. By email or by RSS, whichever you prefer.