crossbeam.rs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 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::{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");
  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. }
  105. criterion_group!(bench, crossbeam);
  106. criterion_main!(bench);