GroupBy.cs 6.4 KB

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