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)

Location:
trunk/sources/HeuristicLab.Persistence/3.3/Default/Xml/Compact
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • 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        }
Note: See TracChangeset for help on using the changeset viewer.