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

FrameThreadWhat it means
`net.razorvine.pickle.Pickler.dump`WriterRows pickled for a plain `@udf`
`EvaluatePython.toJava`WriterSpark values turned into pickle-ready Java objects
`Unpickler.load`, `EvaluatePython.makeFromJava`TaskResults unpickled and turned back into Spark values
`ArrowWriter.write`WriterRow values copied into Arrow column vectors
`socketRead0` or `NioSocketImpl.read` under the Python readerTaskJVM idle, waiting for the Python worker
`ObjectOutputStream.writeObject0`Shuffle writer, block managerJava serialization of RDD records or broadcasts
`Kryo.writeClassAndObject`Shuffle writer, block managerKryo serialization of those same objects
`DefaultClassResolver.writeName`Under Kryo framesFull 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.

FramesShare of executor samples
`Pickler.dump`, `EvaluatePython.toJava`31%
`Unpickler.load`, `EvaluatePython.makeFromJava`14%
Socket read, waiting on the Python worker38%
Parquet scan and the generated stage code12%
Everything else5%

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?

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.