mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-07-05 22:37:25 +00:00
Better wasm instance cache (#5109)
* Wasm instance cache * Reduce slot locking * Fixed test * Dispose of instance in case of error * Fixed benches * Style, comments, some renames * Replaced upgradable lock with mutex * Bump dependencies * Re-export CallInWasm * Update client/executor/src/wasm_runtime.rs Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com> * Update client/executor/src/native_executor.rs Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com> * Update client/executor/src/native_executor.rs Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com> * Update client/executor/src/wasm_runtime.rs Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com> * Update client/executor/wasmtime/src/runtime.rs Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com> * Update client/executor/src/wasm_runtime.rs Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com> * Update client/executor/src/wasm_runtime.rs Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com> * Update client/executor/src/wasm_runtime.rs Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com> * Indents * Whitespace * Formatting * Added issue link Co-authored-by: Benjamin Kampmann <ben.kampmann@googlemail.com> Co-authored-by: Gavin Wood <github@gavwood.com> Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
40b243f1c8
commit
d3208aa7bc
@@ -15,63 +15,29 @@
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use crate::host::{HostContext, HostState};
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
/// A common place to store a reference to the `HostState`.
|
||||
scoped_tls::scoped_thread_local!(static HOST_STATE: HostState);
|
||||
|
||||
/// Provide `HostState` for the runtime method call and execute the given function `f`.
|
||||
///
|
||||
/// This structure is passed into each host function handler and retained in the implementation of
|
||||
/// `WasmRuntime`. Whenever a call into a runtime method is initiated, the host state is populated
|
||||
/// with the state for that runtime method call.
|
||||
///
|
||||
/// During the execution of the runtime method call, wasm can call imported host functions. When
|
||||
/// that happens the host function handler gets a `HostContext` (obtainable through having a
|
||||
/// `HostState` reference).
|
||||
#[derive(Clone)]
|
||||
pub struct StateHolder {
|
||||
// This is `Some` only during a call.
|
||||
state: Rc<RefCell<Option<HostState>>>,
|
||||
/// During the execution of the provided function `with_context` will be callable.
|
||||
pub fn with_initialized_state<R, F>(s: &HostState, f: F) -> R
|
||||
where
|
||||
F: FnOnce() -> R,
|
||||
{
|
||||
HOST_STATE.set(s, f)
|
||||
}
|
||||
|
||||
impl StateHolder {
|
||||
/// Create a placeholder `StateHolder`.
|
||||
pub fn empty() -> StateHolder {
|
||||
StateHolder {
|
||||
state: Rc::new(RefCell::new(None)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Provide `HostState` for the runtime method call and execute the given function `f`.
|
||||
///
|
||||
/// During the execution of the provided function `with_context` will be callable.
|
||||
pub fn with_initialized_state<R, F>(&self, state: HostState, f: F) -> (R, HostState)
|
||||
where
|
||||
F: FnOnce() -> R,
|
||||
{
|
||||
*self.state.borrow_mut() = Some(state);
|
||||
|
||||
let ret = f();
|
||||
let state = self
|
||||
.state
|
||||
.borrow_mut()
|
||||
.take()
|
||||
.expect("cannot be None since was just assigned; qed");
|
||||
|
||||
(ret, state)
|
||||
}
|
||||
|
||||
/// Create a `HostContext` from the contained `HostState` and execute the given function `f`.
|
||||
///
|
||||
/// This function is only callable within closure passed to `init_state`. Otherwise, the passed
|
||||
/// context will be `None`.
|
||||
pub fn with_context<R, F>(&self, f: F) -> R
|
||||
where
|
||||
F: FnOnce(Option<HostContext>) -> R,
|
||||
{
|
||||
let state = self.state.borrow();
|
||||
match *state {
|
||||
Some(ref state) => f(Some(state.materialize())),
|
||||
None => f(None),
|
||||
}
|
||||
/// Create a `HostContext` from the contained `HostState` and execute the given function `f`.
|
||||
///
|
||||
/// This function is only callable within closure passed to `init_state`. Otherwise, the passed
|
||||
/// context will be `None`.
|
||||
pub fn with_context<R, F>(f: F) -> R
|
||||
where
|
||||
F: FnOnce(Option<HostContext>) -> R,
|
||||
{
|
||||
if !HOST_STATE.is_set() {
|
||||
return f(None)
|
||||
}
|
||||
HOST_STATE.with(|state| f(Some(state.materialize())))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user