Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache svg::Event across frames #58

Merged
merged 2 commits into from
Feb 22, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 67 additions & 37 deletions src/flamegraph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,21 @@ fn override_or_add_attributes<'a>(
}
}

struct Rectangle {
x1: usize,
y1: usize,
x2: usize,
y2: usize,
}
impl Rectangle {
fn width(&self) -> usize {
self.x2 - self.x1
}
fn height(&self) -> usize {
self.y2 - self.y1
}
}

pub fn from_sorted_lines<'a, I, W>(opt: Options, lines: I, writer: W) -> quick_xml::Result<()>
where
I: IntoIterator<Item = &'a str>,
Expand Down Expand Up @@ -203,8 +218,10 @@ where
// frames loop.
let mut thread_rng = rand::thread_rng();

// struct to reuse accross loops to avoid allocations
let mut event_start = Event::Start({ BytesStart::owned_name("g") });
// structs to reuse accross loops to avoid allocations
let mut cache_g = Event::Start({ BytesStart::owned_name("g") });
let mut cache_a = Event::Start({ BytesStart::owned_name("a") });
let mut cache_rect = Event::Empty(BytesStart::owned_name("rect"));

// draw frames
let mut samples_txt_buffer = num_format::Buffer::default();
Expand All @@ -223,6 +240,7 @@ where
(y1, y2)
}
};
let rect = Rectangle { x1, y1, x2, y2 };

let samples = frame.end_time - frame.start_time;

Expand Down Expand Up @@ -251,7 +269,7 @@ where
let frame_attributes = override_or_add_attributes(&buffer[info], frame_attributes);
let href_is_some = frame_attributes.href.is_some();

if let Event::Start(ref mut g) = event_start {
if let Event::Start(ref mut g) = cache_g {
// clear the BytesStart
g.clear_attributes();

Expand All @@ -269,9 +287,11 @@ where
if let Some(extra) = frame_attributes.g_extra {
g.extend_attributes(extra.iter().map(|(k, v)| (k.as_str(), v.as_str())));
}
} else {
unreachable!("cache wrapper was of wrong type: {:?}", cache_g);
}

svg.write_event(&event_start)?;
svg.write_event(&cache_g)?;

svg.write_event(Event::Start(BytesStart::borrowed_name(b"title")))?;
svg.write_event(Event::Text(BytesText::from_plain_str(
Expand All @@ -280,38 +300,44 @@ where
svg.write_event(Event::End(BytesEnd::borrowed(b"title")))?;

if let Some(href) = frame_attributes.href {
svg.write_event(Event::Start({
let mut a = BytesStart::borrowed_name(b"a").with_attributes(args!(
if let Event::Start(ref mut a) = cache_a {
// clear the BytesStart
a.clear_attributes();

a.extend_attributes(args!(
"xlink:href" => href,
"target" => frame_attributes.target
));
if let Some(extra) = frame_attributes.a_extra {
a.extend_attributes(extra.iter().map(|(k, v)| (k.as_str(), v.as_str())));
}
a
}))?;
} else {
unreachable!("cache wrapper was of wrong type: {:?}", cache_a);
}

svg.write_event(&cache_a)?;
}

if frame.location.function == "--" {
filled_rectangle(&mut svg, &mut buffer, x1, x2, y1, y2, color::VDGREY)?;
// select the color of the rectangle
let color = if frame.location.function == "--" {
color::VDGREY
} else if frame.location.function == "-" {
filled_rectangle(&mut svg, &mut buffer, x1, x2, y1, y2, color::DGREY)?;
color::DGREY
} else if let Some(ref mut palette_map) = palette_map {
let color = palette_map.find_color_for(&frame.location.function, |name| {
palette_map.find_color_for(&frame.location.function, |name| {
color::color(opt.colors, opt.hash, name, &mut thread_rng)
});
filled_rectangle(&mut svg, &mut buffer, x1, x2, y1, y2, color)?;
})
} else {
let color = color::color(
color::color(
opt.colors,
opt.hash,
frame.location.function,
&mut thread_rng,
);
filled_rectangle(&mut svg, &mut buffer, x1, x2, y1, y2, color)?;
)
};
filled_rectangle(&mut svg, &mut buffer, &rect, color, &mut cache_rect)?;

let fitchars = ((x2 - x1) as f64 / (FONTSIZE as f64 * FONTWIDTH)).trunc() as usize;
let fitchars = (rect.width() as f64 / (FONTSIZE as f64 * FONTWIDTH)).trunc() as usize;
let text: svg::TextArgument = if fitchars >= 3 {
// room for one char plus two dots
let f = deannotate(&frame.location.function);
Expand All @@ -335,14 +361,15 @@ where
"".into()
};

// write the text
svg::write_str(
&mut svg,
&mut buffer,
svg::TextItem {
color: "rgb(0, 0, 0)",
size: FONTSIZE,
x: x1 as f64 + 3.0,
y: 3.0 + (y1 + y2) as f64 / 2.0,
x: rect.x1 as f64 + 3.0,
y: 3.0 + (rect.y1 + rect.y2) as f64 / 2.0,
text,
location: None,
extra: None,
Expand Down Expand Up @@ -413,27 +440,30 @@ fn deannotate(f: &str) -> &str {
fn filled_rectangle<W: Write>(
svg: &mut Writer<W>,
buffer: &mut StrStack,
x1: usize,
x2: usize,
y1: usize,
y2: usize,
rect: &Rectangle,
color: (u8, u8, u8),
cache_rect: &mut Event,
) -> quick_xml::Result<usize> {
let x = write!(buffer, "{}", x1);
let y = write!(buffer, "{}", y1);
let width = write!(buffer, "{}", x2 - x1);
let height = write!(buffer, "{}", y2 - y1);
let x = write!(buffer, "{}", rect.x1);
let y = write!(buffer, "{}", rect.y1);
let width = write!(buffer, "{}", rect.width());
let height = write!(buffer, "{}", rect.height());
let color = write!(buffer, "rgb({},{},{})", color.0, color.1, color.2);

svg.write_event(Event::Empty(
BytesStart::borrowed_name(b"rect").with_attributes(args!(
"x" => &buffer[x],
"y" => &buffer[y],
"width" => &buffer[width],
"height" => &buffer[height],
"fill" => &buffer[color]
)),
))
if let Event::Empty(bytes_start) = cache_rect {
// clear the state
bytes_start.clear_attributes();
bytes_start.extend_attributes(args!(
"x" => &buffer[x],
"y" => &buffer[y],
"width" => &buffer[width],
"height" => &buffer[height],
"fill" => &buffer[color]
));
} else {
unreachable!("cache wrapper was of wrong type: {:?}", cache_rect);
}
svg.write_event(&cache_rect)
}

impl Default for Direction {
Expand Down