Skip to content

Commit

Permalink
performance improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
mattrutherford committed May 18, 2020
1 parent 10a07a6 commit cff0aa2
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
3 changes: 0 additions & 3 deletions frame/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,6 @@ pub use self::storage::{
pub use self::dispatch::{Parameter, Callable, IsSubType};
pub use sp_runtime::{self, ConsensusEngineId, print, traits::Printable};

/// Indicates whether to run traces in wasm
pub static mut WASM_TRACING_ENABLED: bool = true;

/// A type that cannot be instantiated.
#[derive(Debug)]
pub enum Never {}
Expand Down
11 changes: 6 additions & 5 deletions frame/support/src/wasm_tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
//!
//! Facilitated by `sp_io::wasm_tracing`

/// Indicates whether to run traces in wasm
pub static mut WASM_TRACING_ENABLED: bool = true;

/// This holds a tracing span id and is to signal on drop that a tracing span has exited.
/// It must be bound to a named variable eg. `_span_guard`.
///
Expand All @@ -33,9 +36,7 @@ impl TracingSpanGuard {

impl Drop for TracingSpanGuard {
fn drop(&mut self) {
if self.0 > 0 {
crate::sp_io::wasm_tracing::exit_span(self.0);
}
crate::sp_io::wasm_tracing::exit_span(self.0);
}
}

Expand All @@ -51,15 +52,15 @@ impl Drop for TracingSpanGuard {
macro_rules! enter_span {
( $name:expr ) => {
#[cfg(not(feature = "std"))]
let __span_id__ = match unsafe { frame_support::WASM_TRACING_ENABLED } {
let __span_id__ = match unsafe { $crate::wasm_tracing::WASM_TRACING_ENABLED } {
false => $crate::wasm_tracing::TracingSpanGuard::new(0),
true => {
let __id__ = $crate::sp_io::wasm_tracing::enter_span(
module_path!(),
$name
);
if __id__ == 0 {
unsafe { frame_support::WASM_TRACING_ENABLED = false; }
unsafe { $crate::wasm_tracing ::WASM_TRACING_ENABLED = false; }
}
$crate::wasm_tracing::TracingSpanGuard::new(__id__)
}
Expand Down
1 change: 1 addition & 0 deletions primitives/io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,7 @@ pub trait WasmTracing {
target: "sp_tracing",
"Notify to runtime that tracing is disabled."
);
// Zero indicates tracing is disabled in wasm
return 0
}
let proxy = match self.extension::<TracingProxyExt>() {
Expand Down
8 changes: 6 additions & 2 deletions primitives/tracing/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ impl TracingProxy {
pub fn new() -> TracingProxy {
let spans: Vec<(u64, rent_span::SpanAndGuard)> = Vec::new();
TracingProxy {
next_id: 0,
// Important to start at 1, 0 will not record a trace
next_id: 1,
spans,
}
}
Expand Down Expand Up @@ -97,7 +98,10 @@ impl TracingProxy {
/// Exit a span by dropping it along with it's associated guard.
pub fn exit_span(&mut self, id: u64) {
if self.spans.last().map(|l| id > l.0).unwrap_or(true) {
log::warn!("Span id not found {}", id);
// 0 is special case to indicate no wasm tracing
if id != 0 {
log::warn!("Span id not found {}", id);
}
return;
}
let mut last_span = self.spans.pop().expect("Just checked that there is an element to pop");
Expand Down

0 comments on commit cff0aa2

Please sign in to comment.