-
Notifications
You must be signed in to change notification settings - Fork 11.3k
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
Conversation
29a7079
to
c89a6a0
Compare
c89a6a0
to
bcf6166
Compare
There was a problem hiding this 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) })
}
}
😝
sui_types/src/messages.rs
Outdated
// 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>, |
There was a problem hiding this comment.
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>
...
})
}
}
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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
bcf6166
to
21537a2
Compare
Many thanks @huitseeker -- now using OnceCell, and will be using it elsewhere too! Your code reviews are always a Rust masterclass. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two problems:
9757e77
to
e291944
Compare
sui_core/src/authority_server.rs
Outdated
match self | ||
.state | ||
.handle_confirmation_transaction(confirmation_transaction) | ||
.instrument(tracing::debug_span!("process_cert", | ||
tx_digest =? message.transaction.digest(), | ||
tx_digest =? digest, |
There was a problem hiding this comment.
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. ;-)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed!
There was a problem hiding this comment.
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.
sui_core/src/authority_server.rs
Outdated
match self | ||
.state | ||
.handle_confirmation_transaction(confirmation_transaction) | ||
.instrument(tracing::debug_span!("process_cert", | ||
tx_digest =? message.transaction.digest(), | ||
tx_digest =? digest, |
There was a problem hiding this comment.
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
Outdated
@@ -26,6 +26,8 @@ use std::{ | |||
collections::{BTreeSet, HashSet}, | |||
hash::{Hash, Hasher}, | |||
}; | |||
// use once_cell::unsync::Lazy; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tip top!
e0c43a5
to
58f206d
Compare
ok_or
cases with an expensive None case.