Improving XNA Reflection-Based Readers and Writers

This post is a bit random. I’m not feeling well today, but I had an idea while staring into space and decided I shouldn’t waste it.

I thought about the XNA Content Pipeline, and the convenience of reflection-based serialization. I thought about the run-time performance penalty you pay for that convenience, and suddenly realized that you can have the best of both worlds.

What if the reflection-based XNB serializer also generated source code for a ContentTypeReader? Then you’d have the convenience of automatic serialization at build time, and the performance of custom deserialization at run-time! The source code for all the generated content readers could be compiled by the content pipeline into an assembly and deployed along with the rest of the content. Then, when the content pipeline needs to read the types at run-time, it would use the (automatically-generated) custom readers instead of reflection.

Since I’m not sure I want to make this my keep-busy-while-sick project, I’m throwing the idea out there to see if anyone else wants to try it. If you try, but get stuck, I’ll be happy to help by answering questions about dynamic code gen or msbuild customization.

A couple references to get you started:

Content Pipeline

Code Generation and Compilation

About these ads

About badcorporatelogo

My name is Stephen Styrchak, and I work as a software developer in Seattle, Washington, USA.
This entry was posted in Uncategorized. Bookmark the permalink.

2 Responses to Improving XNA Reflection-Based Readers and Writers

  1. Shawn Hargreaves says:

    Hehe, did you know that was the original Content Pipeline design way back when we were first planning XNA 1.0? All that ContentTypeWriter/Reader stuff was going to be purely internal, and the idea was that we would codegen them as part of the build process.

    But that didn’t fit the schedule, so we figured out a way to build the thing much more quickly by just making those types public and having everybody write them by hand. And then when I got some time to revisit the issue in a later release, it would still have taken too long to implement a codegen mechanism, so I did the dynamic reflection based serialization thing instead.

    I would totally love to see a codegen approach, if anyone cares to try that. Would be great to find out after all these years if that original XNA 0.5 design was actually any good!

  2. zeuxcg says:

    While I don’t know anything about XNA, I use a dynamically generated serialization code for fast binary serialization; sources available at http://hg.zeuxcg.org/fungine/src/tip/src/core/serialization

    The approach definitely works; I would prefer to live without JIT overhead on startup, but currently it’s not really feasible for the way I’m using this (my content pipeline steps run inside the game – potentially while the game is running, for asset hot reloading).

    Note that I also JIT-compile the serialization code – this makes saving big files way faster.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s