Skip to content

Commit

Permalink
Sprinkle #[inline] in various impls
Browse files Browse the repository at this point in the history
  • Loading branch information
nazar-pc committed May 10, 2024
1 parent 40a6dd3 commit eccc821
Show file tree
Hide file tree
Showing 15 changed files with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub(in super::super) enum PlottingThreadPriority {
impl FromStr for PlottingThreadPriority {
type Err = String;

#[inline]
fn from_str(s: &str) -> anyhow::Result<Self, Self::Err> {
match s {
"min" => Ok(Self::Min),
Expand All @@ -37,6 +38,7 @@ impl FromStr for PlottingThreadPriority {
}

impl fmt::Display for PlottingThreadPriority {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::Min => "min",
Expand All @@ -47,6 +49,7 @@ impl fmt::Display for PlottingThreadPriority {
}

impl From<PlottingThreadPriority> for Option<ThreadPriority> {
#[inline]
fn from(value: PlottingThreadPriority) -> Self {
match value {
PlottingThreadPriority::Min => Some(ThreadPriority::Min),
Expand All @@ -69,6 +72,7 @@ pub(in super::super) struct DiskFarm {
impl FromStr for DiskFarm {
type Err = String;

#[inline]
fn from_str(s: &str) -> anyhow::Result<Self, Self::Err> {
let parts = s.split(',').collect::<Vec<_>>();
if parts.len() < 2 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub(in super::super) enum SectorState {
}

impl fmt::Display for SectorState {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::NotPlotted => "NotPlotted",
Expand Down
21 changes: 21 additions & 0 deletions crates/subspace-farmer/src/farm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ pub enum ProvingResult {
}

impl fmt::Display for ProvingResult {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
ProvingResult::Success => "Success",
Expand Down Expand Up @@ -174,6 +175,7 @@ pub struct DecodedFarmingError {
}

impl fmt::Display for DecodedFarmingError {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.error.fmt(f)
}
Expand Down Expand Up @@ -212,6 +214,7 @@ pub enum FarmingError {
}

impl Encode for FarmingError {
#[inline]
fn encode_to<O: Output + ?Sized>(&self, dest: &mut O) {
let error = DecodedFarmingError {
error: self.to_string(),
Expand All @@ -223,13 +226,15 @@ impl Encode for FarmingError {
}

impl Decode for FarmingError {
#[inline]
fn decode<I: Input>(input: &mut I) -> Result<Self, parity_scale_codec::Error> {
DecodedFarmingError::decode(input).map(FarmingError::Decoded)
}
}

impl FarmingError {
/// String variant of the error, primarily for monitoring purposes
#[inline]
pub fn str_variant(&self) -> &str {
match self {
FarmingError::FailedToSubscribeSlotInfo { .. } => "FailedToSubscribeSlotInfo",
Expand Down Expand Up @@ -346,6 +351,7 @@ pub trait HandlerId: Send + Sync + fmt::Debug {
}

impl HandlerId for event_listener_primitives::HandlerId {
#[inline]
fn detach(&self) {
self.detach();
}
Expand All @@ -362,12 +368,15 @@ pub enum FarmId {
}

impl Encode for FarmId {
#[inline]
fn size_hint(&self) -> usize {
1_usize
+ match self {
FarmId::Ulid(ulid) => 0_usize.saturating_add(Encode::size_hint(&ulid.0)),
}
}

#[inline]
fn encode_to<O: Output + ?Sized>(&self, output: &mut O) {
match self {
FarmId::Ulid(ulid) => {
Expand All @@ -381,6 +390,7 @@ impl Encode for FarmId {
impl EncodeLike for FarmId {}

impl Decode for FarmId {
#[inline]
fn decode<I: Input>(input: &mut I) -> Result<Self, parity_scale_codec::Error> {
match input
.read_byte()
Expand All @@ -397,6 +407,7 @@ impl Decode for FarmId {
#[allow(clippy::new_without_default)]
impl FarmId {
/// Creates new ID
#[inline]
pub fn new() -> Self {
Self::Ulid(Ulid::new())
}
Expand Down Expand Up @@ -447,48 +458,58 @@ impl<T> Farm for Box<T>
where
T: Farm + ?Sized,
{
#[inline]
fn id(&self) -> &FarmId {
self.as_ref().id()
}

#[inline]
fn total_sectors_count(&self) -> SectorIndex {
self.as_ref().total_sectors_count()
}

#[inline]
fn plotted_sectors(&self) -> Arc<dyn PlottedSectors + 'static> {
self.as_ref().plotted_sectors()
}

#[inline]
fn piece_cache(&self) -> Arc<dyn PieceCache + 'static> {
self.as_ref().piece_cache()
}

#[inline]
fn plot_cache(&self) -> Arc<dyn PlotCache + 'static> {
self.as_ref().plot_cache()
}

#[inline]
fn piece_reader(&self) -> Arc<dyn PieceReader + 'static> {
self.as_ref().piece_reader()
}

#[inline]
fn on_sector_update(
&self,
callback: HandlerFn<(SectorIndex, SectorUpdate)>,
) -> Box<dyn HandlerId> {
self.as_ref().on_sector_update(callback)
}

#[inline]
fn on_farming_notification(
&self,
callback: HandlerFn<FarmingNotification>,
) -> Box<dyn HandlerId> {
self.as_ref().on_farming_notification(callback)
}

#[inline]
fn on_solution(&self, callback: HandlerFn<SolutionResponse>) -> Box<dyn HandlerId> {
self.as_ref().on_solution(callback)
}

#[inline]
fn run(self: Box<Self>) -> Pin<Box<dyn Future<Output = anyhow::Result<()>> + Send>> {
(*self).run()
}
Expand Down
1 change: 1 addition & 0 deletions crates/subspace-farmer/src/piece_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pub struct PieceCache {

#[async_trait]
impl farm::PieceCache for PieceCache {
#[inline]
fn max_num_elements(&self) -> u32 {
self.inner.max_num_elements
}
Expand Down
3 changes: 3 additions & 0 deletions crates/subspace-farmer/src/plotter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,12 @@ impl<P> Plotter for Arc<P>
where
P: Plotter + Send + Sync,
{
#[inline]
async fn has_free_capacity(&self) -> Result<bool, String> {
self.as_ref().has_free_capacity().await
}

#[inline]
async fn plot_sector<PS>(
&self,
public_key: PublicKey,
Expand All @@ -139,6 +141,7 @@ where
.await
}

#[inline]
async fn try_plot_sector<PS>(
&self,
public_key: PublicKey,
Expand Down
1 change: 1 addition & 0 deletions crates/subspace-farmer/src/plotter/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub struct CpuPlotter<PG, PosTable> {
}

impl<PG, PosTable> Drop for CpuPlotter<PG, PosTable> {
#[inline]
fn drop(&mut self) {
self.abort_early.store(true, Ordering::Release);
self.tasks_sender.close_channel();
Expand Down
1 change: 1 addition & 0 deletions crates/subspace-farmer/src/single_disk_farm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ pub struct SingleDiskFarm {
}

impl Drop for SingleDiskFarm {
#[inline]
fn drop(&mut self) {
self.piece_reader.close_all_readers();
// Make background threads that are waiting to do something exit immediately
Expand Down
1 change: 1 addition & 0 deletions crates/subspace-farmer/src/single_disk_farm/farming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ impl<'a, 'b, PosTable> Clone for PlotAuditOptions<'a, 'b, PosTable>
where
PosTable: Table,
{
#[inline]
fn clone(&self) -> Self {
*self
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ impl<File> ReadAtSync for &RayonFiles<File>
where
File: ReadAtSync,
{
#[inline]
fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<()> {
(*self).read_at(buf, offset)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub struct DiskPieceReader {

#[async_trait]
impl PieceReader for DiskPieceReader {
#[inline]
async fn read_piece(
&self,
sector_index: SectorIndex,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@ pub struct UnbufferedIoFileWindows {
}

impl ReadAtSync for UnbufferedIoFileWindows {
#[inline]
fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<()> {
self.read_exact_at(buf, offset)
}
}

impl ReadAtSync for &UnbufferedIoFileWindows {
#[inline]
fn read_at(&self, buf: &mut [u8], offset: u64) -> io::Result<()> {
(*self).read_at(buf, offset)
}
Expand Down
2 changes: 2 additions & 0 deletions crates/subspace-farmer/src/thread_pool_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub struct PlottingThreadPoolsGuard {
impl Deref for PlottingThreadPoolsGuard {
type Target = PlottingThreadPoolPair;

#[inline]
fn deref(&self) -> &Self::Target {
self.thread_pool_pair
.as_ref()
Expand All @@ -37,6 +38,7 @@ impl Deref for PlottingThreadPoolsGuard {
}

impl Drop for PlottingThreadPoolsGuard {
#[inline]
fn drop(&mut self) {
let (mutex, event) = &*self.inner;
mutex.lock().thread_pool_pairs.push(
Expand Down
5 changes: 5 additions & 0 deletions crates/subspace-farmer/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub struct AsyncJoinOnDrop<T> {
}

impl<T> Drop for AsyncJoinOnDrop<T> {
#[inline]
fn drop(&mut self) {
if let Some(handle) = self.handle.take() {
if self.abort_on_drop {
Expand All @@ -49,6 +50,7 @@ impl<T> Drop for AsyncJoinOnDrop<T> {

impl<T> AsyncJoinOnDrop<T> {
/// Create new instance.
#[inline]
pub fn new(handle: task::JoinHandle<T>, abort_on_drop: bool) -> Self {
Self {
handle: Some(handle),
Expand All @@ -60,6 +62,7 @@ impl<T> AsyncJoinOnDrop<T> {
impl<T> Future for AsyncJoinOnDrop<T> {
type Output = Result<T, task::JoinError>;

#[inline]
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Pin::new(
self.handle
Expand All @@ -74,6 +77,7 @@ impl<T> Future for AsyncJoinOnDrop<T> {
pub(crate) struct JoinOnDrop(Option<thread::JoinHandle<()>>);

impl Drop for JoinOnDrop {
#[inline]
fn drop(&mut self) {
self.0
.take()
Expand All @@ -85,6 +89,7 @@ impl Drop for JoinOnDrop {

impl JoinOnDrop {
// Create new instance
#[inline]
pub(crate) fn new(handle: thread::JoinHandle<()>) -> Self {
Self(Some(handle))
}
Expand Down
2 changes: 2 additions & 0 deletions crates/subspace-farmer/src/utils/farmer_piece_getter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ impl<FarmIndex, PV, NC> fmt::Debug for FarmerPieceGetter<FarmIndex, PV, NC> {
}

impl<FarmIndex, PV, NC> Clone for FarmerPieceGetter<FarmIndex, PV, NC> {
#[inline]
fn clone(&self) -> Self {
Self {
inner: Arc::clone(&self.inner),
Expand Down Expand Up @@ -368,6 +369,7 @@ impl<FarmIndex, PV, NC> fmt::Debug for WeakFarmerPieceGetter<FarmIndex, PV, NC>
}

impl<FarmIndex, PV, NC> Clone for WeakFarmerPieceGetter<FarmIndex, PV, NC> {
#[inline]
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
Expand Down
1 change: 1 addition & 0 deletions crates/subspace-farmer/src/utils/plotted_pieces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ struct DummyReader;

#[async_trait]
impl PieceReader for DummyReader {
#[inline]
async fn read_piece(
&self,
_sector_index: SectorIndex,
Expand Down

0 comments on commit eccc821

Please sign in to comment.