|
1 | | -use super::{EnforcedLimits, StackLimits}; |
| 1 | +#[expect(deprecated)] |
| 2 | +use super::StackLimits; |
| 3 | +use super::{EnforcedLimits, StackConfig}; |
2 | 4 | use crate::core::FuelCostsProvider; |
3 | 5 | use wasmparser::WasmFeatures; |
4 | 6 |
|
5 | | -/// The default amount of stacks kept in the cache at most. |
6 | | -const DEFAULT_CACHED_STACKS: usize = 2; |
7 | | - |
8 | 7 | /// Configuration for an [`Engine`]. |
9 | 8 | /// |
10 | 9 | /// [`Engine`]: [`crate::Engine`] |
11 | 10 | #[derive(Debug, Clone)] |
12 | 11 | pub struct Config { |
13 | 12 | /// The limits set on the value stack and call stack. |
14 | | - stack_limits: StackLimits, |
15 | | - /// The amount of Wasm stacks to keep in cache at most. |
16 | | - cached_stacks: usize, |
| 13 | + pub(crate) stack: StackConfig, |
17 | 14 | /// The Wasm features used when validating or translating functions. |
18 | 15 | features: WasmFeatures, |
19 | 16 | /// Is `true` if Wasmi executions shall consume fuel. |
@@ -48,8 +45,7 @@ pub enum CompilationMode { |
48 | 45 | impl Default for Config { |
49 | 46 | fn default() -> Self { |
50 | 47 | Self { |
51 | | - stack_limits: StackLimits::default(), |
52 | | - cached_stacks: DEFAULT_CACHED_STACKS, |
| 48 | + stack: StackConfig::default(), |
53 | 49 | features: Self::default_features(), |
54 | 50 | consume_fuel: false, |
55 | 51 | ignore_custom_sections: false, |
@@ -84,29 +80,91 @@ impl Config { |
84 | 80 | } |
85 | 81 |
|
86 | 82 | /// Sets the [`StackLimits`] for the [`Config`]. |
87 | | - pub fn set_stack_limits(&mut self, stack_limits: StackLimits) -> &mut Self { |
88 | | - self.stack_limits = stack_limits; |
| 83 | + #[deprecated( |
| 84 | + since = "0.51.0", |
| 85 | + note = "\ |
| 86 | + use `Config::set_{min,max}_stack_height`, \ |
| 87 | + `Config::max_recursion_depth` instead" |
| 88 | + )] |
| 89 | + #[expect(deprecated)] |
| 90 | + pub fn set_stack_limits(&mut self, limits: StackLimits) -> &mut Self { |
| 91 | + self.set_min_stack_height(limits.initial_value_stack_height); |
| 92 | + self.set_max_stack_height(limits.maximum_value_stack_height); |
| 93 | + self.set_max_recursion_depth(limits.maximum_recursion_depth); |
89 | 94 | self |
90 | 95 | } |
91 | 96 |
|
92 | | - /// Returns the [`StackLimits`] of the [`Config`]. |
93 | | - pub(super) fn stack_limits(&self) -> StackLimits { |
94 | | - self.stack_limits |
95 | | - } |
96 | | - |
97 | 97 | /// Sets the maximum amount of cached stacks for reuse for the [`Config`]. |
98 | 98 | /// |
99 | 99 | /// # Note |
100 | 100 | /// |
101 | 101 | /// Defaults to 2. |
| 102 | + #[deprecated(since = "0.51.0", note = "use `Config::set_max_cached_stacks` instead")] |
102 | 103 | pub fn set_cached_stacks(&mut self, amount: usize) -> &mut Self { |
103 | | - self.cached_stacks = amount; |
| 104 | + self.set_max_cached_stacks(amount); |
| 105 | + self |
| 106 | + } |
| 107 | + |
| 108 | + /// Sets the maximum recursion depth of the [`Engine`]'s stack during execution. |
| 109 | + /// |
| 110 | + /// # Note |
| 111 | + /// |
| 112 | + /// An execution traps if it exceeds this limits. |
| 113 | + /// |
| 114 | + /// [`Engine`]: [`crate::Engine`] |
| 115 | + pub fn set_max_recursion_depth(&mut self, value: usize) -> &mut Self { |
| 116 | + self.stack.set_max_recursion_depth(value); |
| 117 | + self |
| 118 | + } |
| 119 | + |
| 120 | + /// Sets the minimum (or initial) height of the [`Engine`]'s value stack in bytes. |
| 121 | + /// |
| 122 | + /// # Note |
| 123 | + /// |
| 124 | + /// - Lower initial heights may improve memory consumption. |
| 125 | + /// - Higher initial heights may improve cold start times. |
| 126 | + /// |
| 127 | + /// # Panics |
| 128 | + /// |
| 129 | + /// If `value` is greater than the current maximum height of the value stack. |
| 130 | + /// |
| 131 | + /// [`Engine`]: [`crate::Engine`] |
| 132 | + pub fn set_min_stack_height(&mut self, value: usize) -> &mut Self { |
| 133 | + if self.stack.set_min_stack_height(value).is_err() { |
| 134 | + let max = self.stack.max_stack_height(); |
| 135 | + panic!("minimum stack height exceeds maximum: min={value}, max={max}"); |
| 136 | + } |
104 | 137 | self |
105 | 138 | } |
106 | 139 |
|
107 | | - /// Returns the maximum amount of cached stacks for reuse of the [`Config`]. |
108 | | - pub(super) fn cached_stacks(&self) -> usize { |
109 | | - self.cached_stacks |
| 140 | + /// Sets the maximum height of the [`Engine`]'s value stack in bytes. |
| 141 | + /// |
| 142 | + /// # Note |
| 143 | + /// |
| 144 | + /// An execution traps if it exceeds this limits. |
| 145 | + /// |
| 146 | + /// # Panics |
| 147 | + /// |
| 148 | + /// If `value` is less than the current minimum height of the value stack. |
| 149 | + /// |
| 150 | + /// [`Engine`]: [`crate::Engine`] |
| 151 | + pub fn set_max_stack_height(&mut self, value: usize) -> &mut Self { |
| 152 | + if self.stack.set_max_stack_height(value).is_err() { |
| 153 | + let max = self.stack.min_stack_height(); |
| 154 | + panic!("maximum stack height is lower than minimum: min={value}, max={max}"); |
| 155 | + } |
| 156 | + self |
| 157 | + } |
| 158 | + |
| 159 | + /// Sets the maximum number of cached stacks for reuse for the [`Config`]. |
| 160 | + /// |
| 161 | + /// # Note |
| 162 | + /// |
| 163 | + /// - A higher value may improve execution performance. |
| 164 | + /// - A lower value may improve memory consumption. |
| 165 | + pub fn set_max_cached_stacks(&mut self, value: usize) -> &mut Self { |
| 166 | + self.stack.set_max_cached_stacks(value); |
| 167 | + self |
110 | 168 | } |
111 | 169 |
|
112 | 170 | /// Enable or disable the [`mutable-global`] Wasm proposal for the [`Config`]. |
|
0 commit comments