My tiny Python workload spends almost all its time inside score. Its parents, batch and pipeline, each show almost the same cumulative time. Adding those rows would bill the same work repeatedly. The profiler hasn't found extra work; the report contains overlapping views of it.

This is timely because rust-anything v4.1.0 changes its profiling recommendations to leave a script's top level out of “What to port.” [1] The broader trap is worth understanding before asking any rewrite tool to move the largest-looking row.

Make an overlapping profile yourself

Here is a complete standard-library example. Save it as profile_demo.py and run python3 profile_demo.py. It performs integer arithmetic on synthetic inputs; it doesn't invoke rust-anything, an agent or a compiler.

import cProfile import pstats def score(n): total = 0 for i in range(n): total += (i * i) % 97 return total def batch(): return [score(200_000) for _ in range(4)] def pipeline(): return batch() profiler = cProfile.Profile() result = profiler.runcall(pipeline) assert result == [9_600_239] * 4 print(result) profiler.dump_stats("demo.prof") stats = pstats.Stats(profiler).strip_dirs() stats.sort_stats(pstats.SortKey.CUMULATIVE).print_stats() stats.print_callers("score") stats.sort_stats(pstats.SortKey.TIME).print_stats() 

The result is:

[9600239, 9600239, 9600239, 9600239] 

The report records four calls to score, one to batch and one to pipeline. On my saved profile, their cumulative times each round to 0.197 seconds. That is one instrumented run, not a portable performance expectation. Your timings will differ; the nesting is the point.

One synthetic Python profile shows overlapping cumulative times of about 0.197 seconds for pipeline, batch and score. Almost all own time belongs to score.

Original figure from the saved profile, CC BY 4.0. Orange bars include callees; dark bars show time inside the function itself.

Read down the call chain, not down a shopping list

In Python's profile report, tottime excludes subcalls and cumtime includes them. [2] So pipeline can have a large cumulative time while doing almost no work of its own. It called batch; batch called score. The time spent in score appears in each ancestor's cumulative total.

I first sort by cumulative time to find the expensive route through the program. Then I inspect callers and sort by own time to find where Python work actually occurs. Neither order alone chooses the right boundary for a rewrite.

For this example, moving only the wrapper and continuing to call the same Python score body wouldn't remove that body's arithmetic. Moving the arithmetic could remove it, but would introduce a language boundary whose cost and behavior need checking. The call graph tells me which proposal makes sense to investigate. It does not tell me how fast the port will be.

A batch boundary may also avoid repeated crossings that a per-item boundary would introduce. That's a design consideration, not a result from this run. No Rust alternative ran here, so there is no Rust speedup to report.

The next measurement needs a different tool

Python's documentation explicitly separates profiling from benchmarking and warns that profiling overhead differs between Python and C-level code. [2] Using instrumented Python times against uninstrumented native times can flatter the port before it has earned anything.

For a real rewrite, freeze representative inputs and expected outputs first. Check the port's returned values and error behavior, then measure both complete workflows without the profiler under comparable conditions. Preserve serialization, allocation and crossing costs in that measurement if the application will pay them.

The small example here provides a correctness checksum and a reproducible call structure. It doesn't validate an automated migration system or establish a production bottleneck. What it does remove is a bad reading of the report: the cumulative column is a map of nested work, not a set of independent savings to add up.

References and figure credit

[1] rust-anything v4.1.0 release notes, including the script-top-level profiling correction. Credit: Erkam Yaman and the named release contributors.

[2] Python 3.14 documentation: The Python Profilers, especially report columns, callers and the benchmarking warning.

Figure: original chart from the executed synthetic workload, licensed CC BY 4.0.

AI disclosure

AI was used to draft the article.