|
| 1 | +/* |
| 2 | + * Copyright (c) 2016-present, RxJava Contributors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 | + * compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 | + * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See |
| 11 | + * the License for the specific language governing permissions and limitations under the License. |
| 12 | + */ |
| 13 | + |
| 14 | +package io.reactivex.rxjava4.streamable; |
| 15 | + |
| 16 | +import java.util.*; |
| 17 | +import java.util.concurrent.TimeUnit; |
| 18 | +import java.util.stream.*; |
| 19 | + |
| 20 | +import org.openjdk.jmh.annotations.*; |
| 21 | + |
| 22 | +import io.reactivex.rxjava4.core.Streamable; |
| 23 | + |
| 24 | +/// |
| 25 | +/// The map is one of the most used operator in the ecosystem so it must be fast and |
| 26 | +/// it must support operator fusion across itself. |
| 27 | +/// |
| 28 | +/// i9 275HX, 32GB LPDDR5 6400MT CL52, Windows 25H2, JDK 26.0.1 |
| 29 | +/// |
| 30 | +/// # 0. Baseline |
| 31 | +/// ``` |
| 32 | +/// Benchmark (times) Mode Cnt Score Error Units |
| 33 | +/// StreamableFilterPerf.basic 1 thrpt 5 26170031,056 ┬▒ 100892,180 ops/s |
| 34 | +/// StreamableFilterPerf.basic 10 thrpt 5 4174832,259 ┬▒ 118915,456 ops/s |
| 35 | +/// StreamableFilterPerf.basic 100 thrpt 5 485563,469 ┬▒ 6977,640 ops/s |
| 36 | +/// StreamableFilterPerf.basic 1000 thrpt 5 52859,526 ┬▒ 392,085 ops/s |
| 37 | +/// StreamableFilterPerf.basic 10000 thrpt 5 4757,976 ┬▒ 67,715 ops/s |
| 38 | +/// StreamableFilterPerf.basic 100000 thrpt 5 475,093 ┬▒ 6,846 ops/s |
| 39 | +/// StreamableFilterPerf.basic 1000000 thrpt 5 47,684 ┬▒ 1,866 ops/s |
| 40 | +/// StreamableFilterPerf.enumerated 1 thrpt 5 9351050,757 ┬▒ 267315,421 ops/s |
| 41 | +/// StreamableFilterPerf.enumerated 10 thrpt 5 3315310,614 ┬▒ 72388,028 ops/s |
| 42 | +/// StreamableFilterPerf.enumerated 100 thrpt 5 377616,877 ┬▒ 8096,437 ops/s |
| 43 | +/// StreamableFilterPerf.enumerated 1000 thrpt 5 41714,551 ┬▒ 813,698 ops/s |
| 44 | +/// StreamableFilterPerf.enumerated 10000 thrpt 5 4084,944 ┬▒ 64,690 ops/s |
| 45 | +/// StreamableFilterPerf.enumerated 100000 thrpt 5 367,438 ┬▒ 10,352 ops/s |
| 46 | +/// StreamableFilterPerf.enumerated 1000000 thrpt 5 37,380 ┬▒ 0,771 ops/s |
| 47 | +/// StreamableFilterPerf.indexed 1 thrpt 5 8582157,437 ┬▒ 215730,754 ops/s |
| 48 | +/// StreamableFilterPerf.indexed 10 thrpt 5 3370403,716 ┬▒ 44180,727 ops/s |
| 49 | +/// StreamableFilterPerf.indexed 100 thrpt 5 392725,429 ┬▒ 5825,883 ops/s |
| 50 | +/// StreamableFilterPerf.indexed 1000 thrpt 5 42956,064 ┬▒ 774,701 ops/s |
| 51 | +/// StreamableFilterPerf.indexed 10000 thrpt 5 4274,859 ┬▒ 48,781 ops/s |
| 52 | +/// StreamableFilterPerf.indexed 100000 thrpt 5 427,545 ┬▒ 6,204 ops/s |
| 53 | +/// StreamableFilterPerf.indexed 1000000 thrpt 5 38,761 ┬▒ 1,574 ops/s |
| 54 | +/// ``` |
| 55 | +/// |
| 56 | +/// # 1. Avoid whenComplete and tidy up the internals, better wip management |
| 57 | +/// |
| 58 | +/// 1 times regression -22%, 1 million case +63% |
| 59 | +/// |
| 60 | +/// ``` |
| 61 | +/// Benchmark (times) Mode Cnt Score Error Units |
| 62 | +/// StreamableFilterPerf.basic 1 thrpt 5 20450133,688 ┬▒ 292577,808 ops/s |
| 63 | +/// StreamableFilterPerf.basic 10 thrpt 5 7001560,283 ┬▒ 95757,407 ops/s |
| 64 | +/// StreamableFilterPerf.basic 100 thrpt 5 801954,196 ┬▒ 25583,620 ops/s |
| 65 | +/// StreamableFilterPerf.basic 1000 thrpt 5 81090,359 ┬▒ 1422,785 ops/s |
| 66 | +/// StreamableFilterPerf.basic 10000 thrpt 5 7861,791 ┬▒ 174,594 ops/s |
| 67 | +/// StreamableFilterPerf.basic 100000 thrpt 5 791,253 ┬▒ 10,498 ops/s |
| 68 | +/// StreamableFilterPerf.basic 1000000 thrpt 5 78,027 ┬▒ 1,068 ops/s |
| 69 | +/// StreamableFilterPerf.enumerated 1 thrpt 5 11758684,924 ┬▒ 308081,461 ops/s |
| 70 | +/// StreamableFilterPerf.enumerated 10 thrpt 5 4399154,841 ┬▒ 143732,147 ops/s |
| 71 | +/// StreamableFilterPerf.enumerated 100 thrpt 5 581006,390 ┬▒ 15226,107 ops/s |
| 72 | +/// StreamableFilterPerf.enumerated 1000 thrpt 5 52474,101 ┬▒ 900,684 ops/s |
| 73 | +/// StreamableFilterPerf.enumerated 10000 thrpt 5 5364,237 ┬▒ 55,065 ops/s |
| 74 | +/// StreamableFilterPerf.enumerated 100000 thrpt 5 575,620 ┬▒ 11,915 ops/s |
| 75 | +/// StreamableFilterPerf.enumerated 1000000 thrpt 5 53,243 ┬▒ 0,953 ops/s |
| 76 | +/// StreamableFilterPerf.indexed 1 thrpt 5 10378233,365 ┬▒ 320717,352 ops/s |
| 77 | +/// StreamableFilterPerf.indexed 10 thrpt 5 4450787,764 ┬▒ 16570,500 ops/s |
| 78 | +/// StreamableFilterPerf.indexed 100 thrpt 5 589007,872 ┬▒ 17229,304 ops/s |
| 79 | +/// StreamableFilterPerf.indexed 1000 thrpt 5 61222,108 ┬▒ 1699,621 ops/s |
| 80 | +/// StreamableFilterPerf.indexed 10000 thrpt 5 6181,191 ┬▒ 174,083 ops/s |
| 81 | +/// StreamableFilterPerf.indexed 100000 thrpt 5 582,777 ┬▒ 14,846 ops/s |
| 82 | +/// StreamableFilterPerf.indexed 1000000 thrpt 5 54,477 ┬▒ 1,366 ops/s |
| 83 | +/// ``` |
| 84 | +/// |
| 85 | +/// # 2. Synchronous bias via CAS-based state management, via Claude Fable suggestions |
| 86 | +/// |
| 87 | +/// times-1 +70% vs baseline, times million +240% vs baseline |
| 88 | +/// ``` |
| 89 | +/// Benchmark (times) Mode Cnt Score Error Units |
| 90 | +/// StreamableFilterPerf.basic 1 thrpt 5 44649205,329 ┬▒ 1318761,330 ops/s |
| 91 | +/// StreamableFilterPerf.basic 10 thrpt 5 14573791,114 ┬▒ 307959,449 ops/s |
| 92 | +/// StreamableFilterPerf.basic 100 thrpt 5 1704743,707 ┬▒ 27297,095 ops/s |
| 93 | +/// StreamableFilterPerf.basic 1000 thrpt 5 169935,412 ┬▒ 1894,944 ops/s |
| 94 | +/// StreamableFilterPerf.basic 10000 thrpt 5 16747,005 ┬▒ 149,331 ops/s |
| 95 | +/// StreamableFilterPerf.basic 100000 thrpt 5 1630,870 ┬▒ 39,286 ops/s |
| 96 | +/// StreamableFilterPerf.basic 1000000 thrpt 5 162,566 ┬▒ 4,036 ops/s |
| 97 | +/// StreamableFilterPerf.enumerated 1 thrpt 5 13220834,886 ┬▒ 157990,717 ops/s |
| 98 | +/// StreamableFilterPerf.enumerated 10 thrpt 5 6266989,179 ┬▒ 168298,570 ops/s |
| 99 | +/// StreamableFilterPerf.enumerated 100 thrpt 5 894912,487 ┬▒ 4979,227 ops/s |
| 100 | +/// StreamableFilterPerf.enumerated 1000 thrpt 5 89633,958 ┬▒ 1631,029 ops/s |
| 101 | +/// StreamableFilterPerf.enumerated 10000 thrpt 5 8973,714 ┬▒ 197,897 ops/s |
| 102 | +/// StreamableFilterPerf.enumerated 100000 thrpt 5 887,542 ┬▒ 12,736 ops/s |
| 103 | +/// StreamableFilterPerf.enumerated 1000000 thrpt 5 88,537 ┬▒ 0,994 ops/s |
| 104 | +/// StreamableFilterPerf.indexed 1 thrpt 5 11534659,877 ┬▒ 281862,191 ops/s |
| 105 | +/// StreamableFilterPerf.indexed 10 thrpt 5 6663720,805 ┬▒ 127958,849 ops/s |
| 106 | +/// StreamableFilterPerf.indexed 100 thrpt 5 973299,004 ┬▒ 35818,034 ops/s |
| 107 | +/// StreamableFilterPerf.indexed 1000 thrpt 5 98248,858 ┬▒ 1445,809 ops/s |
| 108 | +/// StreamableFilterPerf.indexed 10000 thrpt 5 9801,805 ┬▒ 183,524 ops/s |
| 109 | +/// StreamableFilterPerf.indexed 100000 thrpt 5 983,497 ┬▒ 23,374 ops/s |
| 110 | +/// StreamableFilterPerf.indexed 1000000 thrpt 5 97,605 ┬▒ 1,170 ops/s |
| 111 | +/// ``` |
| 112 | +/// |
| 113 | +/// # 3. Add EnumerableSource and DeferredEnumerableSource paths. |
| 114 | +/// |
| 115 | +/// +193% times 1 vs baseline, +607% times million vs baseline |
| 116 | +/// ``` |
| 117 | +/// Benchmark (times) Mode Cnt Score Error Units |
| 118 | +/// StreamableFilterPerf.basic 1 thrpt 5 44129567,500 ┬▒ 3907413,532 ops/s |
| 119 | +/// StreamableFilterPerf.basic 10 thrpt 5 15360293,459 ┬▒ 134453,730 ops/s |
| 120 | +/// StreamableFilterPerf.basic 100 thrpt 5 1811371,583 ┬▒ 32392,354 ops/s |
| 121 | +/// StreamableFilterPerf.basic 1000 thrpt 5 163322,591 ┬▒ 4944,111 ops/s |
| 122 | +/// StreamableFilterPerf.basic 10000 thrpt 5 16124,871 ┬▒ 578,538 ops/s |
| 123 | +/// StreamableFilterPerf.basic 100000 thrpt 5 1618,494 ┬▒ 68,714 ops/s |
| 124 | +/// StreamableFilterPerf.basic 1000000 thrpt 5 157,161 ┬▒ 1,401 ops/s |
| 125 | +/// StreamableFilterPerf.enumerated 1 thrpt 5 27449227,121 ┬▒ 639347,534 ops/s |
| 126 | +/// StreamableFilterPerf.enumerated 10 thrpt 5 43288619,923 ┬▒ 787987,036 ops/s |
| 127 | +/// StreamableFilterPerf.enumerated 100 thrpt 5 3013969,186 ┬▒ 83719,137 ops/s |
| 128 | +/// StreamableFilterPerf.enumerated 1000 thrpt 5 272763,930 ┬▒ 4591,739 ops/s |
| 129 | +/// StreamableFilterPerf.enumerated 10000 thrpt 5 27699,074 ┬▒ 591,416 ops/s |
| 130 | +/// StreamableFilterPerf.enumerated 100000 thrpt 5 2769,250 ┬▒ 75,561 ops/s |
| 131 | +/// StreamableFilterPerf.enumerated 1000000 thrpt 5 264,589 ┬▒ 6,425 ops/s |
| 132 | +/// StreamableFilterPerf.indexed 1 thrpt 5 11637720,830 ┬▒ 318248,700 ops/s |
| 133 | +/// StreamableFilterPerf.indexed 10 thrpt 5 6651438,800 ┬▒ 179861,878 ops/s |
| 134 | +/// StreamableFilterPerf.indexed 100 thrpt 5 978543,147 ┬▒ 34745,778 ops/s |
| 135 | +/// StreamableFilterPerf.indexed 1000 thrpt 5 97398,438 ┬▒ 1071,570 ops/s |
| 136 | +/// StreamableFilterPerf.indexed 10000 thrpt 5 9768,628 ┬▒ 221,269 ops/s |
| 137 | +/// StreamableFilterPerf.indexed 100000 thrpt 5 972,717 ┬▒ 31,004 ops/s |
| 138 | +/// StreamableFilterPerf.indexed 1000000 thrpt 5 95,972 ┬▒ 2,552 ops/s |
| 139 | +/// ``` |
| 140 | +@BenchmarkMode(Mode.Throughput) |
| 141 | +@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) |
| 142 | +@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) |
| 143 | +@OutputTimeUnit(TimeUnit.SECONDS) |
| 144 | +@Fork(value = 1) |
| 145 | +@State(Scope.Thread) |
| 146 | +public class StreamableFilterPerf { |
| 147 | + @Param({ "1", "10", "100", "1000", "10000", "100000", "1000000" }) |
| 148 | + public int times; |
| 149 | + |
| 150 | + Streamable<Integer> result; |
| 151 | + Streamable<Optional<Integer>> indexedMax; |
| 152 | + Streamable<Optional<Integer>> enumeratedMax; |
| 153 | + |
| 154 | + @Setup |
| 155 | + public void setup() { |
| 156 | + result = Streamable.range(1, times).filter(v -> (v & 1) != 0); |
| 157 | + indexedMax = result.collect(Collectors.maxBy(Comparator.naturalOrder())); |
| 158 | + enumeratedMax = Streamable.fromIterable(() -> IntStream.range(1, 1 + times).iterator()) |
| 159 | + .filter(v -> (v & 1) != 0) |
| 160 | + .collect(Collectors.maxBy(Comparator.naturalOrder())); |
| 161 | + } |
| 162 | + |
| 163 | + @Benchmark |
| 164 | + public Object basic() { |
| 165 | + return result.blockingLast(); |
| 166 | + } |
| 167 | + |
| 168 | + @Benchmark |
| 169 | + public Object indexed() { |
| 170 | + return indexedMax.blockingLast(); |
| 171 | + } |
| 172 | + |
| 173 | + @Benchmark |
| 174 | + public Object enumerated() { |
| 175 | + return enumeratedMax.blockingLast(); |
| 176 | + } |
| 177 | +} |
0 commit comments