Skip to content

Commit

Permalink
Add tracing to sycamore-reactive and sycamore crates
Browse files Browse the repository at this point in the history
  • Loading branch information
lukechu10 committed Oct 6, 2023
1 parent 8fcaa21 commit 4f2d95e
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/sycamore-reactive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ paste = "1.0.12"
serde = { version = "1.0.188", optional = true }
slotmap = "1.0.6"
smallvec = { version = "1.11.1", features = ["union"] }
tracing = { version = "0.1.37", optional = true }

[features]
default = []
nightly = []
serde = ["dep:serde"]
trace = ["dep:tracing"]
1 change: 1 addition & 0 deletions packages/sycamore-reactive/src/effects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use crate::{create_memo, Root};
/// `create_effect` should only be used for creating **side-effects**. It is generally not
/// recommended to update signal states inside an effect. You probably should be using a
/// [`create_memo`](crate::create_memo) instead.
#[cfg_attr(debug_assertions, track_caller)]
pub fn create_effect(f: impl FnMut() + 'static) {
let f = Rc::new(RefCell::new(f));
create_memo(move || {
Expand Down
3 changes: 3 additions & 0 deletions packages/sycamore-reactive/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ pub(crate) struct ReactiveNode {
pub state: NodeState,
/// Used for DFS traversal of the reactive graph.
pub mark: Mark,
/// Keep track of where the signal was created for diagnostics.
#[cfg(debug_assertions)]
pub created_at: &'static std::panic::Location<'static>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand Down
18 changes: 18 additions & 0 deletions packages/sycamore-reactive/src/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ impl Root {
/// * `root` - The reactive root.
/// * `id` - The id associated with the reactive node.
/// `SignalId` inside the state itself.
#[cfg_attr(
all(feature = "trace", not(debug_assertions)),
tracing::instrument(skip(self))
)]
#[cfg_attr(
all(feature = "trace", debug_assertions),
tracing::instrument(skip(self), fields(created_at = self.nodes.borrow()[current].created_at.to_string()))
)]
fn run_node_update(&'static self, current: NodeId) {
debug_assert_eq!(
self.nodes.borrow()[current].state,
Expand Down Expand Up @@ -197,6 +205,8 @@ impl Root {

// Traverse reactive graph.
Self::dfs(start_node, &mut self.nodes.borrow_mut(), rev_sorted);
#[cfg(feature = "trace")]
tracing::trace!("update len: {}", rev_sorted.len());

self.mark_dependents_dirty(start_node);

Expand All @@ -220,6 +230,14 @@ impl Root {
/// Call this if `start_node` has been updated manually. This will automatically update all
/// signals that depend on `start_node`.
#[cfg_attr(debug_assertions, track_caller)]
#[cfg_attr(
all(feature = "trace", not(debug_assertions)),
tracing::instrument(skip(self))
)]
#[cfg_attr(
all(feature = "trace", debug_assertions),
tracing::instrument(skip(self), fields(created_at = self.nodes.borrow()[start_node].created_at.to_string()))
)]
pub fn propagate_updates(&'static self, start_node: NodeId) {
// Set the global root.
let prev = Root::set_global(Some(self));
Expand Down
4 changes: 4 additions & 0 deletions packages/sycamore-reactive/src/signals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ pub struct ReadSignal<T: 'static> {
pub(crate) id: NodeId,
root: &'static Root,
/// Keep track of where the signal was created for diagnostics.
/// This is also stored in the Node but we want to have access to this when accessing a
/// disposed node so we store it here as well.
#[cfg(debug_assertions)]
created_at: &'static std::panic::Location<'static>,
_phantom: PhantomData<T>,
Expand Down Expand Up @@ -129,6 +131,8 @@ pub(crate) fn create_empty_signal<T>() -> Signal<T> {
context: Vec::new(),
state: NodeState::Clean,
mark: Mark::None,
#[cfg(debug_assertions)]
created_at: std::panic::Location::caller(),
});
// Add the signal to the parent's `children` list.
let current_node = root.current_node.get();
Expand Down
1 change: 1 addition & 0 deletions packages/sycamore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ suspense = [
"sycamore-core/suspense",
]
serde = ["sycamore-reactive/serde"]
trace = ["sycamore-reactive/trace"]
wasm-bindgen-interning = ["web", "wasm-bindgen/enable-interning"]
web = ["wasm-bindgen", "web-sys", "js-sys", "sycamore-web"]

Expand Down

0 comments on commit 4f2d95e

Please sign in to comment.