Cache re-usable work performed by the loader (#12135)

This commit is contained in:
Jack May
2020-09-14 17:42:37 -07:00
committed by GitHub
parent af2262cbba
commit 3278d78f08
11 changed files with 543 additions and 249 deletions

View File

@ -1,10 +1,12 @@
//! @brief Solana Native program entry point
#[cfg(RUSTC_WITH_SPECIALIZATION)]
use crate::abi_example::AbiExample;
use crate::{
account::Account, account::KeyedAccount, instruction::CompiledInstruction,
instruction::InstructionError, message::Message, pubkey::Pubkey,
};
use std::{cell::RefCell, rc::Rc};
use std::{cell::RefCell, rc::Rc, sync::Arc};
// Prototype of a native program entry point
///
@ -218,6 +220,11 @@ pub trait InvokeContext {
fn get_compute_budget(&self) -> ComputeBudget;
/// Get this invocation's compute meter
fn get_compute_meter(&self) -> Rc<RefCell<dyn ComputeMeter>>;
/// Loaders may need to do work in order to execute a program. Cache
/// the work that can be re-used across executions
fn add_executor(&mut self, pubkey: &Pubkey, executor: Arc<dyn Executor>);
/// Get the completed loader work that can be re-used across executions
fn get_executor(&mut self, pubkey: &Pubkey) -> Option<Arc<dyn Executor>>;
}
#[derive(Clone, Copy, Debug)]
@ -265,3 +272,33 @@ pub trait Logger {
/// Log a message
fn log(&mut self, message: &str);
}
/// Program executor
pub trait Executor: Send + Sync {
/// Execute the program
fn execute(
&self,
program_id: &Pubkey,
keyed_accounts: &[KeyedAccount],
instruction_data: &[u8],
invoke_context: &mut dyn InvokeContext,
) -> Result<(), InstructionError>;
}
#[cfg(RUSTC_WITH_SPECIALIZATION)]
impl AbiExample for Arc<dyn Executor> {
fn example() -> Self {
struct ExampleExecutor {}
impl Executor for ExampleExecutor {
fn execute(
&self,
_program_id: &Pubkey,
_keyed_accounts: &[KeyedAccount],
_instruction_data: &[u8],
_invoke_context: &mut dyn InvokeContext,
) -> std::result::Result<(), InstructionError> {
Ok(())
}
}
Arc::new(ExampleExecutor {})
}
}