HeapProfiler.MainWindow.FilterHeapData C# (CSharp) Method

FilterHeapData() protected method

protected FilterHeapData ( HeapProfiler.HeapRecording instance, string filter ) : IEnumerator
instance HeapProfiler.HeapRecording
filter string
return IEnumerator
        protected IEnumerator<object> FilterHeapData(HeapRecording instance, string filter)
        {
            var result = new Dictionary<HeapSnapshotInfo, FilteredHeapSnapshotInfo>();

            var regex = MainWindow.FilterToRegex(filter);
            var functionNames = (from functionName in KnownFunctionNames
                                 where regex.IsMatch(functionName)
                                 select functionName).Distinct();

            using (var activity = Activities.AddItem("Filtering heap")) {
                var fFrameIDs = instance.Database.SymbolsByFunction.Find(functionNames);
                using (fFrameIDs)
                    yield return fFrameIDs;

                var frameIDs = new HashSet<UInt32>(
                    from key in fFrameIDs.Result select BitConverter.ToUInt32(key.Data.Array, key.Data.Offset)
                );

                var matchingTracebacks = new HashSet<UInt32>();

                for (int i = 0, c = instance.Snapshots.Count; i < c; i++) {
                    activity.Maximum = c;
                    activity.Progress = i;

                    var info = instance.Snapshots[i];

                    var fSnapshot = instance.GetSnapshot(info);
                    using (fSnapshot)
                        yield return fSnapshot;

                    var snapshot = fSnapshot.Result;
                    Func<HeapSnapshot.Traceback, bool> tracebackMatches = (traceback) => {
                        if (matchingTracebacks.Contains(traceback.ID))
                            return false;

                        var _f = traceback.Frames.Array;
                        for (int _i = 0, _c = traceback.Frames.Count, _o = traceback.Frames.Offset; _i < _c; _i++) {
                            if (frameIDs.Contains(_f[_i + _o]))
                                return true;
                        }

                        return false;
                    };

                    var fNewMatchingTracebacks = Future.RunInThread(() => new HashSet<UInt32>(
                        from traceback in snapshot.Tracebacks.AsParallel() where tracebackMatches(traceback) select traceback.ID
                    ));
                    yield return fNewMatchingTracebacks;

                    matchingTracebacks.UnionWith(fNewMatchingTracebacks.Result);

                    var fInfo = Future.RunInThread(() => new FilteredHeapSnapshotInfo(
                        (from heap in snapshot.Heaps.AsParallel() select (from alloc in heap.Allocations.AsParallel() where matchingTracebacks.Contains(alloc.TracebackID) select (long)alloc.Size).Sum()).Sum(),
                        (from heap in snapshot.Heaps.AsParallel() select (from alloc in heap.Allocations.AsParallel() where matchingTracebacks.Contains(alloc.TracebackID) select (long)alloc.Overhead).Sum()).Sum(),
                        (from heap in snapshot.Heaps.AsParallel() select (from alloc in heap.Allocations.AsParallel() where matchingTracebacks.Contains(alloc.TracebackID) select (long)(alloc.Size + alloc.Overhead)).Sum()).Sum(),
                        (from heap in snapshot.Heaps.AsParallel() select (from alloc in heap.Allocations.AsParallel() where matchingTracebacks.Contains(alloc.TracebackID) select alloc).Count()).Sum()
                    ));

                    yield return fInfo;
                    result[info] = fInfo.Result;

                    info.ReleaseStrongReference();
                }
            }

            CurrentFilterData = result;
            CurrentFilter = filter;
            PendingFilter = null;
            PendingFilterFuture = null;

            SnapshotTimeline.Invalidate();
        }
MainWindow