Changeset 6205 for trunk/sources
- Timestamp:
- 05/17/11 00:09:59 (14 years ago)
- Location:
- trunk/sources
- Files:
-
- 1 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Common/3.3/ObjectExtensions.cs
r6201 r6205 21 21 22 22 using System; 23 using System.Collections; 23 24 using System.Collections.Generic; 25 using System.Collections.Specialized; 24 26 using System.Reflection; 25 using System. Collections;27 using System.Threading; 26 28 27 29 namespace HeuristicLab.Common { … … 35 37 /// Types not collected: 36 38 /// * System.Delegate 37 /// * System. EventHandler (+ System.EventHandler<T>)39 /// * System.Reflection.Pointer 38 40 /// * Primitives (Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, IntPtr, UIntPtr, Char, Double, Single) 39 41 /// * string, decimal 40 /// * Arrays of primitives (+ string, decimal)41 /// Types of which the fields are not further collected:42 /// * System.Type43 /// * System.Threading.ThreadLocal<T>42 /// * Arrays of types not collected 43 /// 44 /// Dictionaries and HashSets are treated specially, because it is cheaper to iterate over their keys and values 45 /// compared to traverse their internal data structures. 44 46 /// </summary> 45 47 private static void CollectObjectGraphObjects(this object obj, HashSet<object> objects) { 46 48 if (obj == null || objects.Contains(obj)) return; 47 if (obj is Delegate || obj is EventHandler) return;48 if (obj is Pointer) return;49 49 Type type = obj.GetType(); 50 if (type.IsSubclassOfRawGeneric(typeof(EventHandler<>))) return; 51 if (type.IsPrimitive || type == typeof(string) || type == typeof(decimal)) return; 52 if (type.HasElementType) { 53 Type elementType = type.GetElementType(); 54 if (elementType.IsPrimitive || elementType == typeof(string) || elementType == typeof(decimal)) return; 55 //TODO check all types 56 } 50 if (ExcludeType(type)) return; 51 if (type.HasElementType && ExcludeType(type.GetElementType())) return; 57 52 58 53 objects.Add(obj); 59 54 60 //if (typeof(Type).IsInstanceOfType(obj)) return; // avoid infinite recursion 61 //if (type.IsSubclassOfRawGeneric(typeof(ThreadLocal<>))) return; // avoid stack overflow when the field `ConcurrentStack<int> s_availableIndices` grows large 62 63 // performance critical to handle dictionaries in a special way 64 var dictionary = obj as IDictionary; 65 if (dictionary != null) { 55 if (type.IsSubclassOfRawGeneric(typeof(ThreadLocal<>))) return; // avoid stack overflow when the field `ConcurrentStack<int> s_availableIndices` too grows large 56 57 // performance critical to handle dictionaries, hashsets and hashtables in a special way 58 if (type.IsSubclassOfRawGeneric(typeof(Dictionary<,>)) || 59 type.IsSubclassOfRawGeneric(typeof(SortedDictionary<,>)) || 60 type.IsSubclassOfRawGeneric(typeof(SortedList<,>)) || 61 obj is SortedList || 62 obj is OrderedDictionary || 63 obj is ListDictionary || 64 obj is Hashtable) { 65 var dictionary = obj as IDictionary; 66 66 foreach (object value in dictionary.Keys) 67 67 CollectObjectGraphObjects(value, objects); … … 69 69 CollectObjectGraphObjects(value, objects); 70 70 return; 71 } else if (type.IsArray ) {72 var array = obj as Array;73 foreach ( object value in array)71 } else if (type.IsArray || type.IsSubclassOfRawGeneric(typeof(HashSet<>))) { 72 var enumerable = obj as IEnumerable; 73 foreach (var value in enumerable) 74 74 CollectObjectGraphObjects(value, objects); 75 75 return; … … 80 80 } 81 81 } 82 83 private static bool ExcludeType(Type type) { 84 return type.IsPrimitive || 85 type == typeof(string) || 86 type == typeof(decimal) || 87 typeof(Delegate).IsAssignableFrom(type) || 88 typeof(Pointer).IsAssignableFrom(type); 89 } 82 90 } 83 91 } -
trunk/sources/HeuristicLab/3.3/Tests/CollectObjectGraphTest.cs
r6201 r6205 24 24 using System.Diagnostics; 25 25 using System.Linq; 26 using System.Threading; 26 27 using HeuristicLab.Algorithms.GeneticAlgorithm; 27 28 using HeuristicLab.Common; 29 using HeuristicLab.Optimization; 28 30 using HeuristicLab.Persistence.Default.Xml; 31 using HeuristicLab.Problems.TestFunctions; 32 using HeuristicLab.Random; 33 using HeuristicLab.SequentialEngine; 29 34 using Microsoft.VisualStudio.TestTools.UnitTesting; 30 35 … … 82 87 TestContext.WriteLine(""); 83 88 } 89 90 [TestMethod] 91 public void AlgorithmExecutions() { 92 var random = new MersenneTwister(0); 93 var algs = new List<IAlgorithm>(); 94 95 Stopwatch sw = new Stopwatch(); 96 for (int i = 0; i < 100; i++) { 97 GeneticAlgorithm ga = new GeneticAlgorithm(); 98 ga.PopulationSize.Value = 5; 99 ga.MaximumGenerations.Value = 5; 100 ga.Engine = new SequentialEngine(); 101 ga.Problem = new SingleObjectiveTestFunctionProblem(); 102 103 sw.Start(); 104 algs.Add(ga); 105 106 var cancellationTokenSource = new CancellationTokenSource(); 107 ga.StartSync(cancellationTokenSource.Token); 108 sw.Stop(); 109 TestContext.WriteLine("{0}: {1} ", i, sw.Elapsed); 110 sw.Reset(); 111 } 112 } 84 113 } 85 114 } -
trunk/sources/HeuristicLab/3.3/Tests/HeuristicLab-3.3.Tests.csproj
r6201 r6205 122 122 </ItemGroup> 123 123 <ItemGroup> 124 <Compile Include="AlgorithmExtensions.cs" /> 124 125 <Compile Include="CloningConstructorTest.cs" /> 125 126 <Compile Include="CollectObjectGraphTest.cs" /> … … 559 560 <CopyToOutputDirectory>Always</CopyToOutputDirectory> 560 561 </None> 562 <None Include="GA_SymbReg2.hl"> 563 <CopyToOutputDirectory>Always</CopyToOutputDirectory> 564 </None> 561 565 </ItemGroup> 562 566 <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
Note: See TracChangeset
for help on using the changeset viewer.