16 : cache_dir_{std::move(cache_dir)},
19 BOOST_ASSERT(
hasher_ !=
nullptr);
23 const WAVM::U8 *wasmBytes,
24 WAVM::Uptr numWASMBytes,
25 std::function<std::vector<WAVM::U8>()> &&compileThunk) {
27 hasher_->twox_64(gsl::span(wasmBytes, numWASMBytes)).toHex();
35 std ::vector<WAVM::U8> module;
36 if (std::ifstream file{filepath.c_str(), std::ios::in | std::ios::binary};
38 auto module_size = file_size(filepath);
39 module.resize(module_size);
40 file.read(reinterpret_cast<char *>(module.data()), module_size);
41 if (not file.fail()) {
42 SL_VERBOSE(
logger_,
"WAVM runtime cache hit: {}", filepath);
45 SL_ERROR(
logger_,
"Error reading cached module: {}", filepath);
49 module = compileThunk();
51 std::ofstream{filepath.c_str(), std::ios::out | std::ios::binary};
53 file.write(reinterpret_cast<char *>(module.data()), module.size());
55 if (not file.fail()) {
56 SL_VERBOSE(
logger_,
"Saved WAVM runtime to cache: {}", filepath);
59 SL_ERROR(
logger_,
"Error writing module to cache: {}", filepath);
62 SL_ERROR(
logger_,
"Failed to cache WAVM runtime: {}", filepath);
bool createDirectoryRecursive(const path &path)
std::shared_ptr< crypto::Hasher > hasher_
std::vector< WAVM::U8 > getCachedObject(const WAVM::U8 *wasmBytes, WAVM::Uptr numWASMBytes, std::function< std::vector< WAVM::U8 >()> &&compileThunk) override
Logger createLogger(const std::string &tag)
ModuleCache(std::shared_ptr< crypto::Hasher > hasher, fs::path cache_dir)