Move the Store::signature_cache field (#847)

This commit removes the `signature_cache` field from the `Store` type
and performs a few internal changes which are aimed to be a bit forward
looking towards #777, making `Store` threadsafe.

The changes made here are:

* The `SignatureRegistry` internal type now contains the reverse map
  that `signature_cache` was serving to do. This is populated on calls
  to `register` automatically and is accompanied by a `lookup` method as
  well.

* The `register_wasmtime_signature` and `lookup_wasmtime_signature`
  methods were removed from `Store` and now instead work by using the
  `Compiler::signatures` field.

* The `SignatureRegistry` type was updated to have interior mutability.
  The global `Compiler` type is highly likely to get shared across many
  threads through `Store`, so it needs some form of lock somewhere for
  mutation of the registry of signatures and this commit opts to put it
  inside `SignatureRegistry` which will eventually allow for the removal
  of most `&mut self` method on `Compiler`.
This commit is contained in:
Alex Crichton
2020-01-22 14:54:55 -06:00
committed by GitHub
parent 5953215bac
commit e5af0ae3de
7 changed files with 48 additions and 53 deletions

View File

@@ -1,12 +1,8 @@
use anyhow::Result;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use std::sync::Arc;
use wasmtime_environ::{
ir,
settings::{self, Configurable},
};
use wasmtime_environ::settings::{self, Configurable};
use wasmtime_jit::{native, CompilationStrategy, Compiler, Features};
// Runtime Environment
@@ -342,7 +338,6 @@ pub struct Store {
struct StoreInner {
engine: Engine,
compiler: RefCell<Compiler>,
signature_cache: RefCell<HashMap<wasmtime_runtime::VMSharedSignatureIndex, ir::Signature>>,
}
impl Store {
@@ -354,7 +349,6 @@ impl Store {
inner: Rc::new(StoreInner {
engine: engine.clone(),
compiler: RefCell::new(compiler),
signature_cache: RefCell::new(HashMap::new()),
}),
}
}
@@ -364,36 +358,14 @@ impl Store {
&self.inner.engine
}
pub(crate) fn compiler(&self) -> std::cell::Ref<'_, Compiler> {
self.inner.compiler.borrow()
}
pub(crate) fn compiler_mut(&self) -> std::cell::RefMut<'_, Compiler> {
self.inner.compiler.borrow_mut()
}
pub(crate) fn register_wasmtime_signature(
&self,
signature: &ir::Signature,
) -> wasmtime_runtime::VMSharedSignatureIndex {
use std::collections::hash_map::Entry;
let index = self.compiler_mut().signatures().register(signature);
match self.inner.signature_cache.borrow_mut().entry(index) {
Entry::Vacant(v) => {
v.insert(signature.clone());
}
Entry::Occupied(_) => (),
}
index
}
pub(crate) fn lookup_wasmtime_signature(
&self,
type_index: wasmtime_runtime::VMSharedSignatureIndex,
) -> Option<ir::Signature> {
self.inner
.signature_cache
.borrow()
.get(&type_index)
.cloned()
}
/// Returns whether the stores `a` and `b` refer to the same underlying
/// `Store`.
///