Skip to content

Commit 22020b4

Browse files
committed
Simplify cache configuration API
A new `cache_config(Option<CacheConfig>)` method replaces multiple methods for controlling module caching. Now `None` disables caching, and users can directly provide a cache config or load one from a file.
1 parent a2f9cb9 commit 22020b4

File tree

8 files changed

+47
-86
lines changed

8 files changed

+47
-86
lines changed

crates/bench-api/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ impl BenchState {
436436
) -> Result<Self> {
437437
let mut config = options.config(None)?;
438438
// NB: always disable the compilation cache.
439-
config.disable_cache();
439+
config.cache_config(None);
440440
let engine = Engine::new(&config)?;
441441
let mut linker = Linker::<HostState>::new(&engine);
442442

crates/c-api/src/config.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,17 @@ pub unsafe extern "C" fn wasmtime_config_cache_config_load(
211211
c: &mut wasm_config_t,
212212
filename: *const c_char,
213213
) -> Option<Box<wasmtime_error_t>> {
214+
use std::path::Path;
215+
216+
use wasmtime::CacheConfig;
217+
214218
handle_result(
215219
if filename.is_null() {
216-
c.config.cache_config_load_default()
220+
CacheConfig::from_file(None).map(|cfg| c.config.cache_config(Some(cfg)))
217221
} else {
218222
match CStr::from_ptr(filename).to_str() {
219-
Ok(s) => c.config.cache_config_load(s),
223+
Ok(s) => CacheConfig::from_file(Some(&Path::new(s)))
224+
.map(|cfg| c.config.cache_config(Some(cfg))),
220225
Err(e) => Err(e.into()),
221226
}
222227
},

crates/cache/src/config.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,23 @@ impl CacheConfig {
346346
conf
347347
}
348348

349-
/// Parses cache configuration from the file specified
349+
/// Loads cache configuration specified at `path`.
350+
///
351+
/// This method will read the file specified by `path` on the filesystem and
352+
/// attempt to load cache configuration from it. This method can also fail
353+
/// due to I/O errors, misconfiguration, syntax errors, etc. For expected
354+
/// syntax in the configuration file see the [documentation online][docs].
355+
///
356+
/// Passing in `None` loads cache configuration from the system default path.
357+
/// This is located, for example, on Unix at `$HOME/.config/wasmtime/config.toml`
358+
/// and is typically created with the `wasmtime config new` command.
359+
///
360+
/// # Errors
361+
///
362+
/// This method can fail due to any error that happens when loading the file
363+
/// pointed to by `path` and attempting to load the cache configuration.
364+
///
365+
/// [docs]: https://bytecodealliance.github.io/wasmtime/cli-cache.html
350366
pub fn from_file(config_file: Option<&Path>) -> Result<Self> {
351367
let mut config = Self::load_and_parse_file(config_file)?;
352368
config.validate_or_default()?;

crates/cli-flags/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -737,10 +737,12 @@ impl CommonOptions {
737737
if self.codegen.cache != Some(false) {
738738
match &self.codegen.cache_config {
739739
Some(path) => {
740-
config.cache_config_load(path)?;
740+
config.cache_config(Some(wasmtime::CacheConfig::from_file(Some(Path::new(
741+
path,
742+
)))?));
741743
}
742744
None => {
743-
config.cache_config_load_default()?;
745+
config.cache_config(Some(wasmtime::CacheConfig::from_file(None)?));
744746
}
745747
}
746748
}

crates/wasmtime/src/config.rs

Lines changed: 8 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,88 +1371,25 @@ impl Config {
13711371

13721372
/// Set a custom cache configuration.
13731373
///
1374-
/// If you want to load the cache configuration from a file, use [`Config::cache_config_load`]
1375-
/// or [`Config::cache_config_load_default`] for the default enabled configuration.
1374+
/// If you want to load the cache configuration from a file, use [`CacheConfig::from_file`].
1375+
/// You can call [`CacheConfig::from_file(None)`] for the default, enabled configuration.
13761376
///
1377-
/// By default cache configuration is not enabled or loaded.
1377+
/// If you want to disable the cache, you can call this method with `None`.
13781378
///
1379-
/// This method is only available when the `cache` feature of this crate is
1380-
/// enabled.
1381-
///
1382-
/// [docs]: https://bytecodealliance.github.io/wasmtime/cli-cache.html
1383-
#[cfg(feature = "cache")]
1384-
pub fn cache_config(&mut self, cache_config: CacheConfig) -> &mut Self {
1385-
self.cache_config = cache_config;
1386-
self
1387-
}
1388-
1389-
/// Loads cache configuration specified at `path`.
1390-
///
1391-
/// This method will read the file specified by `path` on the filesystem and
1392-
/// attempt to load cache configuration from it. This method can also fail
1393-
/// due to I/O errors, misconfiguration, syntax errors, etc. For expected
1394-
/// syntax in the configuration file see the [documentation online][docs].
1395-
///
1396-
/// By default cache configuration is not enabled or loaded.
1379+
/// By default, new configs do not have caching enabled.
1380+
/// Every call to [`Module::new(my_wasm)`][crate::Module::new] will recompile `my_wasm`,
1381+
/// even when it is unchanged, unless an enabled `CacheConfig` is provided.
13971382
///
13981383
/// This method is only available when the `cache` feature of this crate is
13991384
/// enabled.
14001385
///
1401-
/// # Errors
1402-
///
1403-
/// This method can fail due to any error that happens when loading the file
1404-
/// pointed to by `path` and attempting to load the cache configuration.
1405-
///
14061386
/// [docs]: https://bytecodealliance.github.io/wasmtime/cli-cache.html
14071387
#[cfg(feature = "cache")]
1408-
pub fn cache_config_load(&mut self, path: impl AsRef<Path>) -> Result<&mut Self> {
1409-
self.cache_config = CacheConfig::from_file(Some(path.as_ref()))?;
1410-
Ok(self)
1411-
}
1412-
1413-
/// Disable caching.
1414-
///
1415-
/// Every call to [`Module::new(my_wasm)`][crate::Module::new] will
1416-
/// recompile `my_wasm`, even when it is unchanged.
1417-
///
1418-
/// By default, new configs do not have caching enabled. This method is only
1419-
/// useful for disabling a previous cache configuration.
1420-
///
1421-
/// This method is only available when the `cache` feature of this crate is
1422-
/// enabled.
1423-
#[cfg(feature = "cache")]
1424-
pub fn disable_cache(&mut self) -> &mut Self {
1425-
self.cache_config = CacheConfig::new_cache_disabled();
1388+
pub fn cache_config(&mut self, cache_config: Option<CacheConfig>) -> &mut Self {
1389+
self.cache_config = cache_config.unwrap_or_else(|| CacheConfig::new_cache_disabled());
14261390
self
14271391
}
14281392

1429-
/// Loads cache configuration from the system default path.
1430-
///
1431-
/// This commit is the same as [`Config::cache_config_load`] except that it
1432-
/// does not take a path argument and instead loads the default
1433-
/// configuration present on the system. This is located, for example, on
1434-
/// Unix at `$HOME/.config/wasmtime/config.toml` and is typically created
1435-
/// with the `wasmtime config new` command.
1436-
///
1437-
/// By default cache configuration is not enabled or loaded.
1438-
///
1439-
/// This method is only available when the `cache` feature of this crate is
1440-
/// enabled.
1441-
///
1442-
/// # Errors
1443-
///
1444-
/// This method can fail due to any error that happens when loading the
1445-
/// default system configuration. Note that it is not an error if the
1446-
/// default config file does not exist, in which case the default settings
1447-
/// for an enabled cache are applied.
1448-
///
1449-
/// [docs]: https://bytecodealliance.github.io/wasmtime/cli-cache.html
1450-
#[cfg(feature = "cache")]
1451-
pub fn cache_config_load_default(&mut self) -> Result<&mut Self> {
1452-
self.cache_config = CacheConfig::from_file(None)?;
1453-
Ok(self)
1454-
}
1455-
14561393
/// Sets a custom memory creator.
14571394
///
14581395
/// Custom memory creators are used when creating host `Memory` objects or when

crates/wasmtime/src/engine/serialization.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ impl Metadata<'_> {
446446
#[cfg(test)]
447447
mod test {
448448
use super::*;
449-
use crate::{Config, Module, OptLevel};
449+
use crate::{CacheConfig, Config, Module, OptLevel};
450450
use std::{
451451
collections::hash_map::DefaultHasher,
452452
hash::{Hash, Hasher},
@@ -664,7 +664,7 @@ Caused by:
664664
)?;
665665
let mut cfg = Config::new();
666666
cfg.cranelift_opt_level(OptLevel::None)
667-
.cache_config_load(&config_path)?;
667+
.cache_config(Some(CacheConfig::from_file(Some(&config_path))?));
668668
let engine = Engine::new(&cfg)?;
669669
Module::new(&engine, "(module (func))")?;
670670
assert_eq!(engine.config().cache_config.cache_hits(), 0);
@@ -675,7 +675,7 @@ Caused by:
675675

676676
let mut cfg = Config::new();
677677
cfg.cranelift_opt_level(OptLevel::Speed)
678-
.cache_config_load(&config_path)?;
678+
.cache_config(Some(CacheConfig::from_file(Some(&config_path))?));
679679
let engine = Engine::new(&cfg)?;
680680
Module::new(&engine, "(module (func))")?;
681681
assert_eq!(engine.config().cache_config.cache_hits(), 0);
@@ -686,7 +686,7 @@ Caused by:
686686

687687
let mut cfg = Config::new();
688688
cfg.cranelift_opt_level(OptLevel::SpeedAndSize)
689-
.cache_config_load(&config_path)?;
689+
.cache_config(Some(CacheConfig::from_file(Some(&config_path))?));
690690
let engine = Engine::new(&cfg)?;
691691
Module::new(&engine, "(module (func))")?;
692692
assert_eq!(engine.config().cache_config.cache_hits(), 0);
@@ -696,7 +696,8 @@ Caused by:
696696
assert_eq!(engine.config().cache_config.cache_misses(), 1);
697697

698698
let mut cfg = Config::new();
699-
cfg.debug_info(true).cache_config_load(&config_path)?;
699+
cfg.debug_info(true)
700+
.cache_config(Some(CacheConfig::from_file(Some(&config_path))?));
700701
let engine = Engine::new(&cfg)?;
701702
Module::new(&engine, "(module (func))")?;
702703
assert_eq!(engine.config().cache_config.cache_hits(), 0);
@@ -771,7 +772,7 @@ Caused by:
771772
),
772773
)?;
773774
let mut cfg = Config::new();
774-
cfg.cache_config_load(&config_path)?;
775+
cfg.cache_config(Some(CacheConfig::from_file(Some(&config_path))?));
775776
let engine = Engine::new(&cfg)?;
776777
Component::new(&engine, "(component (core module (func)))")?;
777778
assert_eq!(engine.config().cache_config.cache_hits(), 0);

examples/fast_compilation.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
//! If your application design is compatible with pre-compiling Wasm programs,
44
//! prefer doing that.
55
6-
use wasmtime::{Config, Engine, Result, Strategy};
6+
use wasmtime::{CacheConfig, Config, Engine, Result, Strategy};
77

88
fn main() -> Result<()> {
99
let mut config = Config::new();
1010

1111
// Enable the compilation cache, using the default cache configuration
1212
// settings.
13-
config.cache_config_load_default()?;
13+
config.cache_config(Some(CacheConfig::from_file(None)?));
1414

1515
// Enable Winch, Wasmtime's baseline compiler.
1616
config.strategy(Strategy::Winch);

src/commands/explore.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl ExploreCommand {
5151
let clif_dir = if let Some(Strategy::Cranelift) | None = self.common.codegen.compiler {
5252
let clif_dir = tempdir()?;
5353
config.emit_clif(clif_dir.path());
54-
config.disable_cache(); // cache does not emit clif
54+
config.cache_config(None); // cache does not emit clif
5555
Some(clif_dir)
5656
} else {
5757
None

0 commit comments

Comments
 (0)