|
| 1 | +fn main() { |
| 2 | + #[cfg(panic = "abort")] |
| 3 | + println!("With panic=abort"); |
| 4 | + |
| 5 | + #[cfg(panic = "unwind")] |
| 6 | + println!("With panic=unwind"); |
| 7 | + |
| 8 | + timed("100_000_000 nops", many_nops); |
| 9 | + timed("100_000_000 calls", many_calls); |
| 10 | + timed(" 100_000 calls recursing 1000 frames", many_calls_recursive); |
| 11 | + timed( |
| 12 | + " 100_000 calls recursing 1000 frames with landingpad", |
| 13 | + many_calls_recursive_with_landingpad, |
| 14 | + ); |
| 15 | + #[cfg(panic = "unwind")] |
| 16 | + timed(" 10_000 throws catch unwinding few frame", many_throw_catch_few); |
| 17 | + #[cfg(panic = "unwind")] |
| 18 | + timed( |
| 19 | + " 10_000 throws catch unwinding few frame with landingpad", |
| 20 | + many_throw_catch_few_with_landingpad, |
| 21 | + ); |
| 22 | + #[cfg(panic = "unwind")] |
| 23 | + timed(" 1_000 throws catch unwinding 100 frames", many_throw_catch_many); |
| 24 | +} |
| 25 | + |
| 26 | +fn timed(name: &str, f: fn()) { |
| 27 | + let before = std::time::Instant::now(); |
| 28 | + f(); |
| 29 | + println!("{name} took {:?}", before.elapsed()); |
| 30 | +} |
| 31 | + |
| 32 | +struct DropMe; |
| 33 | + |
| 34 | +impl Drop for DropMe { |
| 35 | + #[inline(never)] |
| 36 | + fn drop(&mut self) {} |
| 37 | +} |
| 38 | + |
| 39 | +fn many_nops() { |
| 40 | + let mut i = 0; |
| 41 | + while i < 100_000_000 { |
| 42 | + // nop |
| 43 | + i += 1; |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +fn many_calls() { |
| 48 | + #[inline(never)] |
| 49 | + fn callee() {} |
| 50 | + |
| 51 | + let mut i = 0; |
| 52 | + while i < 100_000_000 { |
| 53 | + callee(); |
| 54 | + i += 1; |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +fn many_calls_recursive() { |
| 59 | + #[inline(never)] |
| 60 | + fn callee(i: u32) { |
| 61 | + if i > 0 { |
| 62 | + { |
| 63 | + let _a = DropMe; |
| 64 | + // Drop the DropMe outside of a landingpad |
| 65 | + } |
| 66 | + callee(i - 1); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + let mut i = 0; |
| 71 | + while i < 100_000 { |
| 72 | + callee(1000); |
| 73 | + i += 1; |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +fn many_calls_recursive_with_landingpad() { |
| 78 | + #[inline(never)] |
| 79 | + fn callee(i: u32) { |
| 80 | + if i > 0 { |
| 81 | + let _a = DropMe; |
| 82 | + callee(i - 1); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + let mut i = 0; |
| 87 | + while i < 100_000 { |
| 88 | + callee(1000); |
| 89 | + i += 1; |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +fn many_throw_catch_few() { |
| 94 | + #[inline(never)] |
| 95 | + fn callee() { |
| 96 | + std::panic::resume_unwind(Box::new(())); |
| 97 | + } |
| 98 | + |
| 99 | + let mut i = 0; |
| 100 | + while i < 10_000 { |
| 101 | + std::panic::catch_unwind(|| { |
| 102 | + { |
| 103 | + let _a = DropMe; |
| 104 | + // Drop the DropMe outside of a landingpad |
| 105 | + } |
| 106 | + callee(); |
| 107 | + }); |
| 108 | + i += 1; |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +fn many_throw_catch_few_with_landingpad() { |
| 113 | + #[inline(never)] |
| 114 | + fn callee() { |
| 115 | + std::panic::resume_unwind(Box::new(())); |
| 116 | + } |
| 117 | + |
| 118 | + let mut i = 0; |
| 119 | + while i < 10_000 { |
| 120 | + std::panic::catch_unwind(|| { |
| 121 | + let _a = DropMe; |
| 122 | + callee(); |
| 123 | + }); |
| 124 | + i += 1; |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +fn many_throw_catch_many() { |
| 129 | + #[inline(never)] |
| 130 | + fn callee(n: u32) { |
| 131 | + if n == 0 { |
| 132 | + std::panic::resume_unwind(Box::new(())); |
| 133 | + } else { |
| 134 | + callee(n - 1); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + let mut i = 0; |
| 139 | + while i < 1000 { |
| 140 | + std::panic::catch_unwind(|| { |
| 141 | + callee(100); |
| 142 | + }); |
| 143 | + i += 1; |
| 144 | + } |
| 145 | +} |
0 commit comments