GroupBy.cs 6.5 KB

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