-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
db-api: trait Compress and Encode use reference self #11186
base: main
Are you sure you want to change the base?
Conversation
fn compress(self) -> Self::Compressed { | ||
self.value | ||
fn compress(&self) -> Self::Compressed { | ||
self.value.clone() |
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.
headers, tx_lookup and hashing_account stages use this heavily, tbh don't know the impact of cloning for each row
wdyt @DaniPopes
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.
better to return &Self::Compressed
and letting caller context clone, if I understood the issue right, this is happening anyway on main?
always better to call clone in context using return value, so the dev is conscious of this perf downgrade wherever it's used.
fn encode(self) -> Self::Encoded { | ||
self.key | ||
fn encode(&self) -> Self::Encoded { | ||
self.key.clone() |
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.
same
This can only work if we propagate the reference all the way to all call sites (taking in |
@nkysg Hey actually I didn't consider another usecase, which is taking by reference in the diff --git a/crates/storage/provider/src/providers/database/provider.rs b/crates/storage/provider/src/providers/database/provider.rs
index 7159720bf..9efba8805 100644
--- a/crates/storage/provider/src/providers/database/provider.rs
+++ b/crates/storage/provider/src/providers/database/provider.rs
@@ -1340,6 +1340,7 @@ impl<TX: DbTxMut + DbTx, Spec: Send + Sync> DatabaseProvider<TX, Spec> {
P: Copy,
T: Table<Value = BlockNumberList>,
{
+ let mut number_list = BlockNumberList::empty();
for (partial_key, indices) in index_updates {
let mut last_shard =
self.take_shard::<T>(sharded_key_factory(partial_key, u64::MAX))?;
@@ -1354,9 +1355,11 @@ impl<TX: DbTxMut + DbTx, Spec: Send + Sync> DatabaseProvider<TX, Spec> {
// Insert last list with `u64::MAX`.
u64::MAX
};
+ number_list.clear();
+ number_list.append(list.iter().copied());
self.tx.put::<T>(
sharded_key_factory(partial_key, highest_block_number),
- BlockNumberList::new_pre_sorted(list.iter().copied()),
+ &number_list,
)?;
}
} I think we should keep going with this, do you mind picking this up again? If not I'll open an issue for it |
@DaniPopes Got it. Thanks sir. pleasure to give me some suggestions to improve this. and I have opened this issue #11185 again. |
ref #11185.
Trait Encode fn encodes use self. It's a bit weird.
I see many crate use &self, like
This is alloyed rlp Encode trait.
https://github.com/alloy-rs/rlp/blob/9aef28e6da7c2769460dd6bf22eeacd53adf0ffc/crates/rlp/src/encode.rs#L15
And then maybe we can remove some clone. I think some db fn like put also can use reference