Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/21/10 18:46:04 (14 years ago)
Author:
epitzer
Message:

Estimate or calculate StringBuffer sizes in advance, use iterator over string splits instead of arrays. (#1138)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/XmlGenerator.cs

    r3935 r3937  
    2323using System;
    2424using System.Text;
     25using System.Linq;
    2526using HeuristicLab.Persistence.Interfaces;
    2627using HeuristicLab.Persistence.Core;
     
    6364    protected enum NodeType { Start, End, Inline } ;
    6465
    65     protected static void AddXmlTagContent(StringBuilder sb, string name, Dictionary<string, object> attributes) {
     66    protected static void AddXmlTagContent(StringBuilder sb, string name, Dictionary<string, string> attributes) {
    6667      sb.Append(name);
    6768      foreach (var attribute in attributes) {
     
    7677    }
    7778
    78     protected static void AddXmlStartTag(StringBuilder sb, string name, Dictionary<string, object> attributes) {
     79    protected static int AttributeLength(Dictionary<string, string> attributes) {
     80      return attributes
     81        .Where(kvp => !string.IsNullOrEmpty(kvp.Key) && !string.IsNullOrEmpty(kvp.Value))
     82        .Select(kvp => kvp.Key.Length + kvp.Value.Length + 4).Sum();
     83    }
     84
     85    protected static void AddXmlStartTag(StringBuilder sb, string name, Dictionary<string, string> attributes) {
    7986      sb.Append('<');
    8087      AddXmlTagContent(sb, name, attributes);
     
    8289    }
    8390
    84     protected static void AddXmlInlineTag(StringBuilder sb, string name, Dictionary<string, object> attributes) {
     91    protected static void AddXmlInlineTag(StringBuilder sb, string name, Dictionary<string, string> attributes) {
    8592      sb.Append('<');
    8693      AddXmlTagContent(sb, name, attributes);
     
    94101    }
    95102
    96     protected string CreateNodeStart(string name, Dictionary<string, object> attributes) {
    97       StringBuilder sb = new StringBuilder();
     103    protected string CreateNodeStart(string name, Dictionary<string, string> attributes) {
     104      StringBuilder sb = new StringBuilder(prefix.Length + name.Length + 4
     105        + AttributeLength(attributes));
    98106      sb.Append(prefix);
    99107      Depth += 1;
     
    103111    }
    104112
     113    private static Dictionary<string, string> emptyDict = new Dictionary<string, string>();
    105114    protected string CreateNodeStart(string name) {
    106       return CreateNodeStart(name, new Dictionary<string, object>());
     115      return CreateNodeStart(name, emptyDict);
    107116    }
    108117
    109118    protected string CreateNodeEnd(string name) {
    110119      Depth -= 1;
    111       StringBuilder sb = new StringBuilder();
     120      StringBuilder sb = new StringBuilder(prefix.Length + name.Length + 5);
    112121      sb.Append(prefix);
    113122      AddXmlEndTag(sb, name);
     
    116125    }
    117126
    118     protected string CreateNode(string name, Dictionary<string, object> attributes) {
    119       StringBuilder sb = new StringBuilder();
     127    protected string CreateNode(string name, Dictionary<string, string> attributes) {
     128      StringBuilder sb = new StringBuilder(prefix.Length + name.Length + 5
     129        + AttributeLength(attributes));
    120130      sb.Append(prefix);
    121131      AddXmlInlineTag(sb, name, attributes);
     
    124134    }
    125135
    126     protected string CreateNode(string name, Dictionary<string, object> attributes, string content) {
     136    protected string CreateNode(string name, Dictionary<string, string> attributes, string content) {
    127137      StringBuilder sb = new StringBuilder();
    128138      sb.Append(prefix);
     
    139149    /// <returns>The token in serialized form.</returns>
    140150    protected override string Format(BeginToken beginToken) {
    141       var dict = new Dictionary<string, object> {
     151      var dict = new Dictionary<string, string> {
    142152          {"name", beginToken.Name},
    143           {"typeId", beginToken.TypeId},
    144           {"id", beginToken.Id}};
     153          {"typeId", beginToken.TypeId.ToString()},
     154          {"id", beginToken.Id.ToString()}};
    145155      AddTypeInfo(beginToken.TypeId, dict);
    146156      return CreateNodeStart(XmlStringConstants.COMPOSITE, dict);
     
    148158    }
    149159
    150     protected void AddTypeInfo(int typeId, Dictionary<string, object> dict) {
     160    protected void AddTypeInfo(int typeId, Dictionary<string, string> dict) {
    151161      if (lastTypeToken != null) {
    152162        if (typeId == lastTypeToken.Id) {
     
    175185    /// <returns>The token in serialized form.</returns>
    176186    protected override string Format(PrimitiveToken dataToken) {
    177       var dict = new Dictionary<string, object> {
    178             {"typeId", dataToken.TypeId},
     187      var dict = new Dictionary<string, string> {
     188            {"typeId", dataToken.TypeId.ToString()},
    179189            {"name", dataToken.Name},
    180             {"id", dataToken.Id}};
     190            {"id", dataToken.Id.ToString()}};
    181191      AddTypeInfo(dataToken.TypeId, dict);
    182192      return CreateNode(XmlStringConstants.PRIMITIVE, dict,
     
    191201    protected override string Format(ReferenceToken refToken) {
    192202      return CreateNode(XmlStringConstants.REFERENCE,
    193         new Dictionary<string, object> {
    194           {"ref", refToken.Id},
     203        new Dictionary<string, string> {
     204          {"ref", refToken.Id.ToString()},
    195205          {"name", refToken.Name}});
    196206    }
     
    203213    protected override string Format(NullReferenceToken nullRefToken) {
    204214      return CreateNode(XmlStringConstants.NULL,
    205         new Dictionary<string, object>{
     215        new Dictionary<string, string>{
    206216          {"name", nullRefToken.Name}});
    207217    }
     
    241251      try {
    242252        return CreateNode(XmlStringConstants.TYPE,
    243           new Dictionary<string, object> {
    244           {"id", lastTypeToken.Id},
     253          new Dictionary<string, string> {
     254          {"id", lastTypeToken.Id.ToString()},
    245255          {"typeName", lastTypeToken.TypeName },
    246256          {"serializer", lastTypeToken.Serializer }});
Note: See TracChangeset for help on using the changeset viewer.