|
2 | 2 |
|
3 | 3 | require 'benchmark/ips' |
4 | 4 | require 'concurrent' |
| 5 | +require 'concurrent-edge' |
5 | 6 |
|
| 7 | +raise 'concurrent-ext not loaded' if Concurrent.on_cruby? && Concurrent.c_extensions_loaded? |
6 | 8 |
|
7 | 9 | scale = 1 |
8 | 10 | time = 10 * scale |
9 | 11 | warmup = 2 * scale |
10 | 12 | warmup *= 10 if Concurrent.on_jruby? |
11 | 13 |
|
12 | 14 | Benchmark.ips(time, warmup) do |x| |
13 | | - x.report('flat-old') { Concurrent::Promise.execute { 1 }.flat_map { |v| Concurrent::Promise.execute { v + 2 } }.value! } |
14 | | - x.report('flat-new') { Concurrent::Promises.future(:fast) { 1 }.then { |v| Concurrent::Promises.future(:fast) { v + 2 } }.flat.value! } |
| 15 | + x.report('flat-old') do |
| 16 | + Concurrent::Promise.execute { 1 }.flat_map { |v| Concurrent::Promise.execute { v + 2 } }.value! |
| 17 | + end |
| 18 | + x.report('flat-new') do |
| 19 | + Concurrent::Promises.future(:fast) { 1 }.then { |v| Concurrent::Promises.future(:fast) { v + 2 } }.flat.value! |
| 20 | + end |
15 | 21 | x.compare! |
16 | 22 | end |
17 | 23 |
|
18 | 24 | Benchmark.ips(time, warmup) do |x| |
19 | 25 | x.report('status-old') { f = Concurrent::Promise.execute { nil }; 100.times { f.complete? } } |
20 | | - x.report('status-new') { f = Concurrent::Promises.future(:fast) { nil }; 100.times { f.completed? } } |
| 26 | + x.report('status-new') { f = Concurrent::Promises.future(:fast) { nil }; 100.times { f.resolved? } } |
21 | 27 | x.compare! |
22 | 28 | end |
23 | 29 |
|
24 | 30 | Benchmark.ips(time, warmup) do |x| |
25 | 31 | of = Concurrent::Promise.execute { 1 } |
26 | | - nf = Concurrent::Promises.succeeded_future(1, :fast) |
| 32 | + nf = Concurrent::Promises.fulfilled_future(1, :fast) |
27 | 33 | x.report('value-old') { of.value! } |
28 | 34 | x.report('value-new') { nf.value! } |
29 | 35 | x.compare! |
30 | 36 | end |
31 | 37 |
|
32 | 38 | Benchmark.ips(time, warmup) do |x| |
33 | 39 | x.report('graph-old') do |
34 | | - head = Concurrent::Promise.execute { 1 } |
| 40 | + head = Concurrent::Promise.fulfill(1) |
35 | 41 | 10.times do |
36 | 42 | branch1 = head.then(&:succ) |
37 | 43 | branch2 = head.then(&:succ).then(&:succ) |
|
40 | 46 | head.value! |
41 | 47 | end |
42 | 48 | x.report('graph-new') do |
43 | | - head = Concurrent::Promises.succeeded_future(1, :fast) |
| 49 | + head = Concurrent::Promises.fulfilled_future(1, :fast) |
44 | 50 | 10.times do |
45 | 51 | branch1 = head.then(&:succ) |
46 | 52 | branch2 = head.then(&:succ).then(&:succ) |
|
52 | 58 | end |
53 | 59 |
|
54 | 60 | Benchmark.ips(time, warmup) do |x| |
55 | | - x.report('immediate-old') { Concurrent::Promise.execute { nil }.value! } |
56 | | - x.report('immediate-new') { Concurrent::Promises.succeeded_future(nil, :fast).value! } |
| 61 | + x.report('immediate-old') { Concurrent::Promise.fulfill(nil).value! } |
| 62 | + x.report('immediate-new') { Concurrent::Promises.fulfilled_future(nil, :fast).value! } |
57 | 63 | x.compare! |
58 | 64 | end |
59 | 65 |
|
60 | 66 | Benchmark.ips(time, warmup) do |x| |
61 | 67 | of = Concurrent::Promise.execute { 1 } |
62 | | - nf = Concurrent::Promises.succeeded_future(1, :fast) |
63 | | - x.report('then-old') { 100.times.reduce(of) { |nf, _| nf.then(&:succ) }.value! } |
64 | | - x.report('then-new') { 100.times.reduce(nf) { |nf, _| nf.then(&:succ) }.value! } |
| 68 | + nf = Concurrent::Promises.fulfilled_future(1, :fast) |
| 69 | + x.report('then-old') { 50.times.reduce(of) { |nf, _| nf.then(&:succ) }.value! } |
| 70 | + x.report('then-new') { 50.times.reduce(nf) { |nf, _| nf.then(&:succ) }.value! } |
65 | 71 | x.compare! |
66 | 72 | end |
0 commit comments