|
44 | 44 | */
|
45 | 45 | public class OperatorMerge<T> implements Operator<T, Observable<? extends T>> {
|
46 | 46 |
|
| 47 | + /* |
| 48 | + * benjchristensen => This class is complex and I'm not a fan of it despite writing it. I want to give some background |
| 49 | + * as to why for anyone who wants to try and help improve it. |
| 50 | + * |
| 51 | + * One of my first implementations that added backpressure support (Producer.request) was fairly elegant and used a simple |
| 52 | + * queue draining approach. It was simple to understand as all onNext were added to their queues, then a single winner |
| 53 | + * would drain the queues, similar to observeOn. It killed the Netflix API when I canaried it. There were two problems: |
| 54 | + * (1) performance and (2) object allocation overhead causing massive GC pressure. Remember that merge is one of the most |
| 55 | + * used operators (mostly due to flatmap) and is therefore critical to and a limiter of performance in any application. |
| 56 | + * |
| 57 | + * All subsequent work on this class and the various fast-paths and branches within it have been to achieve the needed functionality |
| 58 | + * while reducing or eliminating object allocation and keeping performance acceptable. |
| 59 | + * |
| 60 | + * This has meant adopting strategies such as: |
| 61 | + * |
| 62 | + * - ring buffers instead of growable queues |
| 63 | + * - object pooling |
| 64 | + * - skipping request logic when downstream does not need backpressure |
| 65 | + * - ScalarValueQueue for optimizing synchronous single-value Observables |
| 66 | + * - adopting data structures that use Unsafe (and gating them based on environment so non-Oracle JVMs still work) |
| 67 | + * |
| 68 | + * It has definitely increased the complexity and maintenance cost of this class, but the performance gains have been significant. |
| 69 | + * |
| 70 | + * The biggest cost of the increased complexity is concurrency bugs and reasoning through what's going on. |
| 71 | + * |
| 72 | + * I'd love to have contributions that improve this class, but keep in mind the performance and GC pressure. |
| 73 | + * The benchmarks I use are in the JMH OperatorMergePerf class. GC memory pressure is tested using Java Flight Recorder |
| 74 | + * to track object allocation. |
| 75 | + * |
| 76 | + * TODO There is still a known concurrency bug somewhere either in this class, in SubscriptionIndexedRingBuffer or their relationship. |
| 77 | + * See https://github.com/Netflix/RxJava/issues/1420 for more information on this. |
| 78 | + */ |
| 79 | + |
47 | 80 | public OperatorMerge() {
|
48 | 81 | this.delayErrors = false;
|
49 | 82 | }
|
|
0 commit comments