From b1a24d31997160573028bed15aca4bb6b1454497 Mon Sep 17 00:00:00 2001 From: "Daniel.Bloom" Date: Sat, 24 Jul 2021 17:59:35 -0700 Subject: [PATCH 1/3] Use `array-macro` to simplify `AtomicCell` `LOCKS` --- crossbeam-utils/Cargo.toml | 1 + crossbeam-utils/src/atomic/atomic_cell.rs | 101 +--------------------- 2 files changed, 3 insertions(+), 99 deletions(-) diff --git a/crossbeam-utils/Cargo.toml b/crossbeam-utils/Cargo.toml index 16488cb52..424fe542f 100644 --- a/crossbeam-utils/Cargo.toml +++ b/crossbeam-utils/Cargo.toml @@ -34,6 +34,7 @@ nightly = [] [dependencies] cfg-if = "1" lazy_static = { version = "1.4.0", optional = true } +array-macro = "2.1.0" # Enable the use of loom for concurrency testing. # diff --git a/crossbeam-utils/src/atomic/atomic_cell.rs b/crossbeam-utils/src/atomic/atomic_cell.rs index 1a1c46460..7c405c3ac 100644 --- a/crossbeam-utils/src/atomic/atomic_cell.rs +++ b/crossbeam-utils/src/atomic/atomic_cell.rs @@ -3,6 +3,7 @@ #![allow(clippy::let_unit_value)] use crate::primitive::sync::atomic::{self, AtomicBool}; +use array_macro::array; use core::cell::UnsafeCell; use core::fmt; use core::mem; @@ -684,105 +685,7 @@ fn lock(addr: usize) -> &'static SeqLock { // In order to protect from such cases, we simply choose a large prime number for `LEN`. const LEN: usize = 97; - static LOCKS: [SeqLock; LEN] = [ - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - SeqLock::new(), - ]; + static LOCKS: [SeqLock; LEN] = array![_ => SeqLock::new(); LEN]; // If the modulus is a constant number, the compiler will use crazy math to transform this into // a sequence of cheap arithmetic operations rather than using the slow modulo instruction. From b552900e63904275837807cd09b3dd326216892b Mon Sep 17 00:00:00 2001 From: "Daniel.Bloom" Date: Sat, 24 Jul 2021 22:20:01 -0700 Subject: [PATCH 2/3] remove `array!`, use const to shorten array. --- crossbeam-utils/Cargo.toml | 1 - crossbeam-utils/src/atomic/atomic_cell.rs | 10 +++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/crossbeam-utils/Cargo.toml b/crossbeam-utils/Cargo.toml index 424fe542f..16488cb52 100644 --- a/crossbeam-utils/Cargo.toml +++ b/crossbeam-utils/Cargo.toml @@ -34,7 +34,6 @@ nightly = [] [dependencies] cfg-if = "1" lazy_static = { version = "1.4.0", optional = true } -array-macro = "2.1.0" # Enable the use of loom for concurrency testing. # diff --git a/crossbeam-utils/src/atomic/atomic_cell.rs b/crossbeam-utils/src/atomic/atomic_cell.rs index 7c405c3ac..84de9dfb6 100644 --- a/crossbeam-utils/src/atomic/atomic_cell.rs +++ b/crossbeam-utils/src/atomic/atomic_cell.rs @@ -3,7 +3,6 @@ #![allow(clippy::let_unit_value)] use crate::primitive::sync::atomic::{self, AtomicBool}; -use array_macro::array; use core::cell::UnsafeCell; use core::fmt; use core::mem; @@ -684,8 +683,13 @@ fn lock(addr: usize) -> &'static SeqLock { // stored at addresses that are multiples of 3. It'd be too bad if `LEN` was divisible by 3. // In order to protect from such cases, we simply choose a large prime number for `LEN`. const LEN: usize = 97; - - static LOCKS: [SeqLock; LEN] = array![_ => SeqLock::new(); LEN]; + const L: SeqLock = SeqLock::new(); + static LOCKS: [SeqLock; LEN] = [ + L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, + L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, + L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, + L, L, L, L, L, L, L, + ]; // If the modulus is a constant number, the compiler will use crazy math to transform this into // a sequence of cheap arithmetic operations rather than using the slow modulo instruction. From cb4db4cbd54427203c9358fe5b4dfb2994085461 Mon Sep 17 00:00:00 2001 From: "Daniel.Bloom" Date: Sat, 24 Jul 2021 22:35:11 -0700 Subject: [PATCH 3/3] clippy --- crossbeam-utils/src/atomic/atomic_cell.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crossbeam-utils/src/atomic/atomic_cell.rs b/crossbeam-utils/src/atomic/atomic_cell.rs index 84de9dfb6..55eb40108 100644 --- a/crossbeam-utils/src/atomic/atomic_cell.rs +++ b/crossbeam-utils/src/atomic/atomic_cell.rs @@ -683,6 +683,7 @@ fn lock(addr: usize) -> &'static SeqLock { // stored at addresses that are multiples of 3. It'd be too bad if `LEN` was divisible by 3. // In order to protect from such cases, we simply choose a large prime number for `LEN`. const LEN: usize = 97; + #[allow(clippy::declare_interior_mutable_const)] const L: SeqLock = SeqLock::new(); static LOCKS: [SeqLock; LEN] = [ L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L, L,