Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: simplify some generic type definition #85

Merged
merged 3 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions foyer-intrusive/src/eviction/fifo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,48 +315,44 @@ where
unsafe impl<A> Send for Fifo<A>
where
A: Adapter<Link = FifoLink> + PriorityAdapter<Link = FifoLink>,

<<A as Adapter>::PointerOps as PointerOps>::Pointer: Clone,
{
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Is the empty line between automated added by IDE? Or some format setting needs to be enabled? I'll enable it by default.

unsafe impl<A> Sync for Fifo<A>
where
A: Adapter<Link = FifoLink> + PriorityAdapter<Link = FifoLink>,

<<A as Adapter>::PointerOps as PointerOps>::Pointer: Clone,
{
}

unsafe impl Send for FifoLink {}

unsafe impl Sync for FifoLink {}

unsafe impl<'a, A> Send for FifoIter<'a, A>
where
A: Adapter<Link = FifoLink> + PriorityAdapter<Link = FifoLink>,

<<A as Adapter>::PointerOps as PointerOps>::Pointer: Clone,
{
}

unsafe impl<'a, A> Sync for FifoIter<'a, A>
where
A: Adapter<Link = FifoLink> + PriorityAdapter<Link = FifoLink>,

<<A as Adapter>::PointerOps as PointerOps>::Pointer: Clone,
{
}

impl<A> EvictionPolicy<A> for Fifo<A>
impl<A> EvictionPolicy for Fifo<A>
where
A: Adapter<Link = FifoLink> + PriorityAdapter<Link = FifoLink>,

<<A as Adapter>::PointerOps as PointerOps>::Pointer: Clone,
{
type Link = FifoLink;
type Adapter = A;

type Config = FifoConfig;

type E<'e> = FifoIter<'e, A>;

fn new(config: Self::Config) -> Self {
Self::new(config)
}
Expand All @@ -380,7 +376,7 @@ where
self.len()
}

fn iter(&self) -> Self::E<'_> {
fn iter(&self) -> impl Iterator<Item = &'_ <A::PointerOps as PointerOps>::Pointer> {
self.iter()
}
}
Expand Down
8 changes: 3 additions & 5 deletions foyer-intrusive/src/eviction/lfu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,17 +523,15 @@ where
{
}

impl<A> EvictionPolicy<A> for Lfu<A>
impl<A> EvictionPolicy for Lfu<A>
where
A: Adapter<Link = LfuLink> + KeyAdapter<Link = LfuLink>,
<<A as Adapter>::PointerOps as PointerOps>::Pointer: Clone,
{
type Link = LfuLink;
type Adapter = A;

type Config = LfuConfig;

type E<'e> = LfuIter<'e, A>;

fn new(config: Self::Config) -> Self {
Self::new(config)
}
Expand All @@ -557,7 +555,7 @@ where
self.len()
}

fn iter(&self) -> Self::E<'_> {
fn iter(&self) -> impl Iterator<Item = &'_ <A::PointerOps as PointerOps>::Pointer> {
self.iter()
}
}
Expand Down
11 changes: 6 additions & 5 deletions foyer-intrusive/src/eviction/lru.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ where
<<A as Adapter>::PointerOps as PointerOps>::Pointer: Clone,
{
}

unsafe impl<A> Sync for Lru<A>
where
A: Adapter<Link = LruLink>,
Expand All @@ -360,6 +361,7 @@ where
}

unsafe impl Send for LruLink {}

unsafe impl Sync for LruLink {}

unsafe impl<'a, A> Send for LruIter<'a, A>
Expand All @@ -368,24 +370,23 @@ where
<<A as Adapter>::PointerOps as PointerOps>::Pointer: Clone,
{
}

unsafe impl<'a, A> Sync for LruIter<'a, A>
where
A: Adapter<Link = LruLink>,
<<A as Adapter>::PointerOps as PointerOps>::Pointer: Clone,
{
}

impl<A> EvictionPolicy<A> for Lru<A>
impl<A> EvictionPolicy for Lru<A>
where
A: Adapter<Link = LruLink>,
<<A as Adapter>::PointerOps as PointerOps>::Pointer: Clone,
{
type Link = LruLink;
type Adapter = A;

type Config = LruConfig;

type E<'e> = LruIter<'e, A>;

fn new(config: Self::Config) -> Self {
Self::new(config)
}
Expand All @@ -409,7 +410,7 @@ where
self.len()
}

