diff --git a/foyer-common/Cargo.toml b/foyer-common/Cargo.toml index 16a298b5..0af9293f 100644 --- a/foyer-common/Cargo.toml +++ b/foyer-common/Cargo.toml @@ -15,6 +15,7 @@ normal = ["foyer-workspace-hack"] [dependencies] anyhow = "1.0" bytes = "1" +cfg-if = "1" foyer-workspace-hack = { version = "0.3", path = "../foyer-workspace-hack" } itertools = "0.12" parking_lot = { version = "0.12", features = ["arc_lock"] } diff --git a/foyer-common/src/bits.rs b/foyer-common/src/bits.rs index c7bc8aee..245e02be 100644 --- a/foyer-common/src/bits.rs +++ b/foyer-common/src/bits.rs @@ -31,7 +31,21 @@ use std::{ ops::{Add, BitAnd, Not, Sub}, }; -pub trait UnsignedTrait = Add +// TODO(MrCroxx): Use `trait_alias` after stable. +// pub trait UnsignedTrait = Add +// + Sub +// + BitAnd +// + Not +// + Sized +// + From +// + Eq +// + Debug +// + Display +// + Clone +// + Copy; + +pub trait Unsigned: + Add + Sub + BitAnd + Not @@ -41,11 +55,25 @@ pub trait UnsignedTrait = Add + Debug + Display + Clone - + Copy; - -pub trait Unsigned: UnsignedTrait {} + + Copy +{ +} -impl Unsigned for U {} +impl< + U: Add + + Sub + + BitAnd + + Not + + Sized + + From + + Eq + + Debug + + Display + + Clone + + Copy, + > Unsigned for U +{ +} #[inline(always)] pub fn is_pow2(v: U) -> bool { diff --git a/foyer-common/src/code.rs b/foyer-common/src/code.rs index dda8e00a..22d35d67 100644 --- a/foyer-common/src/code.rs +++ b/foyer-common/src/code.rs @@ -20,31 +20,58 @@ use paste::paste; pub type CodingError = anyhow::Error; pub type CodingResult = Result; -trait BufExt: Buf { - cfg_match! { - cfg(target_pointer_width = "16") => { +pub trait BufExt: Buf { + // TODO(MrCroxx): Use `cfg_match` after stable. + // cfg_match! { + // cfg(target_pointer_width = "16") => { + // fn get_usize(&mut self) -> usize { + // self.get_u16() as usize + // } + + // fn get_isize(&mut self) -> isize { + // self.get_i16() as isize + // } + // } + // cfg(target_pointer_width = "32") => { + // fn get_usize(&mut self) -> usize { + // self.get_u32() as usize + // } + + // fn get_isize(&mut self) -> isize { + // self.get_i32() as isize + // } + // } + // cfg(target_pointer_width = "64") => { + // fn get_usize(&mut self) -> usize { + // self.get_u64() as usize + // } + + // fn get_isize(&mut self) -> isize { + // self.get_i64() as isize + // } + // } + // } + cfg_if::cfg_if! { + if #[cfg(target_pointer_width = "16")] { fn get_usize(&mut self) -> usize { self.get_u16() as usize } - fn get_isize(&mut self) -> isize { self.get_i16() as isize } } - cfg(target_pointer_width = "32") => { + else if #[cfg(target_pointer_width = "32")] { fn get_usize(&mut self) -> usize { self.get_u32() as usize } - fn get_isize(&mut self) -> isize { self.get_i32() as isize } } - cfg(target_pointer_width = "64") => { + else if #[cfg(target_pointer_width = "64")] { fn get_usize(&mut self) -> usize { self.get_u64() as usize } - fn get_isize(&mut self) -> isize { self.get_i64() as isize } @@ -54,31 +81,58 @@ trait BufExt: Buf { impl BufExt for T {} -trait BufMutExt: BufMut { - cfg_match! { - cfg(target_pointer_width = "16") => { +pub trait BufMutExt: BufMut { + // TODO(MrCroxx): Use `cfg_match` after stable. + // cfg_match! { + // cfg(target_pointer_width = "16") => { + // fn put_usize(&mut self, v: usize) { + // self.put_u16(v as u16); + // } + + // fn put_isize(&mut self, v: isize) { + // self.put_i16(v as i16); + // } + // } + // cfg(target_pointer_width = "32") => { + // fn put_usize(&mut self, v: usize) { + // self.put_u32(v as u32); + // } + + // fn put_isize(&mut self, v: isize) { + // self.put_i32(v as i32); + // } + // } + // cfg(target_pointer_width = "64") => { + // fn put_usize(&mut self, v: usize) { + // self.put_u64(v as u64); + // } + + // fn put_isize(&mut self, v: isize) { + // self.put_i64(v as i64); + // } + // } + // } + cfg_if::cfg_if! { + if #[cfg(target_pointer_width = "16")] { fn put_usize(&mut self, v: usize) { self.put_u16(v as u16); } - fn put_isize(&mut self, v: isize) { self.put_i16(v as i16); } } - cfg(target_pointer_width = "32") => { + else if #[cfg(target_pointer_width = "32")] { fn put_usize(&mut self, v: usize) { self.put_u32(v as u32); } - fn put_isize(&mut self, v: isize) { self.put_i32(v as i32); } } - cfg(target_pointer_width = "64") => { + else if #[cfg(target_pointer_width = "64")] { fn put_usize(&mut self, v: usize) { self.put_u64(v as u64); } - fn put_isize(&mut self, v: isize) { self.put_i64(v as i64); } @@ -100,7 +154,9 @@ pub trait Cursor: Send + Sync + 'static + std::io::Read + std::fmt::Debug { pub trait Key: Sized + Send + Sync + 'static + std::hash::Hash + Eq + PartialEq + Ord + PartialOrd + std::fmt::Debug + Clone { - type Cursor: Cursor = UnimplementedCursor; + // TODO(MrCroxx): Restore this after `associated_type_defaults` is stable. + // type Cursor: Cursor = UnimplementedCursor; + type Cursor: Cursor; /// memory weight fn weight(&self) -> usize { @@ -126,7 +182,9 @@ pub trait Key: // TODO(MrCroxx): use `expect` after `lint_reasons` is stable. #[allow(unused_variables)] pub trait Value: Sized + Send + Sync + 'static + std::fmt::Debug + Clone { - type Cursor: Cursor = UnimplementedCursor; + // TODO(MrCroxx): Restore this after `associated_type_defaults` is stable. + // type Cursor: Cursor = UnimplementedCursor; + type Cursor: Cursor; /// memory weight fn weight(&self) -> usize { diff --git a/foyer-common/src/lib.rs b/foyer-common/src/lib.rs index a9541ec9..735c635e 100644 --- a/foyer-common/src/lib.rs +++ b/foyer-common/src/lib.rs @@ -12,10 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#![feature(trait_alias)] #![feature(bound_map)] -#![feature(associated_type_defaults)] -#![feature(cfg_match)] #![cfg_attr(coverage_nightly, feature(coverage_attribute))] pub mod async_queue; diff --git a/foyer-common/src/range.rs b/foyer-common/src/range.rs index bb107413..0e887b3f 100644 --- a/foyer-common/src/range.rs +++ b/foyer-common/src/range.rs @@ -48,10 +48,14 @@ mod private { use private::ZeroOne; -pub trait Idx = - PartialOrd + Add + Sub + Clone + Copy + Send + Sync + 'static + ZeroOne; - -pub trait RangeBoundsExt: RangeBounds { +// TODO(MrCroxx): Use `trait_alias` after stable. +// pub trait Idx = +// PartialOrd + Add + Sub + Clone + Copy + Send + Sync + 'static + ZeroOne; + +pub trait RangeBoundsExt< + T: PartialOrd + Add + Sub + Clone + Copy + Send + Sync + 'static + ZeroOne, +>: RangeBounds +{ fn start(&self) -> Option { match self.start_bound() { Bound::Included(v) => Some(*v), @@ -107,4 +111,9 @@ pub trait RangeBoundsExt: RangeBounds { } } -impl> RangeBoundsExt for RB {} +impl< + T: PartialOrd + Add + Sub + Clone + Copy + Send + Sync + 'static + ZeroOne, + RB: RangeBounds, + > RangeBoundsExt for RB +{ +} diff --git a/foyer-experimental/Cargo.toml b/foyer-experimental/Cargo.toml index 538389c6..48bdc161 100644 --- a/foyer-experimental/Cargo.toml +++ b/foyer-experimental/Cargo.toml @@ -16,7 +16,9 @@ normal = ["foyer-workspace-hack"] anyhow = "1.0" bytes = "1" crossbeam = { version = "0.8", features = ["std", "crossbeam-channel"] } +foyer-common = { version = "0.4", path = "../foyer-common" } foyer-workspace-hack = { version = "0.3", path = "../foyer-workspace-hack" } +lazy_static = "1" parking_lot = { version = "0.12", features = ["arc_lock"] } paste = "1.0" prometheus = "0.13" @@ -27,7 +29,6 @@ tracing = "0.1" [dev-dependencies] bytesize = "1" clap = { version = "4", features = ["derive"] } -foyer-common = { version = "0.4", path = "../foyer-common" } hdrhistogram = "7" itertools = "0.12" rand = "0.8.5" diff --git a/foyer-experimental/src/error.rs b/foyer-experimental/src/error.rs index cb50531c..383930e4 100644 --- a/foyer-experimental/src/error.rs +++ b/foyer-experimental/src/error.rs @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::backtrace::Backtrace; - #[derive(thiserror::Error, Debug)] #[error("{0}")] pub struct Error(Box); @@ -23,7 +21,8 @@ pub struct Error(Box); struct ErrorInner { #[from] source: ErrorKind, - backtrace: Backtrace, + // TODO(MrCroxx): Restore this after `error_generic_member_access` is stable. + // backtrace: Backtrace, } #[derive(thiserror::Error, Debug)] diff --git a/foyer-experimental/src/lib.rs b/foyer-experimental/src/lib.rs index ec80631e..c8a1dd61 100644 --- a/foyer-experimental/src/lib.rs +++ b/foyer-experimental/src/lib.rs @@ -12,11 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#![feature(cfg_match)] -#![feature(error_generic_member_access)] -#![feature(lazy_cell)] - -pub mod buf; pub mod error; pub mod metrics; pub mod notify; diff --git a/foyer-experimental/src/metrics.rs b/foyer-experimental/src/metrics.rs index 5b07df73..8a15e4ec 100644 --- a/foyer-experimental/src/metrics.rs +++ b/foyer-experimental/src/metrics.rs @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::sync::{LazyLock, OnceLock}; +use std::sync::OnceLock; use prometheus::{ core::{AtomicU64, GenericGauge, GenericGaugeVec}, @@ -55,7 +55,12 @@ pub fn get_metrics_registry() -> &'static Registry { REGISTRY.get_or_init(|| prometheus::default_registry().clone()) } -pub static METRICS: LazyLock = LazyLock::new(GlobalMetrics::default); +// TODO(MrCroxx): Use `LazyLock` after `lazy_cell` is stable. +// pub static METRICS: LazyLock = LazyLock::new(GlobalMetrics::default); + +lazy_static::lazy_static! { + pub static ref METRICS: GlobalMetrics = GlobalMetrics::default(); +} #[derive(Debug)] pub struct GlobalMetrics { diff --git a/foyer-experimental/src/wal.rs b/foyer-experimental/src/wal.rs index 517e63b6..0d63b4e4 100644 --- a/foyer-experimental/src/wal.rs +++ b/foyer-experimental/src/wal.rs @@ -25,15 +25,11 @@ use std::{ use bytes::{Buf, BufMut}; use crossbeam::channel; +use foyer_common::code::{BufExt, BufMutExt}; use parking_lot::{Condvar, Mutex}; use tokio::sync::oneshot; -use crate::{ - asyncify, - buf::{BufExt, BufMutExt}, - error::Result, - metrics::Metrics, -}; +use crate::{asyncify, error::Result, metrics::Metrics}; pub trait HashValue: Send + Sync + 'static + Eq + std::fmt::Debug { fn size() -> usize; diff --git a/foyer-intrusive/src/eviction/mod.rs b/foyer-intrusive/src/eviction/mod.rs index 614e1db6..263e24a9 100644 --- a/foyer-intrusive/src/eviction/mod.rs +++ b/foyer-intrusive/src/eviction/mod.rs @@ -16,11 +16,9 @@ use std::fmt::Debug; use crate::core::adapter::Adapter; -pub trait Config = Send + Sync + 'static + Debug + Clone; - pub trait EvictionPolicy: Send + Sync + Debug + 'static { type Adapter: Adapter; - type Config: Config; + type Config: Send + Sync + 'static + Debug + Clone; fn new(config: Self::Config) -> Self; diff --git a/foyer-intrusive/src/lib.rs b/foyer-intrusive/src/lib.rs index 6d6fbc28..038d1c2c 100644 --- a/foyer-intrusive/src/lib.rs +++ b/foyer-intrusive/src/lib.rs @@ -12,8 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#![feature(ptr_metadata)] -#![feature(trait_alias)] #![feature(offset_of)] // TODO(MrCroxx): use `expect` after `lint_reasons` is stable. #![allow(clippy::new_without_default)] diff --git a/foyer-storage/Cargo.toml b/foyer-storage/Cargo.toml index b6069b8e..4aa1bb87 100644 --- a/foyer-storage/Cargo.toml +++ b/foyer-storage/Cargo.toml @@ -22,6 +22,7 @@ foyer-intrusive = { version = "0.3", path = "../foyer-intrusive" } foyer-workspace-hack = { version = "0.3", path = "../foyer-workspace-hack" } futures = "0.3" itertools = "0.12" +lazy_static = "1" libc = "0.2" lz4 = "1.24" memoffset = "0.9" diff --git a/foyer-storage/src/buffer.rs b/foyer-storage/src/buffer.rs index 8090ef7e..0f600b65 100644 --- a/foyer-storage/src/buffer.rs +++ b/foyer-storage/src/buffer.rs @@ -350,7 +350,7 @@ mod tests { let res = buffer.write(entry).await; let entry = match res { - Err(BufferError::NeedRotate(entry)) => Box::into_inner(entry), + Err(BufferError::NeedRotate(entry)) => *entry, _ => panic!("should be not enough error"), }; @@ -397,7 +397,7 @@ mod tests { let res = buffer.write(entry).await; let entry = match res { - Err(BufferError::NeedRotate(entry)) => Box::into_inner(entry), + Err(BufferError::NeedRotate(entry)) => *entry, _ => panic!("should be not enough error"), }; diff --git a/foyer-storage/src/device/allocator.rs b/foyer-storage/src/device/allocator.rs index c848417b..3919334c 100644 --- a/foyer-storage/src/device/allocator.rs +++ b/foyer-storage/src/device/allocator.rs @@ -52,14 +52,14 @@ mod tests { let allocator = AlignedAllocator::new(ALIGN); let mut buf: Vec = Vec::with_capacity_in(ALIGN * 8, &allocator); - bits::assert_aligned(ALIGN, buf.as_ptr().addr()); + bits::assert_aligned(ALIGN, buf.as_ptr() as _); buf.extend_from_slice(&[b'x'; ALIGN * 8]); - bits::assert_aligned(ALIGN, buf.as_ptr().addr()); + bits::assert_aligned(ALIGN, buf.as_ptr() as _); assert_eq!(buf, [b'x'; ALIGN * 8]); buf.extend_from_slice(&[b'x'; ALIGN * 8]); - bits::assert_aligned(ALIGN, buf.as_ptr().addr()); + bits::assert_aligned(ALIGN, buf.as_ptr() as _); assert_eq!(buf, [b'x'; ALIGN * 16]) } } diff --git a/foyer-storage/src/device/mod.rs b/foyer-storage/src/device/mod.rs index fc52bc38..1cae8ba6 100644 --- a/foyer-storage/src/device/mod.rs +++ b/foyer-storage/src/device/mod.rs @@ -24,10 +24,21 @@ use futures::Future; use crate::region::RegionId; -pub trait BufferAllocator = Allocator + Clone + Send + Sync + 'static + Debug; -pub trait IoBuf = AsRef<[u8]> + Send + Sync + 'static + Debug; -pub trait IoBufMut = AsRef<[u8]> + AsMut<[u8]> + Send + Sync + 'static + Debug; -pub trait IoRange = RangeBoundsExt + Sized + Send + Sync + 'static + Debug; +// TODO(MrCroxx): Use `trait_alias` after stable. + +// pub trait BufferAllocator = Allocator + Clone + Send + Sync + 'static + Debug; +// pub trait IoBuf = AsRef<[u8]> + Send + Sync + 'static + Debug; +// pub trait IoBufMut = AsRef<[u8]> + AsMut<[u8]> + Send + Sync + 'static + Debug; +// pub trait IoRange = RangeBoundsExt + Sized + Send + Sync + 'static + Debug; + +pub trait BufferAllocator: Allocator + Clone + Send + Sync + 'static + Debug {} +impl BufferAllocator for T {} +pub trait IoBuf: AsRef<[u8]> + Send + Sync + 'static + Debug {} +impl + Send + Sync + 'static + Debug> IoBuf for T {} +pub trait IoBufMut: AsRef<[u8]> + AsMut<[u8]> + Send + Sync + 'static + Debug {} +impl + AsMut<[u8]> + Send + Sync + 'static + Debug> IoBufMut for T {} +pub trait IoRange: RangeBoundsExt + Sized + Send + Sync + 'static + Debug {} +impl + Sized + Send + Sync + 'static + Debug> IoRange for T {} pub trait Device: Sized + Clone + Send + Sync + 'static + Debug { type IoBufferAllocator: BufferAllocator; diff --git a/foyer-storage/src/flusher.rs b/foyer-storage/src/flusher.rs index 6b19a329..84697268 100644 --- a/foyer-storage/src/flusher.rs +++ b/foyer-storage/src/flusher.rs @@ -144,7 +144,7 @@ where let old_region = self.buffer.region(); let entry = match self.buffer.write(entry).await { - Err(BufferError::NeedRotate(entry)) => Box::into_inner(entry), + Err(BufferError::NeedRotate(entry)) => *entry, Ok(entries) => return self.update_catalog(entries).await, Err(e) => return Err(e.into()), }; diff --git a/foyer-storage/src/lib.rs b/foyer-storage/src/lib.rs index 7e2cf9a1..eadf2d09 100644 --- a/foyer-storage/src/lib.rs +++ b/foyer-storage/src/lib.rs @@ -13,13 +13,6 @@ // limitations under the License. #![feature(allocator_api)] -#![feature(strict_provenance)] -#![feature(trait_alias)] -#![feature(get_mut_unchecked)] -#![feature(error_generic_member_access)] -#![feature(lazy_cell)] -#![feature(box_into_inner)] -#![feature(try_trait_v2)] #![feature(offset_of)] pub mod admission; diff --git a/foyer-storage/src/metrics.rs b/foyer-storage/src/metrics.rs index 2512fc2c..a8334435 100644 --- a/foyer-storage/src/metrics.rs +++ b/foyer-storage/src/metrics.rs @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -use std::sync::{LazyLock, OnceLock}; +use std::sync::OnceLock; use prometheus::{ core::{AtomicU64, GenericGauge, GenericGaugeVec}, @@ -54,8 +54,13 @@ pub fn get_metrics_registry() -> &'static Registry { REGISTRY.get_or_init(|| prometheus::default_registry().clone()) } -/// Multiple foyer instance will share the same global metrics with different label `foyer` name. -pub static METRICS: LazyLock = LazyLock::new(GlobalMetrics::default); +// TODO(MrCroxx): Use `LazyLock` after `lazy_cell` is stable. +// /// Multiple foyer instance will share the same global metrics with different label `foyer` name. +// pub static METRICS: LazyLock = LazyLock::new(GlobalMetrics::default); + +lazy_static::lazy_static! { + pub static ref METRICS: GlobalMetrics = GlobalMetrics::default(); +} #[derive(Debug)] pub struct GlobalMetrics { diff --git a/foyer-storage/src/storage.rs b/foyer-storage/src/storage.rs index 3909731a..0e9a70d6 100644 --- a/foyer-storage/src/storage.rs +++ b/foyer-storage/src/storage.rs @@ -19,7 +19,11 @@ use futures::Future; use crate::{compress::Compression, error::Result}; -pub trait FetchValueFuture = Future> + Send + 'static; +// TODO(MrCroxx): Use `trait_alias` after stable. +// pub trait FetchValueFuture = Future> + Send + 'static; + +pub trait FetchValueFuture: Future> + Send + 'static {} +impl> + Send + 'static> FetchValueFuture for T {} pub trait StorageWriter: Send + Sync + Debug { type Key: Key; diff --git a/foyer/src/lib.rs b/foyer/src/lib.rs index 191d0932..32d97e4d 100644 --- a/foyer/src/lib.rs +++ b/foyer/src/lib.rs @@ -12,9 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#![feature(trait_alias)] -#![feature(pattern)] - pub use foyer_common as common; pub use foyer_intrusive as intrusive; pub use foyer_memory as memory;