Performance optimizing use of generated XmlSerializer class

前端 未结 2 668
[愿得一人]
[愿得一人] 2020-12-10 10:17

We have a few XML files that are being read by our applications. The XML format is fixed, and thus we can read them very easily with XmlSerializer.

I use this code t

相关标签:
2条回答
  • 2020-12-10 10:39

    Unless you are doing the serialization at the very app startup, one way would be to force CLR to load and even compile whatever classes you're using ahead of time, possibly in a thread which would run in background as soon as you've started your app.

    Something like, for example:

    foreach (Assembly a in assembliesThatShouldBeCompileed)
        foreach (Type type in a.GetTypes())
            if (!type.IsAbstract && type.IsClass)
            {
                foreach (MethodInfo method in type.GetMethods(
                                    BindingFlags.DeclaredOnly |
                                    BindingFlags.NonPublic |
                                    BindingFlags.Public |
                                    BindingFlags.Instance |
                                    BindingFlags.Static))
                {
                    if (method.ContainsGenericParameters || 
                        method.IsGenericMethod || 
                        method.IsGenericMethodDefinition)
                        continue;
    
                    if ((method.Attributes & MethodAttributes.PinvokeImpl) > 0)
                        continue;
    
                    System.Runtime.CompilerServices
                       .RuntimeHelpers.PrepareMethod(method.MethodHandle);
                }
            }
    

    It's strange, however, that your profiling seems to indicate that there is not much difference if the SGEN'd code is in a separate assembly, while loading seems to be the bottleneck. I wonder how the graph looks like for the case where they are in the same assembly?

    0 讨论(0)
  • 2020-12-10 10:47

    Note: OP posted a sample config: http://pastebin.com/d67nch3R

    Based on the sample config and the type of issue you're experiencing there are a couple brute-force ways, pretty much guaranteed to do the trick, both boiling down to abandoning the XML serializer altogether

    Route #1

    Abandon XML serialization and use XDocument to get data out of the XML.

    Route #2

    Use json and Newtonsoft Json to store and load configs. It should perform a lot better than XML Serializer

    The sample json counterpart would look like this:

    {
      "Connections": {
        "-default": "Local\\SqlServer",
        "-forcedefault": "false",
        "group": {
          "-name": "Local",
          "connection": {
            "-name": "SqlServer",
            "database": {
              "-provider": "SqlServer",
              "-connectionString": "blah"
            }
          }
        }
      },
      "LastLanguage": "en",
      "UserName": "un",
      "SavePassword": "true",
      "AutoConnect": "false",
      "Password": "someObfuscatedHashedPassword==",
      "ConnectionName": "Somewhere\\Database",
      "LastAvailableBandwidth": "0",
      "LastAvailableLatency": "0",
      "DateLastConnectionSuccesful": "2014-08-13T15:21:35.9663654+02:00"
    }
    

    And load it:

    UserSettings settings = JsonConvert.DeserializeObject<UserSettings>(File.ReadAllText("settings.json"))
    
    0 讨论(0)
提交回复
热议问题