fn iter(&self) -> Self::E<'_> {
fn iter(&self) -> impl Iterator<Item = &'_ <A::PointerOps as PointerOps>::Pointer> {
self.iter()
}
}
Expand Down
45 changes: 26 additions & 19 deletions foyer-intrusive/src/eviction/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,55 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::core::{
adapter::{Adapter, Link},
pointer::PointerOps,
};
use crate::core::{adapter::Adapter, pointer::PointerOps};

use std::fmt::Debug;

pub trait Config = Send + Sync + 'static + Debug + Clone;

pub trait EvictionPolicy<A>: Send + Sync + 'static
where
A: Adapter<Link = Self::Link>,
<<A as Adapter>::PointerOps as PointerOps>::Pointer: Clone,
{
type Link: Link;
pub trait EvictionPolicy: Send + Sync + 'static {
type Adapter: Adapter;
type Config: Config;
type E<'e>: Iterator<Item = &'e <A::PointerOps as PointerOps>::Pointer>;

fn new(config: Self::Config) -> Self;

fn insert(&mut self, ptr: <A::PointerOps as PointerOps>::Pointer);
fn insert(&mut self, ptr: <<Self::Adapter as Adapter>::PointerOps as PointerOps>::Pointer);

fn remove(
&mut self,
ptr: &<A::PointerOps as PointerOps>::Pointer,
) -> <A::PointerOps as PointerOps>::Pointer;
ptr: &<<Self::Adapter as Adapter>::PointerOps as PointerOps>::Pointer,
) -> <<Self::Adapter as Adapter>::PointerOps as PointerOps>::Pointer;

fn access(&mut self, ptr: &<A::PointerOps as PointerOps>::Pointer);
fn access(&mut self, ptr: &<<Self::Adapter as Adapter>::PointerOps as PointerOps>::Pointer);

fn len(&self) -> usize;

fn is_empty(&self) -> bool {
self.len() == 0
}

fn iter(&self) -> Self::E<'_>;
fn iter(
&self,
) -> impl Iterator<Item = &'_ <<Self::Adapter as Adapter>::PointerOps as PointerOps>::Pointer> + '_;
}

