Improve handling of strings for backtraces (#843)
* Improve handling of strings for backtraces Largely avoid storing strings at all in the `wasmtime-*` internal crates, and instead only store strings in a separate global cache specific to the `wasmtime` crate itself. This global cache is inserted and removed from dynamically as modules are created and deallocated, and the global cache is consulted whenever a `Trap` is created to symbolicate any wasm frames. This also avoids the need to thread `module_name` through the jit crates and back, and additionally removes the need for `ModuleSyncString`. * Run rustfmt
This commit is contained in:
@@ -54,7 +54,7 @@ pub use crate::func_environ::BuiltinFunctionIndex;
|
||||
#[cfg(feature = "lightbeam")]
|
||||
pub use crate::lightbeam::Lightbeam;
|
||||
pub use crate::module::{
|
||||
Export, MemoryPlan, MemoryStyle, Module, ModuleSyncString, TableElements, TablePlan, TableStyle,
|
||||
Export, MemoryPlan, MemoryStyle, Module, TableElements, TablePlan, TableStyle,
|
||||
};
|
||||
pub use crate::module_environ::{
|
||||
translate_signature, DataInitializer, DataInitializerLocation, FunctionBodyData,
|
||||
|
||||
@@ -3,15 +3,16 @@
|
||||
use crate::module_environ::FunctionBodyData;
|
||||
use crate::tunables::Tunables;
|
||||
use cranelift_codegen::ir;
|
||||
use cranelift_entity::{EntityRef, PrimaryMap, SecondaryMap};
|
||||
use cranelift_entity::{EntityRef, PrimaryMap};
|
||||
use cranelift_wasm::{
|
||||
DefinedFuncIndex, DefinedGlobalIndex, DefinedMemoryIndex, DefinedTableIndex, FuncIndex, Global,
|
||||
GlobalIndex, Memory, MemoryIndex, SignatureIndex, Table, TableIndex,
|
||||
};
|
||||
use indexmap::IndexMap;
|
||||
use more_asserts::assert_ge;
|
||||
use std::collections::HashMap;
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering::SeqCst};
|
||||
|
||||
/// A WebAssembly table initializer.
|
||||
#[derive(Clone, Debug, Hash)]
|
||||
@@ -129,33 +130,14 @@ impl TablePlan {
|
||||
}
|
||||
}
|
||||
|
||||
/// Allows module strings to be cached as reused across
|
||||
/// multiple threads. Useful for debug/trace information.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ModuleSyncString(Option<Arc<String>>);
|
||||
|
||||
impl ModuleSyncString {
|
||||
/// Gets optional string reference.
|
||||
pub fn get(&self) -> Option<&str> {
|
||||
self.0.as_deref().map(|s| s.as_str())
|
||||
}
|
||||
/// Constructs the string.
|
||||
pub fn new(s: Option<&str>) -> Self {
|
||||
ModuleSyncString(s.map(|s| Arc::new(s.to_string())))
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ModuleSyncString {
|
||||
fn default() -> Self {
|
||||
ModuleSyncString(None)
|
||||
}
|
||||
}
|
||||
|
||||
/// A translated WebAssembly module, excluding the function bodies and
|
||||
/// memory initializers.
|
||||
// WARNING: when modifying, make sure that `hash_for_cache` is still valid!
|
||||
#[derive(Debug)]
|
||||
pub struct Module {
|
||||
/// A unique identifier (within this process) for this module.
|
||||
pub id: usize,
|
||||
|
||||
/// Unprocessed signatures exactly as provided by `declare_signature()`.
|
||||
pub signatures: PrimaryMap<SignatureIndex, ir::Signature>,
|
||||
|
||||
@@ -193,17 +175,17 @@ pub struct Module {
|
||||
/// WebAssembly table initializers.
|
||||
pub table_elements: Vec<TableElements>,
|
||||
|
||||
/// Module name.
|
||||
pub name: ModuleSyncString,
|
||||
|
||||
/// Function names.
|
||||
pub func_names: SecondaryMap<FuncIndex, ModuleSyncString>,
|
||||
/// WebAssembly table initializers.
|
||||
pub func_names: HashMap<FuncIndex, String>,
|
||||
}
|
||||
|
||||
impl Module {
|
||||
/// Allocates the module data structures.
|
||||
pub fn new() -> Self {
|
||||
static NEXT_ID: AtomicUsize = AtomicUsize::new(0);
|
||||
|
||||
Self {
|
||||
id: NEXT_ID.fetch_add(1, SeqCst),
|
||||
signatures: PrimaryMap::new(),
|
||||
imported_funcs: PrimaryMap::new(),
|
||||
imported_tables: PrimaryMap::new(),
|
||||
@@ -216,8 +198,7 @@ impl Module {
|
||||
exports: IndexMap::new(),
|
||||
start_func: None,
|
||||
table_elements: Vec::new(),
|
||||
name: Default::default(),
|
||||
func_names: SecondaryMap::new(),
|
||||
func_names: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::func_environ::FuncEnvironment;
|
||||
use crate::module::{Export, MemoryPlan, Module, ModuleSyncString, TableElements, TablePlan};
|
||||
use crate::module::{Export, MemoryPlan, Module, TableElements, TablePlan};
|
||||
use crate::tunables::Tunables;
|
||||
use cranelift_codegen::ir;
|
||||
use cranelift_codegen::ir::{AbiParam, ArgumentPurpose};
|
||||
@@ -371,7 +371,10 @@ impl<'data> cranelift_wasm::ModuleEnvironment<'data> for ModuleEnvironment<'data
|
||||
}
|
||||
|
||||
fn declare_func_name(&mut self, func_index: FuncIndex, name: &'data str) -> WasmResult<()> {
|
||||
self.result.module.func_names[func_index] = ModuleSyncString::new(Some(name));
|
||||
self.result
|
||||
.module
|
||||
.func_names
|
||||
.insert(func_index, name.to_string());
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user