crossbeam.rs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2025 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use criterion::{black_box, criterion_group, criterion_main, BatchSize, BenchmarkId, Criterion};
  19. use crossbeam_skiplist::SkipMap;
  20. use easy_parallel::Parallel;
  21. use rand::{rngs::OsRng, Rng};
  22. use std::{
  23. collections::HashMap,
  24. sync::{
  25. atomic::{AtomicBool, Ordering},
  26. Arc, Mutex,
  27. },
  28. };
  29. fn crossbeam(c: &mut Criterion) {
  30. let mut group = c.benchmark_group("crossbeam_vs_mutex-hashmap_writes");
  31. for k in 1..10 {
  32. let stopped = Arc::new(AtomicBool::new(false));
  33. let map = Arc::new(SkipMap::new());
  34. let stopped2 = stopped.clone();
  35. let map2 = map.clone();
  36. // Start n threads all doing continuous inserts until we tell them to stop
  37. let parallel_inserts = std::thread::spawn(move || {
  38. Parallel::new()
  39. .each(0..k, |_| {
  40. let stopped = stopped2.clone();
  41. let map = map2.clone();
  42. while !stopped.load(Ordering::Relaxed) {
  43. let key: usize = OsRng.gen();
  44. let val: usize = OsRng.gen();
  45. map.insert(key, val);
  46. }
  47. })
  48. .run();
  49. });
  50. group.bench_with_input(BenchmarkId::new("crossbeam", k), &k, |b, &_| {
  51. b.iter_batched(
  52. || {
  53. let key: usize = OsRng.gen();
  54. let val: usize = OsRng.gen();
  55. (key, val)
  56. },
  57. |(key, val)| {
  58. map.insert(key, val);
  59. },
  60. BatchSize::SmallInput,
  61. )
  62. });
  63. stopped.store(true, Ordering::Relaxed);
  64. parallel_inserts.join().unwrap();
  65. }
  66. // Now try normal Mutex hashmap
  67. // This is not an async Mutex, but async Mutexes are always slower than sync ones anyway
  68. // since they just implement an async interface on top of sync Mutexes.
  69. for k in 1..10 {
  70. let stopped = Arc::new(AtomicBool::new(false));
  71. let map = Arc::new(Mutex::new(HashMap::new()));
  72. let stopped2 = stopped.clone();
  73. let map2 = map.clone();
  74. // Start n threads all doing continuous inserts until we tell them to stop
  75. let parallel_inserts = std::thread::spawn(move || {
  76. Parallel::new()
  77. .each(0..k, |_| {
  78. let stopped = stopped2.clone();
  79. let map = map2.clone();
  80. while !stopped.load(Ordering::Relaxed) {
  81. let key: usize = OsRng.gen();
  82. let val: usize = OsRng.gen();
  83. map.lock().unwrap().insert(key, val);
  84. }
  85. })
  86. .run();
  87. });
  88. group.bench_with_input(BenchmarkId::new("mutex_hashmap", k), &k, |b, &_| {
  89. b.iter_batched(
  90. || {
  91. let key: usize = OsRng.gen();
  92. let val: usize = OsRng.gen();
  93. (key, val)
  94. },
  95. |(key, val)| {
  96. map.lock().unwrap().insert(key, val);
  97. },
  98. BatchSize::SmallInput,
  99. )
  100. });
  101. stopped.store(true, Ordering::Relaxed);
  102. parallel_inserts.join().unwrap();
  103. }
  104. group.finish();
  105. let mut group = c.benchmark_group("crossbeam_vs_mutex-hashmap_reads");
  106. for k in 1..10 {
  107. let stopped = Arc::new(AtomicBool::new(false));
  108. let map = Arc::new(SkipMap::new());
  109. let stopped2 = stopped.clone();
  110. let map2 = map.clone();
  111. // Start n threads all doing continuous inserts until we tell them to stop
  112. let parallel_inserts = std::thread::spawn(move || {
  113. Parallel::new()
  114. .each(0..k, |_| {
  115. let stopped = stopped2.clone();
  116. let map = map2.clone();
  117. while !stopped.load(Ordering::Relaxed) {
  118. let key: usize = OsRng.gen();
  119. let val: usize = OsRng.gen();
  120. map.insert(key, val);
  121. }
  122. })
  123. .run();
  124. });
  125. group.bench_with_input(BenchmarkId::new("crossbeam", k), &k, |b, &_| {
  126. b.iter(|| {
  127. for entry in map.iter().take(100) {
  128. let key = entry.key();
  129. let val = entry.value();
  130. black_box((key, val));
  131. }
  132. })
  133. });
  134. stopped.store(true, Ordering::Relaxed);
  135. parallel_inserts.join().unwrap();
  136. }
  137. // Now try normal Mutex hashmap
  138. // This is not an async Mutex, but async Mutexes are always slower than sync ones anyway
  139. // since they just implement an async interface on top of sync Mutexes.
  140. for k in 1..10 {
  141. let stopped = Arc::new(AtomicBool::new(false));
  142. let map = Arc::new(Mutex::new(HashMap::new()));
  143. let stopped2 = stopped.clone();
  144. let map2 = map.clone();
  145. // Start n threads all doing continuous inserts until we tell them to stop
  146. let parallel_inserts = std::thread::spawn(move || {
  147. Parallel::new()
  148. .each(0..k, |_| {
  149. let stopped = stopped2.clone();
  150. let map = map2.clone();
  151. while !stopped.load(Ordering::Relaxed) {
  152. let key: usize = OsRng.gen();
  153. let val: usize = OsRng.gen();
  154. map.lock().unwrap().insert(key, val);
  155. }
  156. })
  157. .run();
  158. });
  159. group.bench_with_input(BenchmarkId::new("mutex_hashmap", k), &k, |b, &_| {
  160. b.iter(|| {
  161. for (key, val) in map.lock().unwrap().iter().take(100) {
  162. // Do nothing
  163. black_box((key, val));
  164. }
  165. })
  166. });
  167. stopped.store(true, Ordering::Relaxed);
  168. parallel_inserts.join().unwrap();
  169. }
  170. }
  171. criterion_group!(bench, crossbeam);
  172. criterion_main!(bench);