pub trait EvictionPolicyExt: EvictionPolicy {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea!

fn push(&mut self, ptr: <<Self::Adapter as Adapter>::PointerOps as PointerOps>::Pointer);

fn pop(&mut self) -> Option<<<Self::Adapter as Adapter>::PointerOps as PointerOps>::Pointer>;

fn push(&mut self, ptr: <A::PointerOps as PointerOps>::Pointer) {
fn peek(&self) -> Option<&<<Self::Adapter as Adapter>::PointerOps as PointerOps>::Pointer>;
}

impl<E: EvictionPolicy> EvictionPolicyExt for E
where
<<E::Adapter as Adapter>::PointerOps as PointerOps>::Pointer: Clone,
{
fn push(&mut self, ptr: <<Self::Adapter as Adapter>::PointerOps as PointerOps>::Pointer) {
self.insert(ptr)
}

fn pop(&mut self) -> Option<<A::PointerOps as PointerOps>::Pointer> {
fn pop(&mut self) -> Option<<<Self::Adapter as Adapter>::PointerOps as PointerOps>::Pointer> {
let ptr = {
let mut iter = self.iter();
let ptr = iter.next();
Expand All @@ -62,7 +69,7 @@ where
ptr.map(|ptr| self.remove(&ptr))
}

fn peek(&self) -> Option<&<A::PointerOps as PointerOps>::Pointer> {
fn peek(&self) -> Option<&<<Self::Adapter as Adapter>::PointerOps as PointerOps>::Pointer> {
self.iter().next()
}
}
Expand Down
1 change: 1 addition & 0 deletions foyer-intrusive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#![feature(associated_type_bounds)]
#![feature(ptr_metadata)]
#![feature(trait_alias)]
#![feature(return_position_impl_trait_in_trait)]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting feature!

#![allow(clippy::new_without_default)]
#![allow(clippy::wrong_self_convention)]
#![allow(clippy::vtable_address_comparisons)]
Expand Down
19 changes: 8 additions & 11 deletions foyer-memory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,9 @@ use parking_lot::Mutex;
use std::hash::Hasher;
use twox_hash::XxHash64;

pub struct CacheConfig<K, V, E, EL>
pub struct CacheConfig<E>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice save!

where
K: Key,
V: Value,
E: EvictionPolicy<CacheItemEpAdapter<K, V, EL>, Link = EL>,
EL: Link,
E: EvictionPolicy,
{
capacity: usize,
shard_bits: usize,
Expand All @@ -42,7 +39,7 @@ pub struct Cache<K, V, E, EL>
where
K: Key,
V: Value,
E: EvictionPolicy<CacheItemEpAdapter<K, V, EL>, Link = EL>,
E: EvictionPolicy<Adapter = CacheItemEpAdapter<K, V, EL>>,
EL: Link,
{
shards: Vec<Mutex<CacheShard<K, V, E, EL>>>,
Expand All @@ -52,7 +49,7 @@ struct CacheShard<K, V, E, EL>
where
K: Key,
V: Value,
E: EvictionPolicy<CacheItemEpAdapter<K, V, EL>, Link = EL>,
E: EvictionPolicy<Adapter = CacheItemEpAdapter<K, V, EL>>,
EL: Link,
{
container: HashMap<K, V, CacheItemHmAdapter<K, V, EL>>,
Expand Down Expand Up @@ -115,10 +112,10 @@ impl<K, V, E, EL> Cache<K, V, E, EL>
where
K: Key,
V: Value,
E: EvictionPolicy<CacheItemEpAdapter<K, V, EL>, Link = EL>,
E: EvictionPolicy<Adapter = CacheItemEpAdapter<K, V, EL>>,
EL: Link,
{
pub fn new(config: CacheConfig<K, V, E, EL>) -> Self {
pub fn new(config: CacheConfig<E>) -> Self {
let mut shards = Vec::with_capacity(1 << config.shard_bits);

let shard_capacity = config.capacity / (1 << config.shard_bits);
Expand Down Expand Up @@ -231,8 +228,8 @@ mod tests {
}
}

type FifoCacheConfig = CacheConfig<K, V, Fifo<CacheItemEpAdapter<K, V, FifoLink>>, FifoLink>;
type FifoCache = Cache<K, V, Fifo<CacheItemEpAdapter<K, V, FifoLink>>, FifoLink>;
type FifoCacheConfig = CacheConfig<Fifo<CacheItemEpAdapter<K, V, FifoLink>>>;
type FifoCache = Cache<K, V, Fifo<CacheItemEpAdapter<K, V, FifoLink>>>;

#[test]
fn test_fifo_cache_simple() {
Expand Down
29 changes: 13 additions & 16 deletions foyer-storage/src/flusher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,17 @@ impl Flusher {
}
}

pub async fn run<A, D, E, EL>(
pub async fn run<D, E, EL>(
&self,
buffers: Arc<AsyncQueue<Vec<u8, A>>>,
region_manager: Arc<RegionManager<A, D, E, EL>>,
buffers: Arc<AsyncQueue<Vec<u8, D::IoBufferAllocator>>>,
region_manager: Arc<RegionManager<D, E, EL>>,
rate_limiter: Option<Arc<RateLimiter>>,
stop_rxs: Vec<broadcast::Receiver<()>>,
metrics: Arc<Metrics>,
) -> Vec<JoinHandle<()>>
where
A: BufferAllocator,
D: Device<IoBufferAllocator = A>,
E: EvictionPolicy<RegionEpItemAdapter<EL>, Link = EL>,
D: Device,
E: EvictionPolicy<Adapter = RegionEpItemAdapter<EL>>,
EL: Link,
{
let mut inner = self.inner.lock().await;
Expand Down Expand Up @@ -118,17 +117,16 @@ impl Flusher {
}
}

struct Runner<A, D, E, EL>
struct Runner<D, E, EL>
where
A: BufferAllocator,
D: Device<IoBufferAllocator = A>,
E: EvictionPolicy<RegionEpItemAdapter<EL>, Link = EL>,
D: Device,
E: EvictionPolicy<Adapter = RegionEpItemAdapter<EL>>,
EL: Link,
{
task_rx: mpsc::UnboundedReceiver<FlushTask>,
buffers: Arc<AsyncQueue<Vec<u8, A>>>,
buffers: Arc<AsyncQueue<Vec<u8, D::IoBufferAllocator>>>,

region_manager: Arc<RegionManager<A, D, E, EL>>,
region_manager: Arc<RegionManager<D, E, EL>>,

rate_limiter: Option<Arc<RateLimiter>>,

Expand All @@ -137,11 +135,10 @@ where
metrics: Arc<Metrics>,
}

impl<A, D, E, EL> Runner<A, D, E, EL>
impl<D, E, EL> Runner<D, E, EL>
where
A: BufferAllocator,
D: Device<IoBufferAllocator = A>,
E: EvictionPolicy<RegionEpItemAdapter<EL>, Link = EL>,
D: Device,
E: EvictionPolicy<Adapter = RegionEpItemAdapter<EL>>,
EL: Link,
{
async fn run(mut self) -> Result<()> {
Expand Down
Loading
Loading