uses enum instead of trait for VoteTransaction (#22019)

Box<dyn Trait> involves runtime dispatch, has significant overhead and
is slow. It also requires hacky boilerplate code for implementing Clone
or other basic traits:
https://github.com/solana-labs/solana/blob/e92a81b74/programs/vote/src/vote_state/mod.rs#L70-L102

Only limited known types can be VoteTransaction and they are all defined
in the same crate. So using a trait here only adds overhead.
https://github.com/solana-labs/solana/blob/e92a81b74/programs/vote/src/vote_state/mod.rs#L125-L165
https://github.com/solana-labs/solana/blob/e92a81b74/programs/vote/src/vote_state/mod.rs#L221-L264
This commit is contained in:
behzad nouri
2021-12-22 14:25:46 +00:00
committed by GitHub
parent d6de4a2f4e
commit 4d62f03297
10 changed files with 118 additions and 325 deletions

View File

@ -611,7 +611,7 @@ mod tests {
transaction::{self, Transaction},
},
solana_stake_program::stake_state,
solana_vote_program::vote_state::Vote,
solana_vote_program::vote_state::{Vote, VoteTransaction},
std::{
sync::{
atomic::{AtomicBool, AtomicU64},
@ -1315,7 +1315,7 @@ mod tests {
hash: Hash::default(),
timestamp: None,
};
subscriptions.notify_vote(Box::new(vote));
subscriptions.notify_vote(VoteTransaction::from(vote));
let response = receiver.recv();
assert_eq!(

View File

@ -93,7 +93,7 @@ impl From<NotificationEntry> for TimestampedNotificationEntry {
pub enum NotificationEntry {
Slot(SlotInfo),
SlotUpdate(SlotUpdate),
Vote(Box<dyn VoteTransaction>),
Vote(VoteTransaction),
Root(Slot),
Bank(CommitmentSlots),
Gossip(Slot),
@ -677,8 +677,8 @@ impl RpcSubscriptions {
self.enqueue_notification(NotificationEntry::SignaturesReceived(slot_signatures));
}
pub fn notify_vote(&self, vote: Box<dyn VoteTransaction>) {
self.enqueue_notification(NotificationEntry::Vote(vote.clone()));
pub fn notify_vote(&self, vote: VoteTransaction) {
self.enqueue_notification(NotificationEntry::Vote(vote));
}
pub fn notify_roots(&self, mut rooted_slots: Vec<Slot>) {