-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move parsing of subscribable events off critical path
- Loading branch information
Showing
6 changed files
with
113 additions
and
24 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright (c) Aptos Foundation | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use aptos_infallible::Mutex; | ||
use once_cell::sync::OnceCell; | ||
use rayon::ThreadPool; | ||
use std::{ops::Deref, sync::mpsc::Receiver}; | ||
|
||
#[derive(Debug)] | ||
pub struct Planned<T> { | ||
value: OnceCell<T>, | ||
rx: OnceCell<Mutex<Receiver<T>>>, | ||
} | ||
|
||
impl<T> Planned<T> { | ||
pub fn place_holder() -> Self { | ||
Self { | ||
value: OnceCell::new(), | ||
rx: OnceCell::new(), | ||
} | ||
} | ||
|
||
pub fn plan(&self, thread_pool: &ThreadPool, getter: impl FnOnce() -> T + Send + 'static) | ||
where | ||
T: Send + 'static, | ||
{ | ||
let (tx, rx) = std::sync::mpsc::channel(); | ||
|
||
thread_pool.spawn(move || { | ||
tx.send(getter()).ok(); | ||
}); | ||
|
||
self.rx.set(Mutex::new(rx)).expect("Already planned."); | ||
} | ||
|
||
pub fn ready(t: T) -> Self { | ||
Self { | ||
value: OnceCell::with_value(t), | ||
rx: OnceCell::new(), | ||
} | ||
} | ||
|
||
pub fn get(&self) -> &T { | ||
if let Some(t) = self.value.get() { | ||
t | ||
} else { | ||
let rx = self.rx.get().expect("Not planned").lock(); | ||
if self.value.get().is_none() { | ||
let t = rx.recv().expect("Plan failed."); | ||
self.value.set(t).map_err(|_| "").expect("Already set."); | ||
} | ||
self.value.get().expect("Must have been set.") | ||
} | ||
} | ||
} | ||
|
||
impl<T> Deref for Planned<T> { | ||
type Target = T; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
self.get() | ||
} | ||
} | ||
|
||
pub trait Plan { | ||
fn plan<T: Send + 'static>(&self, getter: impl FnOnce() -> T + Send + 'static) -> Planned<T>; | ||
} | ||
|
||
impl Plan for ThreadPool { | ||
fn plan<T: Send + 'static>(&self, getter: impl FnOnce() -> T + Send + 'static) -> Planned<T> { | ||
let planned = Planned::<T>::place_holder(); | ||
planned.plan(self, getter); | ||
planned | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters