Skip to content

Commit

Permalink
Fragment debug_id u16 only exists in debug, prod will format mem address
Browse files Browse the repository at this point in the history
  • Loading branch information
Mitchell Hentges committed Jun 4, 2016
1 parent d890453 commit 43396c0
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 12 deletions.
71 changes: 59 additions & 12 deletions components/layout/fragment.rs
Expand Up @@ -22,6 +22,7 @@ use incremental::{RECONSTRUCT_FLOW, RestyleDamage};
use inline::{FIRST_FRAGMENT_OF_ELEMENT, InlineFragmentContext, InlineFragmentNodeInfo};
use inline::{InlineMetrics, LAST_FRAGMENT_OF_ELEMENT};
use ipc_channel::ipc::IpcSender;
#[cfg(debug_assertions)]
use layout_debug;
use model::{self, IntrinsicISizes, IntrinsicISizesContribution, MaybeAuto, specified};
use msg::constellation_msg::PipelineId;
Expand Down Expand Up @@ -118,7 +119,9 @@ pub struct Fragment {
pub flags: FragmentFlags,

/// A debug ID that is consistent for the life of this fragment (via transform etc).
pub debug_id: u16,
/// This ID should not be considered stable across multiple layouts or fragment
/// manipulations.
debug_id: DebugId,

/// The ID of the StackingContext that contains this fragment. This is initialized
/// to 0, but it assigned during the collect_stacking_contexts phase of display
Expand All @@ -129,7 +132,7 @@ pub struct Fragment {
impl Encodable for Fragment {
fn encode<S: Encoder>(&self, e: &mut S) -> Result<(), S::Error> {
e.emit_struct("fragment", 0, |e| {
try!(e.emit_struct_field("id", 0, |e| self.debug_id().encode(e)));
try!(e.emit_struct_field("id", 0, |e| self.debug_id.encode(e)));
try!(e.emit_struct_field("border_box", 1, |e| self.border_box.encode(e)));
e.emit_struct_field("margin", 2, |e| self.margin.encode(e))
})
Expand Down Expand Up @@ -809,7 +812,7 @@ impl Fragment {
inline_context: None,
pseudo: node.get_pseudo_element_type().strip(),
flags: FragmentFlags::empty(),
debug_id: layout_debug::generate_unique_debug_id(),
debug_id: DebugId::new(),
stacking_context_id: StackingContextId::new(0),
}
}
Expand Down Expand Up @@ -838,17 +841,11 @@ impl Fragment {
inline_context: None,
pseudo: pseudo,
flags: FragmentFlags::empty(),
debug_id: layout_debug::generate_unique_debug_id(),
debug_id: DebugId::new(),
stacking_context_id: StackingContextId::new(0),
}
}

/// Returns a debug ID of this fragment. This ID should not be considered stable across
/// multiple layouts or fragment manipulations.
pub fn debug_id(&self) -> u16 {
self.debug_id
}

/// Transforms this fragment into another fragment of the given type, with the given size,
/// preserving all the other data.
pub fn transform(&self, size: LogicalSize<Au>, info: SpecificFragmentInfo)
Expand All @@ -872,7 +869,7 @@ impl Fragment {
inline_context: self.inline_context.clone(),
pseudo: self.pseudo.clone(),
flags: FragmentFlags::empty(),
debug_id: self.debug_id,
debug_id: self.debug_id.clone(),
stacking_context_id: StackingContextId::new(0),
}
}
Expand Down Expand Up @@ -2624,7 +2621,7 @@ impl fmt::Debug for Fragment {

write!(f, "{}({}) [{:?}] border_box={:?}{}{}{}",
self.specific.get_type(),
self.debug_id(),
self.debug_id,
self.specific,
self.border_box,
border_padding_string,
Expand Down Expand Up @@ -2780,3 +2777,53 @@ pub struct SpeculatedInlineContentEdgeOffsets {
pub start: Au,
pub end: Au,
}

#[cfg(not(debug_assertions))]
#[derive(Clone)]
struct DebugId;

#[cfg(debug_assertions)]
#[derive(Clone)]
struct DebugId(u16);

#[cfg(not(debug_assertions))]
impl DebugId {
pub fn new() -> DebugId {
DebugId
}
}

#[cfg(debug_assertions)]
impl DebugId {
pub fn new() -> DebugId {
DebugId(layout_debug::generate_unique_debug_id())
}
}

#[cfg(not(debug_assertions))]
impl fmt::Display for DebugId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:p}", &self)
}
}

#[cfg(debug_assertions)]
impl fmt::Display for DebugId {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}

#[cfg(not(debug_assertions))]
impl Encodable for DebugId {
fn encode<S: Encoder>(&self, e: &mut S) -> Result<(), S::Error> {
e.emit_str(&format!("{:p}", &self))
}
}

#[cfg(debug_assertions)]
impl Encodable for DebugId {
fn encode<S: Encoder>(&self, e: &mut S) -> Result<(), S::Error> {
e.emit_u16(self.0)
}
}
3 changes: 3 additions & 0 deletions components/layout/layout_debug.rs
Expand Up @@ -15,10 +15,12 @@ use std::borrow::ToOwned;
use std::cell::RefCell;
use std::fs::File;
use std::io::Write;
#[cfg(debug_assertions)]
use std::sync::atomic::{ATOMIC_USIZE_INIT, AtomicUsize, Ordering};

thread_local!(static STATE_KEY: RefCell<Option<State>> = RefCell::new(None));

#[cfg(debug_assertions)]
static DEBUG_ID_COUNTER: AtomicUsize = ATOMIC_USIZE_INIT;

pub struct Scope;
Expand Down Expand Up @@ -96,6 +98,7 @@ impl Drop for Scope {
/// Generate a unique ID. This is used for items such as Fragment
/// which are often reallocated but represent essentially the
/// same data.
#[cfg(debug_assertions)]
pub fn generate_unique_debug_id() -> u16 {
DEBUG_ID_COUNTER.fetch_add(1, Ordering::SeqCst) as u16
}
Expand Down

0 comments on commit 43396c0

Please sign in to comment.