GroupBy.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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.ObservableImpl
  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 IEqualityComparer<TKey> _comparer;
  16. public GroupBy(IObservable<TSource> source, Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer)
  17. {
  18. _source = source;
  19. _keySelector = keySelector;
  20. _elementSelector = elementSelector;
  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. _map = new Dictionary<TKey, ISubject<TElement>>(_parent._comparer);
  44. }
  45. public void OnNext(TSource value)
  46. {
  47. var key = default(TKey);
  48. try
  49. {
  50. key = _parent._keySelector(value);
  51. }
  52. catch (Exception exception)
  53. {
  54. Error(exception);
  55. return;
  56. }
  57. var fireNewMapEntry = false;
  58. var writer = default(ISubject<TElement>);
  59. try
  60. {
  61. //
  62. // Note: The box instruction in the IL will be erased by the JIT in case T is
  63. // a value type. In fact, the whole if block will go away and we'll end
  64. // up with nothing but the TryGetValue check below.
  65. //
  66. // // var fireNewMapEntry = false;
  67. // C:\Projects\Rx\Rx\Experimental\Main\Source\Rx\System.Reactive.Linq\Reactive\Linq\Observable\GroupBy.cs @ 67:
  68. // 000007fb`6d544b80 48c7452800000000 mov qword ptr [rbp+28h],0
  69. //
  70. // // var writer = default(ISubject<TElement>);
  71. // C:\Projects\Rx\Rx\Experimental\Main\Source\Rx\System.Reactive.Linq\Reactive\Linq\Observable\GroupBy.cs @ 66:
  72. // 000007fb`6d544b88 c6453400 mov byte ptr [rbp+34h],0
  73. //
  74. // // if (!_map.TryGetValue(key, out writer))
  75. // C:\Projects\Rx\Rx\Experimental\Main\Source\Rx\System.Reactive.Linq\Reactive\Linq\Observable\GroupBy.cs @ 86:
  76. // 000007fb`6d544b8c 488b4560 mov rax,qword ptr [rbp+60h]
  77. // ...
  78. //
  79. if (key == null)
  80. {
  81. if (_null == null)
  82. {
  83. _null = new Subject<TElement>();
  84. fireNewMapEntry = true;
  85. }
  86. writer = _null;
  87. }
  88. else
  89. {
  90. if (!_map.TryGetValue(key, out writer))
  91. {
  92. writer = new Subject<TElement>();
  93. _map.Add(key, writer);
  94. fireNewMapEntry = true;
  95. }
  96. }
  97. }
  98. catch (Exception exception)
  99. {
  100. Error(exception);
  101. return;
  102. }
  103. if (fireNewMapEntry)
  104. {
  105. var group = new GroupedObservable<TKey, TElement>(key, writer, _parent._refCountDisposable);
  106. _observer.OnNext(group);
  107. }
  108. var element = default(TElement);
  109. try
  110. {
  111. element = _parent._elementSelector(value);
  112. }
  113. catch (Exception exception)
  114. {
  115. Error(exception);
  116. return;
  117. }
  118. writer.OnNext(element);
  119. }
  120. public void OnError(Exception error)
  121. {
  122. Error(error);
  123. }
  124. public void OnCompleted()
  125. {
  126. if (_null != null)
  127. _null.OnCompleted();
  128. foreach (var w in _map.Values)
  129. w.OnCompleted();
  130. base._observer.OnCompleted();
  131. base.Dispose();
  132. }
  133. private void Error(Exception exception)
  134. {
  135. if (_null != null)
  136. _null.OnError(exception);
  137. foreach (var w in _map.Values)
  138. w.OnError(exception);
  139. base._observer.OnError(exception);
  140. base.Dispose();
  141. }
  142. }
  143. }
  144. }
  145. #endif