Why is .NET reflection slow?
I&#x27;ve been researching JSON serialization&#x2F;deserialization libraries for Unity, and I&#x27;m writing a spreadsheet comparing the different alternatives.<p>Unity JSON Libraries: <a href="https:&#x2F;&#x2F;docs.google.com&#x2F;spreadsheets&#x2F;d&#x2F;1NoIYASJR9uUT-kZxjb54zWX2Lr3EXp8F7t7N62yo7J8&#x2F;edit?usp=sharing" rel="nofollow">https:&#x2F;&#x2F;docs.google.com&#x2F;spreadsheets&#x2F;d&#x2F;1NoIYASJR9uUT-kZxjb54...</a><p>Unity is limited by the fact that it uses an older version of .NET 2.0, or even preferably a .NET 2.0 subset. It can also be optimized by disabling exceptions. And most importantly on AOT-compiled platforms like iOS and consoles, it doesn&#x27;t support dynamic JIT compilation and interfaces like Reflection.Emit, which some of the high performance JSON serialization libraries use for optimization.<p>So the question of &quot;Why is reflection so slow?&quot; is very interesting to me as a Unity iOS developer, since I&#x27;m forced to use it without all the fancy optimizations (or figure out how to avoid using it).<p>Some of the big fancy luxurious all dancing all singing JSON libraries intended for less restricted CLR platforms like Windows do all kinds of amazing tricks to reduce and optimize their use of reflection. Some of those have been stripped down and ported to Unity, but others haven&#x27;t.<p>JSON.Net is one of the most advanced feature-rich C# JSON libraries, and it is free, but it is not the fastest. The free version doesn&#x27;t run on Unity out of the box, but there is a striped down simplified version that runs on Unity available on the Unity Asset Store for $25.<p>JSON.NET home page: <a href="http:&#x2F;&#x2F;www.newtonsoft.com&#x2F;json" rel="nofollow">http:&#x2F;&#x2F;www.newtonsoft.com&#x2F;json</a><p>JSON.Net source code: <a href="https:&#x2F;&#x2F;github.com&#x2F;JamesNK&#x2F;Newtonsoft.Json" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;JamesNK&#x2F;Newtonsoft.Json</a><p>Unity JSON.Net port: <a href="https:&#x2F;&#x2F;www.assetstore.unity3d.com&#x2F;#!&#x2F;content&#x2F;11347" rel="nofollow">https:&#x2F;&#x2F;www.assetstore.unity3d.com&#x2F;#!&#x2F;content&#x2F;11347</a><p>FastJSON is quite fast true to its name, free, has a lot of advanced features, is deeply customizable, is highly optimized and benchmarked against other libraries, and there&#x27;s a great in-depth article comparing it to other libraries and explaining its optimization techniques. It hasn&#x27;t been ported to Unity, but that might be worth doing, since it&#x27;s such a nice piece of work. I asked the author about it, and he said that its speed was the result of dynamic code generation features that Unity doesn&#x27;t support. That would sacrifice some of the optimizations and advanced features, but still might be worth doing.<p>FastJSON article and benchmarks: <a href="http:&#x2F;&#x2F;www.codeproject.com&#x2F;Articles&#x2F;159450&#x2F;fastJSON" rel="nofollow">http:&#x2F;&#x2F;www.codeproject.com&#x2F;Articles&#x2F;159450&#x2F;fastJSON</a><p>FastJSON source: <a href="https:&#x2F;&#x2F;github.com&#x2F;mgholam&#x2F;fastJSON" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;mgholam&#x2F;fastJSON</a><p>Full Serializer is a well written free JSON library with lots of useful features that is specifically designed for Unity from the start, which avoids using anything that will limit optimization, like anything higher than .Net 2.0 subset features, advanced reflection features, LINQ, code generation and even exceptions. So far, it seems like the best balanced compromise I&#x27;ve found between Unity&#x27;s limitations and a full set of useful features, and it looks to me like well written but not overly complex code.<p>FullSerializer source: <a href="https:&#x2F;&#x2F;github.com&#x2F;jacobdufault&#x2F;fullserializer" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;jacobdufault&#x2F;fullserializer</a><p>Thread asking about using FastJSON on Unity, where the FullSerializer author chimes in: <a href="https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;Unity3D&#x2F;comments&#x2F;2a4c7p&#x2F;using_fastjson_in_unity&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;Unity3D&#x2F;comments&#x2F;2a4c7p&#x2F;using_fastj...</a><p>JSONObject is another free simple JSON library written for Unity, which I&#x27;ve been using for years, and while it works for basic stuff, I&#x27;m not very happy with it (which is why I&#x27;m looking around for alternatives). It&#x27;s not particularly efficient (in speed or runtime size), has a terrible API, few advanced features, and some embarrassing bugs. (It doesn&#x27;t correctly handle string escapes like \r \n \u1234 etc -- c&#x27;mon, there&#x27;s a very simple explicit JSON spec that defines every bit of the syntax: follow it!)<p>JSONObject source: <a href="https:&#x2F;&#x2F;github.com&#x2F;mtschoen&#x2F;JSONObject" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;mtschoen&#x2F;JSONObject</a><p>JSONObject on the Asset Store: <a href="https:&#x2F;&#x2F;www.assetstore.unity3d.com&#x2F;en&#x2F;#!&#x2F;content&#x2F;710" rel="nofollow">https:&#x2F;&#x2F;www.assetstore.unity3d.com&#x2F;en&#x2F;#!&#x2F;content&#x2F;710</a><p>Unity now has its own JSONUtility module that at first glance seems like it might be useful, but once you try to actually use it for anything in the real or virtual world, you hit a wall, because it&#x27;s a wolf in sheep&#x27;s clothing. Even worse than a wolf: it&#x27;s layered directly on top of Unity&#x27;s infamous serialization system, and inherits all its bizarre quirks and limitations, making it practically useless (and probably slow).<p><a href="https:&#x2F;&#x2F;docs.unity3d.com&#x2F;ScriptReference&#x2F;JsonUtility.html" rel="nofollow">https:&#x2F;&#x2F;docs.unity3d.com&#x2F;ScriptReference&#x2F;JsonUtility.html</a><p><a href="https:&#x2F;&#x2F;docs.unity3d.com&#x2F;Manual&#x2F;JSONSerialization.html" rel="nofollow">https:&#x2F;&#x2F;docs.unity3d.com&#x2F;Manual&#x2F;JSONSerialization.html</a><p>First, in order to begin to understand how truly fucked up Unity&#x27;s JSONUtility library truly is, you have to understand how truly fucked up Unity&#x27;s serialization system is, and this great classic blog posting by Lucas Meijer bravely and honestly touches the surface, then falls off into the deep end:<p>Serialization in Unity: <a href="https:&#x2F;&#x2F;blogs.unity3d.com&#x2F;2014&#x2F;06&#x2F;24&#x2F;serialization-in-unity&#x2F;" rel="nofollow">https:&#x2F;&#x2F;blogs.unity3d.com&#x2F;2014&#x2F;06&#x2F;24&#x2F;serialization-in-unity&#x2F;</a><p>While this reddit discussion goes to the heart of the matter:<p>Unity Serialization is truly FUBAR: <a href="https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;Unity3D&#x2F;comments&#x2F;2e9vlg&#x2F;unity_serialization_is_truly_fu" rel="nofollow">https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;Unity3D&#x2F;comments&#x2F;2e9vlg&#x2F;unity_seria...</a><p>For many Unity developers, an important reason for using a JSON serialization library is to avoid using Unity&#x27;s serialization system. So the idea of JSONUtility being layered on top of the system they were trying to get away from doesn&#x27;t appeal to them.<p>&quot;This is why I am moving away from scriptable objects style services and into using static clr objects that load their configuration from csv &#x2F; json txt files. I am loosing editor integration, but Im gaining peace of mind that unity wont blow up and corrupt my array of 100 items, levels, whatever.&quot; [–]nventimiglia Expert 2 points 2 years ago <a href="https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;Unity3D&#x2F;comments&#x2F;2e9vlg&#x2F;unity_serialization_is_truly_fubar&#x2F;cjy86ho&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;Unity3D&#x2F;comments&#x2F;2e9vlg&#x2F;unity_seria...</a><p>I&#x27;d prefer to build on top of Unity&#x27;s built-in optimized JSON parser if that were possible, layering a system like Full Serialization on top of it without tangling with Unity Serialization, but the current JSONUtility API makes that impossible. Unity should publish the source for JSONUtility to the community (and throw in Serialization too please, so we can at least understand how it works when we&#x27;re forced to interact with it), and accept pushes, please!