| 1 |
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. |
| 2 |
|
| 3 |
#if !NO_PERF |
| 4 |
using System; |
| 5 |
using System.Collections.Generic; |
| 6 |
using System.Linq; |
| 7 |
using System.Reactive.Disposables; |
| 8 |
using System.Reactive.Subjects; |
| 9 |
|
| 10 |
namespace System.Reactive.Linq.ObservableImpl |
| 11 |
{ |
| 12 |
class GroupBy<TSource, TKey, TElement> : Producer<IGroupedObservable<TKey, TElement>> |
| 13 |
{ |
| 14 |
private readonly IObservable<TSource> _source; |
| 15 |
private readonly Func<TSource, TKey> _keySelector; |
| 16 |
private readonly Func<TSource, TElement> _elementSelector; |
| 17 |
private readonly int? _capacity; |
| 18 |
private readonly IEqualityComparer<TKey> _comparer; |
| 19 |
|
| 20 |
public GroupBy(IObservable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, int? capacity, IEqualityComparer<TKey> comparer) |
| 21 |
{ |
| 22 |
_source = source; |
| 23 |
_keySelector = keySelector; |
| 24 |
_elementSelector = elementSelector; |
| 25 |
_capacity = capacity; |
| 26 |
_comparer = comparer; |
| 27 |
} |
| 28 |
|
| 29 |
private CompositeDisposable _groupDisposable; |
| 30 |
private RefCountDisposable _refCountDisposable; |
| 31 |
|
| 32 |
protected override IDisposable Run(IObserver<IGroupedObservable<TKey, TElement>> observer, IDisposable cancel, Action<IDisposable> setSink) |
| 33 |
{ |
| 34 |
_groupDisposable = new CompositeDisposable(); |
| 35 |
_refCountDisposable = new RefCountDisposable(_groupDisposable); |
| 36 |
|
| 37 |
var sink = new _(this, observer, cancel); |
| 38 |
setSink(sink); |
| 39 |
_groupDisposable.Add(_source.SubscribeSafe(sink)); |
| 40 |
|
| 41 |
return _refCountDisposable; |
| 42 |
} |
| 43 |
|
| 44 |
class _ : Sink<IGroupedObservable<TKey, TElement>>, IObserver<TSource> |
| 45 |
{ |
| 46 |
private readonly GroupBy<TSource, TKey, TElement> _parent; |
| 47 |
private readonly Dictionary<TKey, ISubject<TElement>> _map; |
| 48 |
private ISubject<TElement> _null; |
| 49 |
|
| 50 |
public _(GroupBy<TSource, TKey, TElement> parent, IObserver<IGroupedObservable<TKey, TElement>> observer, IDisposable cancel) |
| 51 |
: base(observer, cancel) |
| 52 |
{ |
| 53 |
_parent = parent; |
| 54 |
|
| 55 |
if (_parent._capacity.HasValue) |
| 56 |
{ |
| 57 |
_map = new Dictionary<TKey, ISubject<TElement>>(_parent._capacity.Value, _parent._comparer); |
| 58 |
} |
| 59 |
else |
| 60 |
{ |
| 61 |
_map = new Dictionary<TKey, ISubject<TElement>>(_parent._comparer); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
public void OnNext(TSource value) |
| 66 |
{ |
| 67 |
var key = default(TKey); |
| 68 |
try |
| 69 |
{ |
| 70 |
key = _parent._keySelector(value); |
| 71 |
} |
| 72 |
catch (Exception exception) |
| 73 |
{ |
| 74 |
Error(exception); |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
var fireNewMapEntry = false; |
| 79 |
var writer = default(ISubject<TElement>); |
| 80 |
try |
| 81 |
{ |
| 82 |
// |
| 83 |
// Note: The box instruction in the IL will be erased by the JIT in case T is |
| 84 |
// a value type. In fact, the whole if block will go away and we'll end |
| 85 |
// up with nothing but the TryGetValue check below. |
| 86 |
// |
| 87 |
// // var fireNewMapEntry = false; |
| 88 |
// C:\Projects\Rx\Rx\Experimental\Main\Source\Rx\System.Reactive.Linq\Reactive\Linq\Observable\GroupBy.cs @ 67: |
| 89 |
// 000007fb`6d544b80 48c7452800000000 mov qword ptr [rbp+28h],0 |
| 90 |
// |
| 91 |
// // var writer = default(ISubject<TElement>); |
| 92 |
// C:\Projects\Rx\Rx\Experimental\Main\Source\Rx\System.Reactive.Linq\Reactive\Linq\Observable\GroupBy.cs @ 66: |
| 93 |
// 000007fb`6d544b88 c6453400 mov byte ptr [rbp+34h],0 |
| 94 |
// |
| 95 |
// // if (!_map.TryGetValue(key, out writer)) |
| 96 |
// C:\Projects\Rx\Rx\Experimental\Main\Source\Rx\System.Reactive.Linq\Reactive\Linq\Observable\GroupBy.cs @ 86: |
| 97 |
// 000007fb`6d544b8c 488b4560 mov rax,qword ptr [rbp+60h] |
| 98 |
// ... |
| 99 |
// |
| 100 |
if (key == null) |
| 101 |
{ |
| 102 |
if (_null == null) |
| 103 |
{ |
| 104 |
_null = new Subject<TElement>(); |
| 105 |
fireNewMapEntry = true; |
| 106 |
} |
| 107 |
|
| 108 |
writer = _null; |
| 109 |
} |
| 110 |
else |
| 111 |
{ |
| 112 |
if (!_map.TryGetValue(key, out writer)) |
| 113 |
{ |
| 114 |
writer = new Subject<TElement>(); |
| 115 |
_map.Add(key, writer); |
| 116 |
fireNewMapEntry = true; |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
catch (Exception exception) |
| 121 |
{ |
| 122 |
Error(exception); |
| 123 |
return; |
| 124 |
} |
| 125 |
|
| 126 |
if (fireNewMapEntry) |
| 127 |
{ |
| 128 |
var group = new GroupedObservable<TKey, TElement>(key, writer, _parent._refCountDisposable); |
| 129 |
_observer.OnNext(group); |
| 130 |
} |
| 131 |
|
| 132 |
var element = default(TElement); |
| 133 |
try |
| 134 |
{ |
| 135 |
element = _parent._elementSelector(value); |
| 136 |
} |
| 137 |
catch (Exception exception) |
| 138 |
{ |
| 139 |
Error(exception); |
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
writer.OnNext(element); |
| 144 |
} |
| 145 |
|
| 146 |
public void OnError(Exception error) |
| 147 |
{ |
| 148 |
Error(error); |
| 149 |
} |
| 150 |
|
| 151 |
public void OnCompleted() |
| 152 |
{ |
| 153 |
if (_null != null) |
| 154 |
_null.OnCompleted(); |
| 155 |
|
| 156 |
foreach (var w in _map.Values) |
| 157 |
w.OnCompleted(); |
| 158 |
|
| 159 |
base._observer.OnCompleted(); |
| 160 |
base.Dispose(); |
| 161 |
} |
| 162 |
|
| 163 |
private void Error(Exception exception) |
| 164 |
{ |
| 165 |
if (_null != null) |
| 166 |
_null.OnError(exception); |
| 167 |
|
| 168 |
foreach (var w in _map.Values) |
| 169 |
w.OnError(exception); |
| 170 |
|
| 171 |
base._observer.OnError(exception); |
| 172 |
base.Dispose(); |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
#endif |