Serialization in a Flame Graph
A sampling profiler attached to an executor records JVM stacks, so it sees only half of a Python UDF job. In Spark 3.x, each task that talks to Python has 2 JVM threads worth reading: the task thread, which runs the stage and reads results back from the worker, and a writer thread named after the Python executable, such as stdout writer for python3, which pushes input rows to the worker. The Python worker is a separate process, so its CPU time is missing from the graph and shows up indirectly, as the task thread blocked reading a socket.
Frames that mean serialization
| Frame | Thread | What it means |
|---|---|---|
| `net.razorvine.pickle.Pickler.dump` | Writer | Rows pickled for a plain `@udf` |
| `EvaluatePython.toJava` | Writer | Spark values turned into pickle-ready Java objects |
| `Unpickler.load`, `EvaluatePython.makeFromJava` | Task | Results unpickled and turned back into Spark values |
| `ArrowWriter.write` | Writer | Row values copied into Arrow column vectors |
| `socketRead0` or `NioSocketImpl.read` under the Python reader | Task | JVM idle, waiting for the Python worker |
| `ObjectOutputStream.writeObject0` | Shuffle writer, block manager | Java serialization of RDD records or broadcasts |
| `Kryo.writeClassAndObject` | Shuffle writer, block manager | Kryo serialization of those same objects |
| `DefaultClassResolver.writeName` | Under Kryo frames | Full class names written for unregistered classes |
Reading one profile
The table below is one illustrative capture from a 40-minute stage that applies a plain Python UDF to a Parquet table, with samples grouped by the frame families above. Real profiles vary, and the proportions are what you read.
| Frames | Share of executor samples |
|---|---|
| `Pickler.dump`, `EvaluatePython.toJava` | 31% |
| `Unpickler.load`, `EvaluatePython.makeFromJava` | 14% |
| Socket read, waiting on the Python worker | 38% |
| Parquet scan and the generated stage code | 12% |
| Everything else | 5% |
45% of the samples is the JVM side of the pickle border, and 38% is the JVM waiting while Python unpickles, calls the function per row and pickles the results. Both point the same way. If the body vectorizes, a pandas UDF removes the pickle frames and divides the Python calls by the batch size. If it does not, useArrow=True removes the pickle frames and leaves the socket wait, which is now mostly your function, for a profile on the Python side to explain. With a scan share this small, reading the data was never the problem.
Check every fix the way you found the problem: compare the stage's task time in the Spark UI before and after, then capture a new graph and confirm the frames you targeted shrank. A fix that moves work from one frame family to another, say from Pickler.dump to ArrowWriter.write, should still shrink the total. If the total holds steady, read the new graph again before choosing the next fix: a wide ArrowWriter.write means columns the function never reads are crossing, and a wide socket wait means the Python body itself is now the cost.
After a move to a pandas UDF, a stage's flame graph shows ArrowWriter.write at 40% of executor samples. The UDF is called on F.struct(*df.columns) over a 60-column table, and its body reads 2 fields. What should change?
- Switch spark.serializer to Kryo so that the Arrow batches are written faster
- Raise spark.sql.execution.arrow.maxRecordsPerBatch so the writer runs fewer times
- Go back to a plain @udf, since converting to Arrow costs more than pickling
- Pass only the 2 fields the body reads, so 2 columns cross instead of 60
About this lesson
This section is part of the UDFs and Serialization: Advanced lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.