Skip to content

Commit ffb78f7

Browse files
fix: borrow export name from bytecode instead of allocating it
Signed-off-by: Florian Hartung <florian.hartung@dlr.de>
1 parent 03960dc commit ffb78f7

File tree

3 files changed

+17
-21
lines changed

3 files changed

+17
-21
lines changed

src/core/reader/types/export.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
use alloc::borrow::ToOwned;
2-
use alloc::string::String;
3-
41
use crate::core::indices::{FuncIdx, GlobalIdx, MemIdx, TableIdx};
52
use crate::core::reader::types::import::ImportDesc;
63
use crate::core::reader::WasmReader;
@@ -9,14 +6,14 @@ use crate::{ValidationError, ValidationInfo};
96
use super::ExternType;
107

118
#[derive(Debug, Clone)]
12-
pub struct Export {
13-
pub name: String,
9+
pub struct Export<'wasm> {
10+
pub name: &'wasm str,
1411
pub desc: ExportDesc,
1512
}
1613

17-
impl Export {
18-
pub fn read(wasm: &mut WasmReader) -> Result<Self, ValidationError> {
19-
let name = wasm.read_name()?.to_owned();
14+
impl<'wasm> Export<'wasm> {
15+
pub fn read(wasm: &mut WasmReader<'wasm>) -> Result<Self, ValidationError> {
16+
let name = wasm.read_name()?;
2017
let desc = ExportDesc::read(wasm)?;
2118
Ok(Export { name, desc })
2219
}

src/execution/store/mod.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::core::indices::TypeIdx;
99
use crate::core::reader::span::Span;
1010
use crate::core::reader::types::data::{DataModeActive, DataSegment};
1111
use crate::core::reader::types::element::{ActiveElem, ElemItems, ElemMode, ElemType};
12-
use crate::core::reader::types::export::{Export, ExportDesc};
12+
use crate::core::reader::types::export::ExportDesc;
1313
use crate::core::reader::types::global::{Global, GlobalType};
1414
use crate::core::reader::types::{
1515
ExternType, FuncType, ImportSubTypeRelation, MemType, ResultType, TableType,
@@ -22,6 +22,7 @@ use crate::resumable::{
2222
Dormitory, FreshResumableRef, InvokedResumableRef, Resumable, ResumableRef, RunState,
2323
};
2424
use crate::{RefType, RuntimeError, ValidationInfo};
25+
use alloc::borrow::ToOwned;
2526
use alloc::collections::btree_map::BTreeMap;
2627
use alloc::string::String;
2728
use alloc::sync::Arc;
@@ -287,19 +288,17 @@ impl<'b, T: Config> Store<'b, T> {
287288
let export_insts: BTreeMap<String, ExternVal> = module
288289
.exports
289290
.iter()
290-
.map(|Export { name, desc }| {
291+
.map(|export| {
291292
let module_inst = self.modules.get(module_addr);
292-
let value = match desc {
293-
ExportDesc::Func(func_idx) => {
294-
ExternVal::Func(module_inst.func_addrs[*func_idx])
295-
}
296-
ExportDesc::Table(table_idx) => ExternVal::Table(table_addrs_mod[*table_idx]),
297-
ExportDesc::Mem(mem_idx) => ExternVal::Mem(mem_addrs_mod[*mem_idx]),
293+
let value = match export.desc {
294+
ExportDesc::Func(func_idx) => ExternVal::Func(module_inst.func_addrs[func_idx]),
295+
ExportDesc::Table(table_idx) => ExternVal::Table(table_addrs_mod[table_idx]),
296+
ExportDesc::Mem(mem_idx) => ExternVal::Mem(mem_addrs_mod[mem_idx]),
298297
ExportDesc::Global(global_idx) => {
299-
ExternVal::Global(module_inst.global_addrs[*global_idx])
298+
ExternVal::Global(module_inst.global_addrs[global_idx])
300299
}
301300
};
302-
(String::from(name), value)
301+
(export.name.to_owned(), value)
303302
})
304303
.collect();
305304

src/validation/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub struct ValidationInfo<'bytecode> {
4444
pub(crate) tables: Vec<TableType>,
4545
pub(crate) memories: Vec<MemType>,
4646
pub(crate) globals: Vec<Global>,
47-
pub(crate) exports: Vec<Export>,
47+
pub(crate) exports: Vec<Export<'bytecode>>,
4848
/// Each block contains the validated code section and the stp corresponding to
4949
/// the beginning of that code section
5050
pub(crate) func_blocks_stps: Vec<(Span, usize)>,
@@ -61,10 +61,10 @@ fn validate_exports(validation_info: &ValidationInfo) -> Result<(), ValidationEr
6161
let mut found_export_names: btree_set::BTreeSet<&str> = btree_set::BTreeSet::new();
6262
use crate::core::reader::types::export::ExportDesc::*;
6363
for export in &validation_info.exports {
64-
if found_export_names.contains(export.name.as_str()) {
64+
if found_export_names.contains(export.name) {
6565
return Err(ValidationError::DuplicateExportName);
6666
}
67-
found_export_names.insert(export.name.as_str());
67+
found_export_names.insert(export.name);
6868
match export.desc {
6969
Func(func_idx) => {
7070
if validation_info.functions.len()

0 commit comments

Comments
 (0)