[Mono-devel-list] Q. about implementation of serialization

Jonathan Pryor jonpryor at vt.edu
Tue Oct 26 22:15:44 EDT 2004

I'll just note up front that I don't know the actual answers to your
questions, but I do have some background.

On Tue, 2004-10-26 at 21:53, Okehee Goh wrote:
> I have a question about Serialization.
> Serialization should trace all reachable objects (an object graph)
> from a root-object to be serialized. To determine what to serialize
> needs class information of reachable objects in the graph. How  does
> it get the information? Is Serialization implemented using reflection?

Reflection *can* (and will) be used.

It's also important to note that there are two forms of serialization:
Runtime Serialization (involving the System.Runtime.Serialization
namespace), and XML Serialization (via System.Xml).

System.Xml, IIRC, uses Reflection, and is limited to serializing *only*
public class members.  As you note, it can be slow, which is why in .NET
2.0 Microsoft optimized XML Serialization, to the point that dynamic
code is generated at run time to speed up the (de)serialization
process.  Of course, there's a performance hit generating code instead
of interpreting it, but if you use lots of serialization, you make up
for the code generation + JIT overhead.

Runtime serialization is the standard interface ([Serializable] and
ISerializable).  It need not directly use Reflection, as it's part of
the runtime (and can thus use any internal interfaces the runtime might
support), which is why it also handles private and protected class
members.  I don't know what Mono does here.

> Otherwise, MONO uses internal functions to identify the information? I
> also wonder how it is doen in JVM?

Well, there's really only two ways to identify the information: via
Reflection (or in Java's case, java.lang.reflect), and through some
internal runtime mechanism (which provides the equivalent of
java.lang.reflect).  I imagine Java uses whichever is more convenient. 
That is, for JVM's implemented in Java, java.lang.reflect may be used;
for native JVM's, an internal mechanism may be used.  Keep in mind that
there are several JVM's running around, so the answer to this can and
will vary.

> I'm worrying that if reflection is involved for serialization, it may
> have too much performance overhead.

"Premature optimization is the root of all evil."  -- Donald Knuth.

"You have to measure to be sure of anything."  -- Rico Mariani.

In short, don't worry about the overhead until you've measured the
overhead and determined that it's too high.  Any (every) feature will
have some overhead associated with it, and you need to measure the
associated overheads to determine which is worth worrying about.
Otherwise, you potentially waste your time working around the overhead
(premature optimization), when it may not be a concern.

There may be other parts of your app which have bigger impacts on
performance than serialization.  The only way to know for sure is to
test and measure.

 - Jon

More information about the Mono-devel-list mailing list