Free cookie consent management tool by TermsFeed Policy Generator

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

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

Location:
trunk/sources/HeuristicLab.Persistence/3.3
Files:
1 added
10 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Persistence/3.3/Core/TypeMapping.cs

    r3743 r3937  
    6565    /// <returns>A dictionary containing all properties
    6666    /// and values of this instance.</returns>
    67     public Dictionary<string, object> GetDict() {
    68       return new Dictionary<string, object> {
    69         {"id", Id},
     67    public Dictionary<string, string> GetDict() {
     68      return new Dictionary<string, string> {
     69        {"id", Id.ToString()},
    7070        {"typeName", TypeName},
    7171        {"serializer", Serializer}};
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/CompactNumberArray2StringSerializer.cs

    r3742 r3937  
    2121
    2222using System;
     23using System.Linq;
    2324using HeuristicLab.Persistence.Interfaces;
    2425using HeuristicLab.Persistence.Core;
     
    2829using System.Text;
    2930using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     31using HeuristicLab.Persistence.Auxiliary;
    3032
    3133namespace HeuristicLab.Persistence.Default.CompositeSerializers {
     
    5860      int[] lengths = new int[a.Rank];
    5961      int[] lowerBounds = new int[a.Rank];
    60       StringBuilder sb = new StringBuilder();
     62      StringBuilder sb = new StringBuilder(a.Rank * 3);
    6163      sb.Append(a.Rank).Append(';');
     64      int capacity = 1;
    6265      for (int i = 0; i < a.Rank; i++) {
    6366        sb.Append(a.GetLength(i)).Append(';');
    6467        lengths[i] = a.GetLength(i);
     68        capacity *= lengths[i];
    6569      }
     70      sb.EnsureCapacity(capacity * 3);
    6671      for (int i = 0; i < a.Rank; i++) {
    6772        sb.Append(a.GetLowerBound(i)).Append(';');
     
    9297        var tagIter = metaInfo.GetEnumerator();
    9398        tagIter.MoveNext();
    94         var valueIter = ((string)tagIter.Current.Value)
    95           .Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
    96           .GetEnumerator();
     99        var valueIter = ((string)tagIter.Current.Value).GetSplitEnumerator(';');
    97100        valueIter.MoveNext();
    98101        int rank = int.Parse((string)valueIter.Current);
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/NumberEnumerable2StringSerializer.cs

    r3742 r3937  
    9393    }
    9494
     95    private static object[] emptyArgs = new object[0];
    9596    public IEnumerable<Tag> Decompose(object obj) {
    9697      Type type = obj.GetType();
     
    102103          iMap.InterfaceMethods,
    103104          enumerable.GetMethod("GetEnumerator"))];
    104       object[] empty = new object[] { };
    105       object genericEnumerator = getEnumeratorMethod.Invoke(obj, empty);
     105      object genericEnumerator = getEnumeratorMethod.Invoke(obj, emptyArgs);
    106106      MethodInfo moveNextMethod = genericEnumerator.GetType().GetMethod("MoveNext");
    107107      PropertyInfo currentProperty = genericEnumerator.GetType().GetProperty("Current");
    108108      StringBuilder sb = new StringBuilder();
    109       while ((bool)moveNextMethod.Invoke(genericEnumerator, empty))
     109      while ((bool)moveNextMethod.Invoke(genericEnumerator, emptyArgs))
    110110        sb.Append(
    111111          numberConverter.Format(
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Compact/DoubleList2XmlSerializer.cs

    r3913 r3937  
    2727using HeuristicLab.Persistence.Default.Xml.Primitive;
    2828using System.Text;
     29using HeuristicLab.Persistence.Auxiliary;
    2930
    3031namespace HeuristicLab.Persistence.Default.Xml.Compact {
     
    3233  internal sealed class DoubleList2XmlSerializer : CompactXmlSerializerBase<List<double>> {
    3334
    34     private static readonly char[] separators = new char[] { ';' };
    35 
    3635    public override XmlString Format(List<double> list) {
    37       StringBuilder sb = new StringBuilder();
     36      StringBuilder sb = new StringBuilder(list.Count * 3);
    3837      foreach (var d in list) {
    3938        sb.Append(Double2XmlSerializer.FormatG17(d)).Append(';');
     
    4443    public override List<double> Parse(XmlString data) {
    4544      try {
    46         var values = data.Data.Split(separators, StringSplitOptions.RemoveEmptyEntries);
    47         List<double> list = new List<double>(values.Length);
    48         foreach (var value in values) {
     45        List<double> list = new List<double>();
     46        foreach (var value in data.Data.EnumerateSplit(';')) {
    4947          list.Add(Double2XmlSerializer.ParseG17(value));
    5048        }
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Compact/NumberArray2XmlSerializerBase.cs

    r3742 r3937  
    2626using HeuristicLab.Persistence.Core;
    2727using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     28using HeuristicLab.Persistence.Auxiliary;
    2829
    2930namespace HeuristicLab.Persistence.Default.Xml.Compact {
     
    3233  internal abstract class NumberArray2XmlSerializerBase<T> : CompactXmlSerializerBase<T> where T : class {
    3334
    34     protected virtual string Separator { get { return ";"; } }
     35    protected virtual char Separator { get { return ';'; } }
    3536    protected abstract string FormatValue(object o);
    3637    protected abstract object ParseValue(string o);
     
    4041      int[] lengths = new int[a.Rank];
    4142      int[] lowerBounds = new int[a.Rank];
    42       StringBuilder sb = new StringBuilder();
     43      StringBuilder sb = new StringBuilder(a.Rank * 3);
    4344      sb.Append(a.Rank);
     45      int capacity = 1;
    4446      for (int i = 0; i < a.Rank; i++) {
    4547        sb.Append(Separator);
    4648        sb.Append(a.GetLength(i));
    4749        lengths[i] = a.GetLength(i);
     50        capacity *= lengths[i];
    4851      }
     52      sb.EnsureCapacity(capacity * 3);
    4953      for (int i = 0; i < a.Rank; i++) {
    5054        sb.Append(Separator);
     
    7175    public override T Parse(XmlString x) {
    7276      try {
    73         IEnumerator values =
    74           x.Data.Split(new[] { Separator },
    75           StringSplitOptions.RemoveEmptyEntries).GetEnumerator();
     77        IEnumerator values = x.Data.GetSplitEnumerator(Separator);
    7678        values.MoveNext();
    7779        int rank = int.Parse((string)values.Current);
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Compact/NumberEnumeration2XmlSerializerBase.cs

    r3742 r3937  
    2626using HeuristicLab.Persistence.Core;
    2727using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     28using HeuristicLab.Persistence.Auxiliary;
    2829
    2930namespace HeuristicLab.Persistence.Default.Xml.Compact {
     
    3233  internal abstract class NumberEnumeration2XmlSerializerBase<T> : CompactXmlSerializerBase<T> where T : IEnumerable {
    3334
    34     protected virtual string Separator { get { return ";"; } }
     35    protected virtual char Separator { get { return ';'; } }
    3536    protected abstract void Add(IEnumerable enumeration, object o);
    3637    protected abstract IEnumerable Instantiate();
     
    5051      try {
    5152        IEnumerable enumeration = Instantiate();
    52         string[] values = x.Data.Split(new[] { Separator }, StringSplitOptions.RemoveEmptyEntries);
    53         foreach (var value in values) {
     53        foreach (var value in x.Data.EnumerateSplit(Separator)) {
    5454          Add(enumeration, ParseValue(value));
    5555        }
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/EasyXmlGenerator.cs

    r3935 r3937  
    4949    /// <returns>The token in serialized form.</returns>
    5050    protected override string Format(BeginToken beginToken) {
    51       var dict = new Dictionary<string, object> {
     51      var dict = new Dictionary<string, string> {
    5252          {"name", beginToken.Name},
    53           {"id", beginToken.Id}};
    54       return CreateNodeStart(typeCache[beginToken.TypeId], dict);       
     53          {"id", beginToken.Id.ToString()}};
     54      return CreateNodeStart(typeCache[beginToken.TypeId], dict);
    5555    }
    5656
     
    7070    /// <returns>The token in serialized form.</returns>
    7171    protected override string Format(PrimitiveToken dataToken) {
    72       var dict = new Dictionary<string, object> {
     72      var dict = new Dictionary<string, string> {
    7373            {"name", dataToken.Name},
    74             {"id", dataToken.Id}};
     74            {"id", dataToken.Id.ToString()}};
    7575      return CreateNode(typeCache[dataToken.TypeId], dict,
    7676        ((XmlString)dataToken.SerialData).Data);
     
    8484    protected override string Format(ReferenceToken refToken) {
    8585      return CreateNode(refToken.Id.ToString(),
    86         new Dictionary<string, object> {
     86        new Dictionary<string, string> {
    8787          {"name", refToken.Name}});
    8888    }
     
    9595    protected override string Format(NullReferenceToken nullRefToken) {
    9696      return CreateNode(XmlStringConstants.NULL,
    97         new Dictionary<string, object>{
     97        new Dictionary<string, string>{
    9898          {"name", nullRefToken.Name}});
    9999    }
  • 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 }});
  • trunk/sources/HeuristicLab.Persistence/3.3/HeuristicLab.Persistence-3.3.csproj

    r3935 r3937  
    9898  </ItemGroup>
    9999  <ItemGroup>
     100    <Compile Include="Auxiliary\StringExtensions.cs" />
    100101    <Compile Include="Auxiliary\TypeLoader.cs" />
    101102    <Compile Include="Auxiliary\TypeNameParser.cs" />
  • trunk/sources/HeuristicLab.Persistence/3.3/Tests/UseCases.cs

    r3935 r3937  
    10651065    }
    10661066
     1067    [TestMethod]
     1068    public void TestStringSplit() {
     1069      string s = "1.2;2.3;3.4;;;4.9";
     1070      var l = s.EnumerateSplit(';').ToList();
     1071      Assert.AreEqual("1.2", l[0]);
     1072      Assert.AreEqual("2.3", l[1]);
     1073      Assert.AreEqual("3.4", l[2]);
     1074      Assert.AreEqual("4.9", l[3]);
     1075    }
     1076
    10671077
    10681078    [ClassInitialize]
Note: See TracChangeset for help on using the changeset viewer.