|
| 1 | +//! The multi-threading abstractions used by [`Hasher::update_with_join`]. |
| 2 | +//! |
| 3 | +//! Different implementations of the `Join` trait determine whether |
| 4 | +//! [`Hasher::update_with_join`] performs multi-threading on sufficiently large |
| 5 | +//! inputs. The `SerialJoin` implementation is single-threaded, and the |
| 6 | +//! `RayonJoin` implementation (gated by the `rayon` feature) is |
| 7 | +//! multi-threaded. Interfaces other than [`Hasher::update_with_join`], like |
| 8 | +//! [`hash`] and [`Hasher::update`], always use `SerialJoin` internally. |
| 9 | +//! |
| 10 | +//! The `Join` trait is an almost exact copy of the [`rayon::join`] API, and |
| 11 | +//! `RayonJoin` is the only non-trivial implementation provided. The only |
| 12 | +//! difference between the function signature in the `Join` trait and the |
| 13 | +//! underlying one in Rayon, is that the trait method includes two length |
| 14 | +//! parameters. This gives an implementation the option of e.g. setting a |
| 15 | +//! subtree size threshold below which it keeps splits on the same thread. |
| 16 | +//! However, neither of the two provided implementations currently makes use of |
| 17 | +//! those parameters. Note that in Rayon, the very first `join` call is more |
| 18 | +//! expensive than subsequent calls, because it moves work from the calling |
| 19 | +//! thread into the thread pool. That makes a coarse-grained input length |
| 20 | +//! threshold in the caller more effective than a fine-grained subtree size |
| 21 | +//! threshold after the implementation has already started recursing. |
| 22 | +//! |
| 23 | +//! # Example |
| 24 | +//! |
| 25 | +//! ``` |
| 26 | +//! // Hash a large input using multi-threading. Note that multi-threading |
| 27 | +//! // comes with some overhead, and it can actually hurt performance for small |
| 28 | +//! // inputs. The meaning of "small" varies, however, depending on the |
| 29 | +//! // platform and the number of threads. (On x86_64, the cutoff tends to be |
| 30 | +//! // around 128 KiB.) You should benchmark your own use case to see whether |
| 31 | +//! // multi-threading helps. |
| 32 | +//! # #[cfg(feature = "rayon")] |
| 33 | +//! # { |
| 34 | +//! # fn some_large_input() -> &'static [u8] { b"foo" } |
| 35 | +//! let input: &[u8] = some_large_input(); |
| 36 | +//! let mut hasher = blake3::Hasher::new(); |
| 37 | +//! hasher.update_with_join::<blake3::join::RayonJoin>(input); |
| 38 | +//! let hash = hasher.finalize(); |
| 39 | +//! # } |
| 40 | +//! ``` |
| 41 | +//! |
| 42 | +//! [`Hasher::update_with_join`]: ../struct.Hasher.html#method.update_with_join |
| 43 | +//! [`Hasher::update`]: ../struct.Hasher.html#method.update |
| 44 | +//! [`hash`]: ../fn.hash.html |
| 45 | +//! [`rayon::join`]: https://docs.rs/rayon/1.3.0/rayon/fn.join.html |
| 46 | +
|
| 47 | +/// The trait that abstracts over single-threaded and multi-threaded recursion. |
| 48 | +pub trait Join { |
| 49 | + fn join<A, B, RA, RB>(oper_a: A, oper_b: B, len_a: usize, len_b: usize) -> (RA, RB) |
| 50 | + where |
| 51 | + A: FnOnce() -> RA + Send, |
| 52 | + B: FnOnce() -> RB + Send, |
| 53 | + RA: Send, |
| 54 | + RB: Send; |
| 55 | +} |
| 56 | + |
| 57 | +/// The trivial, serial implementation of `Join`. The left and right sides are |
| 58 | +/// executed one after the other, on the calling thread. The standalone hashing |
| 59 | +/// functions and the `Hasher::update` method use this implementation |
| 60 | +/// internally. |
| 61 | +pub enum SerialJoin {} |
| 62 | + |
| 63 | +impl Join for SerialJoin { |
| 64 | + #[inline] |
| 65 | + fn join<A, B, RA, RB>(oper_a: A, oper_b: B, _len_a: usize, _len_b: usize) -> (RA, RB) |
| 66 | + where |
| 67 | + A: FnOnce() -> RA + Send, |
| 68 | + B: FnOnce() -> RB + Send, |
| 69 | + RA: Send, |
| 70 | + RB: Send, |
| 71 | + { |
| 72 | + (oper_a(), oper_b()) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +/// The Rayon-based implementation of `Join`. The left and right sides are |
| 77 | +/// executed on the Rayon thread pool, potentially in parallel. This |
| 78 | +/// implementation is gated by the `rayon` feature, which is off by default. |
| 79 | +#[cfg(feature = "rayon")] |
| 80 | +pub enum RayonJoin {} |
| 81 | + |
| 82 | +#[cfg(feature = "rayon")] |
| 83 | +impl Join for RayonJoin { |
| 84 | + #[inline] |
| 85 | + fn join<A, B, RA, RB>(oper_a: A, oper_b: B, _len_a: usize, _len_b: usize) -> (RA, RB) |
| 86 | + where |
| 87 | + A: FnOnce() -> RA + Send, |
| 88 | + B: FnOnce() -> RB + Send, |
| 89 | + RA: Send, |
| 90 | + RB: Send, |
| 91 | + { |
| 92 | + rayon::join(oper_a, oper_b) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +#[cfg(test)] |
| 97 | +mod test { |
| 98 | + use super::*; |
| 99 | + |
| 100 | + #[test] |
| 101 | + fn test_serial_join() { |
| 102 | + let oper_a = || 1 + 1; |
| 103 | + let oper_b = || 2 + 2; |
| 104 | + assert_eq!((2, 4), SerialJoin::join(oper_a, oper_b, 3, 4)); |
| 105 | + } |
| 106 | + |
| 107 | + #[test] |
| 108 | + #[cfg(feature = "rayon")] |
| 109 | + fn test_rayon_join() { |
| 110 | + let oper_a = || 1 + 1; |
| 111 | + let oper_b = || 2 + 2; |
| 112 | + assert_eq!((2, 4), RayonJoin::join(oper_a, oper_b, 3, 4)); |
| 113 | + } |
| 114 | +} |
0 commit comments