GroupBy.cs 6.4 KB

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