|
25 | 25 | */
|
26 | 26 | public final class OnSubscribeRange implements OnSubscribe<Integer> {
|
27 | 27 |
|
28 |
| - private final int start; |
29 |
| - private final int end; |
| 28 | + private final int startIndex; |
| 29 | + private final int endIndex; |
30 | 30 |
|
31 | 31 | public OnSubscribeRange(int start, int end) {
|
32 |
| - this.start = start; |
33 |
| - this.end = end; |
| 32 | + this.startIndex = start; |
| 33 | + this.endIndex = end; |
34 | 34 | }
|
35 | 35 |
|
36 | 36 | @Override
|
37 |
| - public void call(final Subscriber<? super Integer> o) { |
38 |
| - o.setProducer(new RangeProducer(o, start, end)); |
| 37 | + public void call(final Subscriber<? super Integer> childSubscriber) { |
| 38 | + childSubscriber.setProducer(new RangeProducer(childSubscriber, startIndex, endIndex)); |
39 | 39 | }
|
40 | 40 |
|
41 | 41 | private static final class RangeProducer extends AtomicLong implements Producer {
|
42 | 42 | /** */
|
43 | 43 | private static final long serialVersionUID = 4114392207069098388L;
|
44 | 44 |
|
45 |
| - private final Subscriber<? super Integer> o; |
46 |
| - private final int end; |
47 |
| - private long index; |
| 45 | + private final Subscriber<? super Integer> childSubscriber; |
| 46 | + private final int endOfRange; |
| 47 | + private long currentIndex; |
48 | 48 |
|
49 |
| - RangeProducer(Subscriber<? super Integer> o, int start, int end) { |
50 |
| - this.o = o; |
51 |
| - this.index = start; |
52 |
| - this.end = end; |
| 49 | + RangeProducer(Subscriber<? super Integer> childSubscriber, int startIndex, int endIndex) { |
| 50 | + this.childSubscriber = childSubscriber; |
| 51 | + this.currentIndex = startIndex; |
| 52 | + this.endOfRange = endIndex; |
53 | 53 | }
|
54 | 54 |
|
55 | 55 | @Override
|
56 |
| - public void request(long n) { |
| 56 | + public void request(long requestedAmount) { |
57 | 57 | if (get() == Long.MAX_VALUE) {
|
58 | 58 | // already started with fast-path
|
59 | 59 | return;
|
60 | 60 | }
|
61 |
| - if (n == Long.MAX_VALUE && compareAndSet(0L, Long.MAX_VALUE)) { |
| 61 | + if (requestedAmount == Long.MAX_VALUE && compareAndSet(0L, Long.MAX_VALUE)) { |
62 | 62 | // fast-path without backpressure
|
63 | 63 | fastpath();
|
64 |
| - } else if (n > 0L) { |
65 |
| - long c = BackpressureUtils.getAndAddRequest(this, n); |
| 64 | + } else if (requestedAmount > 0L) { |
| 65 | + long c = BackpressureUtils.getAndAddRequest(this, requestedAmount); |
66 | 66 | if (c == 0L) {
|
67 | 67 | // backpressure is requested
|
68 |
| - slowpath(n); |
| 68 | + slowpath(requestedAmount); |
69 | 69 | }
|
70 | 70 | }
|
71 | 71 | }
|
72 | 72 |
|
73 | 73 | /**
|
74 |
| - * |
| 74 | + * Emits as many values as requested or remaining from the range, whichever is smaller. |
75 | 75 | */
|
76 |
| - void slowpath(long r) { |
77 |
| - long idx = index; |
78 |
| - while (true) { |
79 |
| - /* |
80 |
| - * This complicated logic is done to avoid touching the volatile `index` and `requested` values |
81 |
| - * during the loop itself. If they are touched during the loop the performance is impacted significantly. |
82 |
| - */ |
83 |
| - long fs = end - idx + 1; |
84 |
| - long e = Math.min(fs, r); |
85 |
| - final boolean complete = fs <= r; |
86 |
| - |
87 |
| - fs = e + idx; |
88 |
| - final Subscriber<? super Integer> o = this.o; |
| 76 | + void slowpath(long requestedAmount) { |
| 77 | + long emitted = 0L; |
| 78 | + long endIndex = endOfRange + 1L; |
| 79 | + long index = currentIndex; |
| 80 | + |
| 81 | + final Subscriber<? super Integer> childSubscriber = this.childSubscriber; |
| 82 | + |
| 83 | + for (;;) { |
89 | 84 |
|
90 |
| - for (long i = idx; i != fs; i++) { |
91 |
| - if (o.isUnsubscribed()) { |
| 85 | + while (emitted != requestedAmount && index != endIndex) { |
| 86 | + if (childSubscriber.isUnsubscribed()) { |
92 | 87 | return;
|
93 | 88 | }
|
94 |
| - o.onNext((int) i); |
| 89 | + |
| 90 | + childSubscriber.onNext((int)index); |
| 91 | + |
| 92 | + index++; |
| 93 | + emitted++; |
95 | 94 | }
|
96 | 95 |
|
97 |
| - if (complete) { |
98 |
| - if (o.isUnsubscribed()) { |
99 |
| - return; |
100 |
| - } |
101 |
| - o.onCompleted(); |
| 96 | + if (childSubscriber.isUnsubscribed()) { |
102 | 97 | return;
|
103 | 98 | }
|
104 | 99 |
|
105 |
| - idx = fs; |
106 |
| - index = fs; |
107 |
| - |
108 |
| - r = addAndGet(-e); |
109 |
| - if (r == 0L) { |
110 |
| - // we're done emitting the number requested so return |
| 100 | + if (index == endIndex) { |
| 101 | + childSubscriber.onCompleted(); |
111 | 102 | return;
|
112 | 103 | }
|
| 104 | + |
| 105 | + requestedAmount = get(); |
| 106 | + |
| 107 | + if (requestedAmount == emitted) { |
| 108 | + currentIndex = index; |
| 109 | + requestedAmount = addAndGet(-emitted); |
| 110 | + if (requestedAmount == 0L) { |
| 111 | + break; |
| 112 | + } |
| 113 | + emitted = 0L; |
| 114 | + } |
113 | 115 | }
|
114 | 116 | }
|
115 | 117 |
|
116 | 118 | /**
|
117 |
| - * |
| 119 | + * Emits all remaining values without decrementing the requested amount. |
118 | 120 | */
|
119 | 121 | void fastpath() {
|
120 |
| - final long end = this.end + 1L; |
121 |
| - final Subscriber<? super Integer> o = this.o; |
122 |
| - for (long i = index; i != end; i++) { |
123 |
| - if (o.isUnsubscribed()) { |
| 122 | + final long endIndex = this.endOfRange + 1L; |
| 123 | + final Subscriber<? super Integer> childSubscriber = this.childSubscriber; |
| 124 | + for (long index = currentIndex; index != endIndex; index++) { |
| 125 | + if (childSubscriber.isUnsubscribed()) { |
124 | 126 | return;
|
125 | 127 | }
|
126 |
| - o.onNext((int) i); |
| 128 | + childSubscriber.onNext((int) index); |
127 | 129 | }
|
128 |
| - if (!o.isUnsubscribed()) { |
129 |
| - o.onCompleted(); |
| 130 | + if (!childSubscriber.isUnsubscribed()) { |
| 131 | + childSubscriber.onCompleted(); |
130 | 132 | }
|
131 | 133 | }
|
132 | 134 | }
|
|
0 commit comments