Skip to content

Instantly share code, notes, and snippets.

@dkuehr
Created May 29, 2024 13:10
Show Gist options
  • Save dkuehr/67307e5b660342bbfa27c2f6ffdcba85 to your computer and use it in GitHub Desktop.
Save dkuehr/67307e5b660342bbfa27c2f6ffdcba85 to your computer and use it in GitHub Desktop.
diff --git a/Cargo.toml b/Cargo.toml
index dc67426..0a55f9d 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -8,6 +8,7 @@ license = "Apache-2.0"
serde = { version = "1.0", features = ["derive"], optional = true }
fuzzcheck = { git = "https://github.com/openmina/fuzzcheck-rs.git", optional = true }
enum_dispatch = "0.3.7"
+psm = "0.1.21"
[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-timer = { git = "https://github.com/fusetim/wasm-timer", branch = "tim-add-missing-methods" }
diff --git a/src/store.rs b/src/store.rs
index 4f5e090..6de5380 100644
--- a/src/store.rs
+++ b/src/store.rs
@@ -5,6 +5,17 @@ use crate::{
SystemTime, TimeService,
};
+use std::sync::atomic::{AtomicUsize, Ordering};
+
+static STACK_START: AtomicUsize = AtomicUsize::new(0);
+
+fn measure_stack(context: &str) {
+ let stack_current = psm::stack_pointer();
+ let stack_used =
+ (STACK_START.load(Ordering::SeqCst) as isize - stack_current as isize).abs() / 1024;
+ println!("Stack used {}: {} kb", context, stack_used);
+}
+
/// Wraps around State and allows only immutable borrow,
/// Through `StateWrapper::get` method.
///
@@ -94,6 +105,7 @@ where
impl<State, Service, Action> Store<State, Service, Action>
where
Service: TimeService,
+ Action: std::fmt::Debug,
{
/// Creates a new store.
pub fn new(
@@ -103,6 +115,9 @@ where
initial_time: SystemTime,
initial_state: State,
) -> Self {
+ let stack_start = psm::stack_pointer() as usize;
+ STACK_START.store(stack_start, Ordering::SeqCst);
+
let initial_monotonic_time = service.monotonic_time();
let initial_time_nanos = initial_time
.duration_since(SystemTime::UNIX_EPOCH)
@@ -155,6 +170,7 @@ where
if !action.is_enabled(self.state(), self.last_action_id.into()) {
return false;
}
+
self.dispatch_enabled(action.into());
true
@@ -220,13 +236,34 @@ where
/// Runs the reducer.
#[inline(always)]
fn dispatch_reducer(&mut self, action_with_id: &ActionWithMeta<Action>) {
+ measure_stack(&format!(
+ "{}->dispatch_reducer[{}]",
+ " ".repeat(self.recursion_depth as usize),
+ self.recursion_depth
+ ));
(self.reducer)(self.state.get_mut(), action_with_id);
+ measure_stack(&format!(
+ "{}<-dispatch_reducer[{}]",
+ " ".repeat(self.recursion_depth as usize),
+ self.recursion_depth
+ ));
}
/// Runs the effects.
#[inline(always)]
fn dispatch_effects(&mut self, action_with_id: ActionWithMeta<Action>) {
+ measure_stack(&format!(
+ "{}->dispatch_effects[{}] (action={:?})",
+ " ".repeat(self.recursion_depth as usize),
+ self.recursion_depth,
+ action_with_id.action(),
+ ));
(self.effects)(self, action_with_id);
+ measure_stack(&format!(
+ "{}<-dispatch_effects[{}]",
+ " ".repeat(self.recursion_depth as usize),
+ self.recursion_depth
+ ));
}
}
@@ -234,7 +271,7 @@ impl<State, Service, Action> Clone for Store<State, Service, Action>
where
State: Clone,
Service: Clone,
- Action: Clone,
+ Action: Clone + std::fmt::Debug,
{
fn clone(&self) -> Self {
Self {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment