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

[authority] Small code fixes on the back of profiling #839

Merged
merged 8 commits into from
Mar 18, 2022

Conversation

gdanezis
Copy link
Collaborator

  • Cache the certificate digest but do not serialize / deserialize it.
  • Handle better some instrumentation, and generally eliminate cloning when not needed.
  • Handle better some ok_or cases with an expensive None case.

@gdanezis gdanezis force-pushed the authority-small-improv branch from 29a7079 to c89a6a0 Compare March 15, 2022 16:48
@gdanezis gdanezis changed the base branch from authority-mock-crypto to main March 15, 2022 16:48
@gdanezis gdanezis force-pushed the authority-small-improv branch from c89a6a0 to bcf6166 Compare March 15, 2022 17:12
Copy link
Contributor

@huitseeker huitseeker left a comment

Choose a reason for hiding this comment

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

Pretty dope! While I have you here fixing ok_or_else instances:

diff --git a/sui_types/src/base_types.rs b/sui_types/src/base_types.rs
index b86d5fde..314993e2 100644
--- a/sui_types/src/base_types.rs
+++ b/sui_types/src/base_types.rs
@@ -617,10 +617,7 @@ impl TryFrom<String> for ObjectID {
     type Error = ObjectIDParseError;

     fn try_from(s: String) -> Result<ObjectID, ObjectIDParseError> {
-        match Self::from_hex(s.clone()) {
-            Ok(q) => Ok(q),
-            Err(_) => Self::from_hex_literal(&s),
-        }
+        Self::from_hex(s.clone()).or_else(|_| { Self::from_hex_literal(&s) })
     }
 }

@@ -628,10 +625,7 @@ impl std::str::FromStr for ObjectID {
     type Err = ObjectIDParseError;
     // Try to match both the literal (0xABC..) and the normal (ABC)
     fn from_str(s: &str) -> Result<Self, ObjectIDParseError> {
-        match Self::from_hex(s) {
-            Ok(q) => Ok(q),
-            Err(_) => Self::from_hex_literal(s),
-        }
+        Self::from_hex(s).or_else(|_| { Self::from_hex_literal(s) })
     }
 }

😝

// This is a cache of an otherwise expensive to compute value.
// DO NOT serialize or deserialize from the network or disk.
#[serde(skip)]
pub transaction_digest: Option<TransactionDigest>,
Copy link
Contributor

@huitseeker huitseeker Mar 17, 2022

Choose a reason for hiding this comment

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

If you use OnceCell instead of Option, you can have just a single method digest and make it super straightforward. Please note the code comment in the below, that's where the magic happens:

struct CertifiedTransaction {
    transaction_digest: OnceCell<TransactionDigest>,
    ...
}

impl Hashable for CertifiedTransaction {
    // Note the &self here, it's not a &mut
    pub fn digest(&self) -> TransactionDigest {
        self.transaction_digest.get_or_init(|| {
            <your usual hashing implementation here>
            ...
        })
    }
}

https://docs.rs/once_cell/latest/once_cell/#

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Love it love it love, and will reuse!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

// Note the &self here, it's not a &mut

Yep I missed that

@gdanezis gdanezis force-pushed the authority-small-improv branch from bcf6166 to 21537a2 Compare March 17, 2022 15:48
@gdanezis
Copy link
Collaborator Author

gdanezis commented Mar 17, 2022

Many thanks @huitseeker -- now using OnceCell, and will be using it elsewhere too! Your code reviews are always a Rust masterclass.

Copy link
Contributor

@huitseeker huitseeker left a comment

Choose a reason for hiding this comment

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

@gdanezis gdanezis force-pushed the authority-small-improv branch from 9757e77 to e291944 Compare March 17, 2022 20:34
match self
.state
.handle_confirmation_transaction(confirmation_transaction)
.instrument(tracing::debug_span!("process_cert",
tx_digest =? message.transaction.digest(),
tx_digest =? digest,
Copy link
Contributor

Choose a reason for hiding this comment

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

You could just rename the original let variable as let tx_digest then here you can just do ?tx_digest. BTW I fixed this in my outstanding PR, which nobody has looked at yet. ;-)

Copy link
Contributor

Choose a reason for hiding this comment

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

Happy to help with codemods :D

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fixed!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Also, the only open PR that I see from you has a big "WIP:" at the start, which I took to mean you are still working on it.

match self
.state
.handle_confirmation_transaction(confirmation_transaction)
.instrument(tracing::debug_span!("process_cert",
tx_digest =? message.transaction.digest(),
tx_digest =? digest,
Copy link
Contributor

Choose a reason for hiding this comment

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

Happy to help with codemods :D

sui_types/src/messages.rs Show resolved Hide resolved
@@ -26,6 +26,8 @@ use std::{
collections::{BTreeSet, HashSet},
hash::{Hash, Hasher},
};
// use once_cell::unsync::Lazy;
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done!

Copy link
Contributor

@huitseeker huitseeker left a comment

Choose a reason for hiding this comment

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

Tip top!

@gdanezis gdanezis force-pushed the authority-small-improv branch from e0c43a5 to 58f206d Compare March 18, 2022 09:35
@gdanezis gdanezis merged commit ce93bd3 into main Mar 18, 2022
@gdanezis gdanezis deleted the authority-small-improv branch March 18, 2022 10:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants