After reading few great articles (this, this and this) about Spark's DataSets, I finishing with next DataSet's performance benefits over RDD:
- Logical and physical plan optimization;
- Strict typization;
- Vectorized operations;
- Low level memory management.
Questions:
- Spark's RDD also builds physical plan and can combine/optimize multiple transformations at the same stage. Then what is the benefit of DataSet over RDD?
- From the first link you can see an example of
RDD[Person]
. Does DataSet have advanced typization? - What do they mean by "vectorized operations"?
- As I understand, DataSet's low memory management = advanced serialization. That means off-heap storage of serializable objects, where you can read only one field of an object without deserialization. But how about the situation when you have
IN_MEMORY_ONLY
persistence strategy? Will DataSet serialize everything any case? Will it have any performance benefit over RDD?
When working with RDD what you write is what you get. While certain transformations are optimized by chaining, the execution plan is direct translation of the DAG. For example:
where
shuffle
is an arbitrary shuffling transformation (*byKey
,repartition
, etc.) all threemapPartitions
(map
,flatMap
,filter
) will be chained without creating intermediate objects but cannot be rearranged.Compared to that
Datasets
use significantly more restrictive programming model but can optimize execution using a number of techniques including:Selection (
filter
) pushdown. For example if you have:can be executed as:
Early projections (
select
) and eliminations. For example:can be rewritten as:
to avoid fetching and passing obsolete data. In the extreme case it can eliminate particular transformation completely:
can be optimized to
These optimizations are possible for two reasons:
To make it clear let's say we have a following data model:
And we want to retrieve surnames of all people older than 21. With
RDD
it can be expressed as:Now let's ask ourselves a few questions:
age
inf
andage
variable withg
?f
and theng
the same asg
and thenf
?f
andg
side effects free?While the answer is obvious for a human reader it is not for a hypothetical optimizer. Compared to that with
Dataframe
version:the answers are clear for both optimizer and human reader.
This has some further consequences when using statically typed
Datasets
(Spark 2.0 Dataset vs DataFrame).Dataset[Row]
and at this moment it is not possible to encode complex type hierarchy.In context of optimization we usually mean loop vectorization / loop unrolling. Spark SQL uses code generation to create compiler friendly version of the high level transformations which can be further optimized to take advantage of the vectorized instruction sets.
Not exactly. The biggest advantage of using native allocation is escaping garbage collector loop. Since garbage collections is quite often a limiting factor in Spark this is a huge improvement, especially in contexts which require large data structures (like preparing shuffles).
Another important aspect is columnar storage which enables effective compression (potentially lower memory footprint) and optimized operations on compressed data.
In general you can apply exactly the same types of optimizations using hand crafted code on plain
RDDs
. After allDatasets
are backed byRDDs
. The difference is only how much effort it takes.sun.misc.Unsafe
with native memory allocation is not for the faint-hearted.Despite all its merits
Dataset
API is not universal. While certain types of common tasks can benefit from its optimizations in many contexts you may so no improvement whatsoever or even performance degradation compared to RDD equivalent.