Making caching support optional in Wasmtime (#2119)

This commit moves all of the caching support that currently lives in
`wasmtime-environ` into a `wasmtime-cache` crate and makes it optional. The
goal here is to slim down the `wasmtime-environ` crate and clearly separate
boundaries where caching is a standalone and optional feature, not intertwined
with other crates.
This commit is contained in:
Alex Crichton
2020-08-07 15:42:40 -05:00
committed by GitHub
parent a796d65467
commit 08f9eb1725
21 changed files with 104 additions and 54 deletions

View File

@@ -13,6 +13,7 @@ edition = "2018"
wasmtime-runtime = { path = "../runtime", version = "0.19.0" }
wasmtime-environ = { path = "../environ", version = "0.19.0" }
wasmtime-jit = { path = "../jit", version = "0.19.0" }
wasmtime-cache = { path = "../cache", version = "0.19.0", optional = true }
wasmtime-profiling = { path = "../profiling", version = "0.19.0" }
wasmparser = "0.59.0"
target-lexicon = { version = "0.10.0", default-features = false }
@@ -40,7 +41,7 @@ wasmtime-wasi = { path = "../wasi" }
maintenance = { status = "actively-developed" }
[features]
default = ['wat', 'jitdump', 'parallel-compilation']
default = ['cache', 'wat', 'jitdump', 'parallel-compilation']
# Enables experimental support for the lightbeam codegen backend, an alternative
# to cranelift. Requires Nightly Rust currently, and this is not enabled by
@@ -55,3 +56,6 @@ vtune = ["wasmtime-jit/vtune"]
# Enables parallel compilation of WebAssembly code
parallel-compilation = ["wasmtime-jit/parallel-compilation"]
# Enables support for automatic cache configuration to be enabled in `Config`.
cache = ["wasmtime-cache"]

View File

@@ -4,7 +4,8 @@ use crate::types::{EntityType, ExportType, ExternType, ImportType};
use anyhow::{bail, Context, Result};
use std::path::Path;
use std::sync::{Arc, Mutex};
use wasmtime_environ::ModuleCacheEntry;
#[cfg(feature = "cache")]
use wasmtime_cache::ModuleCacheEntry;
use wasmtime_jit::{CompilationArtifacts, CompiledModule};
/// A compiled WebAssembly module, ready to be instantiated.
@@ -301,11 +302,13 @@ impl Module {
}
unsafe fn compile(engine: &Engine, binary: &[u8]) -> Result<Self> {
let cache_entry = ModuleCacheEntry::new("wasmtime", engine.cache_config());
let artifacts = cache_entry
#[cfg(feature = "cache")]
let artifacts = ModuleCacheEntry::new("wasmtime", engine.cache_config())
.get_data((engine.compiler(), binary), |(compiler, binary)| {
CompilationArtifacts::build(compiler, binary)
})?;
#[cfg(not(feature = "cache"))]
let artifacts = CompilationArtifacts::build(engine.compiler(), binary)?;
let compiled = CompiledModule::from_artifacts(
artifacts,

View File

@@ -7,13 +7,16 @@ use std::cmp;
use std::convert::TryFrom;
use std::fmt;
use std::hash::{Hash, Hasher};
#[cfg(feature = "cache")]
use std::path::Path;
use std::rc::{Rc, Weak};
use std::sync::Arc;
use target_lexicon::Triple;
use wasmparser::Validator;
#[cfg(feature = "cache")]
use wasmtime_cache::CacheConfig;
use wasmtime_environ::settings::{self, Configurable, SetError};
use wasmtime_environ::{ir, isa, isa::TargetIsa, wasm, CacheConfig, Tunables};
use wasmtime_environ::{ir, isa, isa::TargetIsa, wasm, Tunables};
use wasmtime_jit::{native, CompilationStrategy, Compiler};
use wasmtime_profiling::{JitDumpAgent, NullProfilerAgent, ProfilingAgent, VTuneAgent};
use wasmtime_runtime::{
@@ -37,6 +40,7 @@ pub struct Config {
pub(crate) isa_flags: isa::Builder,
pub(crate) tunables: Tunables,
pub(crate) strategy: CompilationStrategy,
#[cfg(feature = "cache")]
pub(crate) cache_config: CacheConfig,
pub(crate) profiler: Arc<dyn ProfilingAgent>,
pub(crate) memory_creator: Option<MemoryCreatorProxy>,
@@ -89,6 +93,7 @@ impl Config {
flags,
isa_flags: native::builder(),
strategy: CompilationStrategy::Auto,
#[cfg(feature = "cache")]
cache_config: CacheConfig::new_cache_disabled(),
profiler: Arc::new(NullProfilerAgent),
memory_creator: None,
@@ -395,14 +400,18 @@ impl Config {
///
/// By default cache configuration is not enabled or loaded.
///
/// This method is only available when the `cache` feature of this crate is
/// enabled.
///
/// # Errors
///
/// This method can fail due to any error that happens when loading the file
/// pointed to by `path` and attempting to load the cache configuration.
///
/// [docs]: https://bytecodealliance.github.io/wasmtime/cli-cache.html
#[cfg(feature = "cache")]
pub fn cache_config_load(&mut self, path: impl AsRef<Path>) -> Result<&mut Self> {
self.cache_config = wasmtime_environ::CacheConfig::from_file(Some(path.as_ref()))?;
self.cache_config = CacheConfig::from_file(Some(path.as_ref()))?;
Ok(self)
}
@@ -416,6 +425,9 @@ impl Config {
///
/// By default cache configuration is not enabled or loaded.
///
/// This method is only available when the `cache` feature of this crate is
/// enabled.
///
/// # Errors
///
/// This method can fail due to any error that happens when loading the
@@ -424,8 +436,9 @@ impl Config {
/// for an enabled cache are applied.
///
/// [docs]: https://bytecodealliance.github.io/wasmtime/cli-cache.html
#[cfg(feature = "cache")]
pub fn cache_config_load_default(&mut self) -> Result<&mut Self> {
self.cache_config = wasmtime_environ::CacheConfig::from_file(None)?;
self.cache_config = CacheConfig::from_file(None)?;
Ok(self)
}
@@ -793,6 +806,7 @@ impl Engine {
&self.inner.compiler
}
#[cfg(feature = "cache")]
pub(crate) fn cache_config(&self) -> &CacheConfig {
&self.config().cache_config
}