Skip to content
This repository was archived by the owner on Oct 21, 2022. It is now read-only.

Commit 90bf032

Browse files
committed
reduce stack allocated buffered size to render this crate constrained system friendly
1 parent 3cfa7bc commit 90bf032

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ time = "0.1"
2020
default = ["std"]
2121
std = []
2222

23+
reduced-stack-buffer = []
24+
2325
#
2426
# Features for enabling non-MVP proposals.
2527
# These features should be tested as part of Travis CI build.

src/elements/primitives.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ use alloc::{string::String, vec::Vec};
22
use crate::{io, elements};
33
use super::{Error, Deserialize, Serialize};
44

5+
6+
#[cfg(feature = "reduced-stack-buffer")]
7+
const PRIMITIVES_BUFFER_LENGTH: usize = 256;
8+
9+
#[cfg(not(feature = "reduced-stack-buffer"))]
10+
const PRIMITIVES_BUFFER_LENGTH: usize = 1024;
11+
512
/// Unsigned variable-length integer, limited to 32 bits,
613
/// represented by at most 5 bytes that may contain padding 0x80 bytes.
714
#[derive(Debug, Copy, Clone, PartialEq)]
@@ -520,7 +527,7 @@ impl Deserialize for String {
520527
fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
521528
let length = u32::from(VarUint32::deserialize(reader)?) as usize;
522529
if length > 0 {
523-
String::from_utf8(buffered_read!(1024, length, reader)).map_err(|_| Error::NonUtf8String)
530+
String::from_utf8(buffered_read!(PRIMITIVES_BUFFER_LENGTH, length, reader)).map_err(|_| Error::NonUtf8String)
524531
}
525532
else {
526533
Ok(String::new())

src/elements/section.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ use super::types::Type;
2626
use super::name_section::NameSection;
2727
use super::reloc_section::RelocSection;
2828

29+
#[cfg(feature = "reduced-stack-buffer")]
30+
const ENTRIES_BUFFER_LENGTH: usize = 256;
31+
32+
#[cfg(not(feature = "reduced-stack-buffer"))]
2933
const ENTRIES_BUFFER_LENGTH: usize = 16384;
3034

3135
/// Section in the WebAssembly module.
@@ -331,7 +335,7 @@ impl Deserialize for CustomSection {
331335

332336
fn deserialize<R: io::Read>(reader: &mut R) -> Result<Self, Self::Error> {
333337
let section_length: usize = u32::from(VarUint32::deserialize(reader)?) as usize;
334-
let buf = buffered_read!(16384, section_length, reader);
338+
let buf = buffered_read!(ENTRIES_BUFFER_LENGTH, section_length, reader);
335339
let mut cursor = io::Cursor::new(&buf[..]);
336340
let name = String::deserialize(&mut cursor)?;
337341
let payload = buf[cursor.position() as usize..].to_vec();

src/elements/segment.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ const FLAG_PASSIVE: u32 = 1;
99
#[cfg(feature="bulk")]
1010
const FLAG_MEM_NONZERO: u32 = 2;
1111

12+
#[cfg(feature = "reduced-stack-buffer")]
13+
const VALUES_BUFFER_LENGTH: usize = 256;
14+
15+
#[cfg(not(feature = "reduced-stack-buffer"))]
16+
const VALUES_BUFFER_LENGTH: usize = 16384;
17+
1218
/// Entry in the element section.
1319
#[derive(Debug, Clone, PartialEq)]
1420
pub struct ElementSegment {
@@ -217,7 +223,7 @@ impl Deserialize for DataSegment {
217223
let index = VarUint32::deserialize(reader)?;
218224
let offset = InitExpr::deserialize(reader)?;
219225
let value_len = u32::from(VarUint32::deserialize(reader)?) as usize;
220-
let value_buf = buffered_read!(65536, value_len, reader);
226+
let value_buf = buffered_read!(VALUES_BUFFER_LENGTH, value_len, reader);
221227

222228
Ok(DataSegment {
223229
index: index.into(),
@@ -242,7 +248,7 @@ impl Deserialize for DataSegment {
242248
Some(InitExpr::deserialize(reader)?)
243249
};
244250
let value_len = u32::from(VarUint32::deserialize(reader)?) as usize;
245-
let value_buf = buffered_read!(65536, value_len, reader);
251+
let value_buf = buffered_read!(VALUES_BUFFER_LENGTH, value_len, reader);
246252

247253
Ok(DataSegment {
248254
index: index,

0 commit comments

Comments
 (0)