Skip to content

Commit

Permalink
update quick-xml to version 0.24
Browse files Browse the repository at this point in the history
  • Loading branch information
crepererum committed Sep 19, 2022
1 parent 8463105 commit 24ab2e9
Show file tree
Hide file tree
Showing 34 changed files with 156 additions and 111 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ itoa = "1"
log = "0.4"
num_cpus = { version = "1.10", optional = true }
num-format = { version = "0.4", default-features = false }
quick-xml = { version = "0.23", default-features = false }
quick-xml = { version = "0.24", default-features = false }
rgb = "0.8.13"
str_stack = "0.1"
clap = { version = "3.0.1", optional = true, features = ["derive"] }
Expand Down
36 changes: 17 additions & 19 deletions src/flamegraph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ where
extra: None,
},
)?;
svg.write_event(Event::End(BytesEnd::borrowed(b"svg")))?;
svg.write_event(Event::End(BytesEnd::new("svg")))?;
svg.write_event(Event::Eof)?;
return Err(quick_xml::Error::Io(io::Error::new(
io::ErrorKind::InvalidData,
Expand Down Expand Up @@ -526,23 +526,21 @@ where
let mut thread_rng = rand::thread_rng();

// structs to reuse across 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"));
let cache_g_end = Event::End(BytesEnd::borrowed(b"g"));
let cache_a_end = Event::End(BytesEnd::borrowed(b"a"));
let mut cache_g = Event::Start(BytesStart::new("g"));
let mut cache_a = Event::Start(BytesStart::new("a"));
let mut cache_rect = Event::Empty(BytesStart::new("rect"));
let cache_g_end = Event::End(BytesEnd::new("g"));
let cache_a_end = Event::End(BytesEnd::new("a"));

// create frames container
let container_x = format!("{}", XPAD);
let container_width = format!("{}", image_width as usize - XPAD - XPAD);
svg.write_event(Event::Start(
BytesStart::borrowed_name(b"svg").with_attributes(vec![
("id", "frames"),
("x", &container_x),
("width", &container_width),
("total_samples", &format!("{}", timemax)),
]),
))?;
svg.write_event(Event::Start(BytesStart::new("svg").with_attributes(vec![
("id", "frames"),
("x", &container_x),
("width", &container_width),
("total_samples", &format!("{}", timemax)),
])))?;

// draw frames
let mut samples_txt_buffer = num_format::Buffer::default();
Expand Down Expand Up @@ -625,9 +623,9 @@ where
&buffer[info],
)?;

svg.write_event(Event::Start(BytesStart::borrowed_name(b"title")))?;
svg.write_event(Event::Text(BytesText::from_plain_str(title)))?;
svg.write_event(Event::End(BytesEnd::borrowed(b"title")))?;
svg.write_event(Event::Start(BytesStart::new("title")))?;
svg.write_event(Event::Text(BytesText::new(title)))?;
svg.write_event(Event::End(BytesEnd::new("title")))?;

// select the color of the rectangle
let color = if frame.location.function == "--" {
Expand Down Expand Up @@ -710,8 +708,8 @@ where
}
}

svg.write_event(Event::End(BytesEnd::borrowed(b"svg")))?;
svg.write_event(Event::End(BytesEnd::borrowed(b"svg")))?;
svg.write_event(Event::End(BytesEnd::new("svg")))?;
svg.write_event(Event::End(BytesEnd::new("svg")))?;
svg.write_event(Event::Eof)?;

svg.into_inner().flush()?;
Expand Down
105 changes: 45 additions & 60 deletions src/flamegraph/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::cell::RefCell;
use std::io::prelude::*;
use std::iter;

use quick_xml::events::{BytesCData, BytesEnd, BytesStart, BytesText, Event};
use quick_xml::events::{BytesCData, BytesDecl, BytesEnd, BytesStart, BytesText, Event};
use quick_xml::Writer;
use str_stack::StrStack;

Expand Down Expand Up @@ -62,27 +62,25 @@ pub fn write_header<W>(
where
W: Write,
{
svg.write(br#"<?xml version="1.0" standalone="no"?>"#)?;
svg.write(br#"<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">"#)?;
svg.write_event(Event::Decl(BytesDecl::new("1.0", None, Some("no"))))?;
svg.write_event(Event::DocType(BytesText::from_escaped(r#"svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd""#)))?;
let imagewidth = opt.image_width.unwrap_or(super::DEFAULT_IMAGE_WIDTH);
svg.write_event(Event::Start(
BytesStart::borrowed_name(b"svg").with_attributes(vec![
("version", "1.1"),
("width", &*format!("{}", imagewidth)),
("height", &*format!("{}", imageheight)),
("onload", "init(evt)"),
("viewBox", &*format!("0 0 {} {}", imagewidth, imageheight)),
("xmlns", "http://www.w3.org/2000/svg"),
("xmlns:xlink", "http://www.w3.org/1999/xlink"),
("xmlns:fg", "http://github.com/jonhoo/inferno"),
]),
))?;
svg.write_event(Event::Comment(BytesText::from_plain_str(
svg.write_event(Event::Start(BytesStart::new("svg").with_attributes(vec![
("version", "1.1"),
("width", &*format!("{}", imagewidth)),
("height", &*format!("{}", imageheight)),
("onload", "init(evt)"),
("viewBox", &*format!("0 0 {} {}", imagewidth, imageheight)),
("xmlns", "http://www.w3.org/2000/svg"),
("xmlns:xlink", "http://www.w3.org/1999/xlink"),
("xmlns:fg", "http://github.com/jonhoo/inferno"),
])))?;
svg.write_event(Event::Comment(BytesText::new(
"Flame graph stack visualization. \
See https://github.com/brendangregg/FlameGraph for latest version, \
and http://www.brendangregg.com/flamegraphs.html for examples.",
)))?;
svg.write_event(Event::Comment(BytesText::from_plain_str(
svg.write_event(Event::Comment(BytesText::new(
format!("NOTES: {}", opt.notes).as_str(),
)))?;
Ok(())
Expand All @@ -96,28 +94,22 @@ pub(super) fn write_prelude<'a, W>(
where
W: Write,
{
svg.write_event(Event::Start(BytesStart::borrowed_name(b"defs")))?;
svg.write_event(Event::Start(BytesStart::borrowed(
br#"linearGradient id="background" y1="0" y2="1" x1="0" x2="0""#,
svg.write_event(Event::Start(BytesStart::new("defs")))?;
svg.write_event(Event::Start(BytesStart::from_content(
r#"linearGradient id="background" y1="0" y2="1" x1="0" x2="0""#,
"linearGradient".len(),
)))?;
svg.write_event(Event::Empty(
BytesStart::borrowed_name(b"stop").with_attributes(
iter::once(("stop-color", &*style_options.bgcolor1))
.chain(iter::once(("offset", "5%"))),
),
))?;
svg.write_event(Event::Empty(
BytesStart::borrowed_name(b"stop").with_attributes(
iter::once(("stop-color", &*style_options.bgcolor2))
.chain(iter::once(("offset", "95%"))),
),
))?;
svg.write_event(Event::End(BytesEnd::borrowed(b"linearGradient")))?;
svg.write_event(Event::End(BytesEnd::borrowed(b"defs")))?;
svg.write_event(Event::Empty(BytesStart::new("stop").with_attributes(
iter::once(("stop-color", &*style_options.bgcolor1)).chain(iter::once(("offset", "5%"))),
)))?;
svg.write_event(Event::Empty(BytesStart::new("stop").with_attributes(
iter::once(("stop-color", &*style_options.bgcolor2)).chain(iter::once(("offset", "95%"))),
)))?;
svg.write_event(Event::End(BytesEnd::new("linearGradient")))?;
svg.write_event(Event::End(BytesEnd::new("defs")))?;

svg.write_event(Event::Start(
BytesStart::borrowed_name(b"style").with_attributes(iter::once(("type", "text/css"))),
BytesStart::new("style").with_attributes(iter::once(("type", "text/css"))),
))?;

let font_type: Cow<str> = if GENERIC_FONT_FAMILIES.contains(&opt.font_type.as_str()) {
Expand All @@ -127,29 +119,26 @@ where
};

let titlesize = &opt.font_size + 5;
svg.write_event(Event::Text(BytesText::from_escaped_str(&format!(
svg.write_event(Event::Text(BytesText::from_escaped(&format!(
"
text {{ font-family:{}; font-size:{}px; fill:rgb(0,0,0); }}
#title {{ text-anchor:middle; font-size:{}px; }}
",
font_type, &opt.font_size, titlesize,
))))?;
if let Some(strokecolor) = &style_options.strokecolor {
svg.write_event(Event::Text(BytesText::from_escaped_str(&format!(
svg.write_event(Event::Text(BytesText::from_escaped(&format!(
"#frames > g > rect {{ stroke:{}; stroke-width:1; }}\n",
strokecolor
))))?;
}
svg.write_event(Event::Text(BytesText::from_escaped_str(include_str!(
"flamegraph.css"
))))?;
svg.write_event(Event::End(BytesEnd::borrowed(b"style")))?;
svg.write_event(Event::Text(BytesText::from_escaped(include_str!("flamegraph.css"))))?;
svg.write_event(Event::End(BytesEnd::new("style")))?;

svg.write_event(Event::Start(
BytesStart::borrowed_name(b"script")
.with_attributes(iter::once(("type", "text/ecmascript"))),
BytesStart::new("script").with_attributes(iter::once(("type", "text/ecmascript"))),
))?;
svg.write_event(Event::CData(BytesCData::from_str(&format!(
svg.write_event(Event::CData(BytesCData::new(&format!(
"
var nametype = {};
var fontsize = {};
Expand All @@ -169,21 +158,17 @@ text {{ font-family:{}; font-size:{}px; fill:rgb(0,0,0); }}
opt.text_truncate_direction == TextTruncateDirection::Right
))))?;
if !opt.no_javascript {
svg.write_event(Event::CData(BytesCData::from_str(include_str!(
"flamegraph.js"
))))?;
svg.write_event(Event::CData(BytesCData::new(include_str!("flamegraph.js"))))?;
}
svg.write_event(Event::End(BytesEnd::borrowed(b"script")))?;
svg.write_event(Event::End(BytesEnd::new("script")))?;

svg.write_event(Event::Empty(
BytesStart::borrowed_name(b"rect").with_attributes(vec![
("x", "0"),
("y", "0"),
("width", "100%"),
("height", &*format!("{}", style_options.imageheight)),
("fill", "url(#background)"),
]),
))?;
svg.write_event(Event::Empty(BytesStart::new("rect").with_attributes(vec![
("x", "0"),
("y", "0"),
("width", "100%"),
("height", &*format!("{}", style_options.imageheight)),
("fill", "url(#background)"),
])))?;

// We don't care too much about allocating just for the prelude
let mut buf = StrStack::new();
Expand Down Expand Up @@ -284,7 +269,7 @@ where

thread_local! {
// reuse for all text elements to avoid allocations
static TEXT: RefCell<Event<'static>> = RefCell::new(Event::Start(BytesStart::owned_name("text")))
static TEXT: RefCell<Event<'static>> = RefCell::new(Event::Start(BytesStart::new("text")))
};
TEXT.with(|start_event| {
if let Event::Start(ref mut text) = *start_event.borrow_mut() {
Expand All @@ -304,8 +289,8 @@ where
TextArgument::String(ref s) => s,
TextArgument::FromBuffer(i) => &buf[i],
};
svg.write_event(Event::Text(BytesText::from_plain_str(s)))?;
svg.write_event(Event::End(BytesEnd::borrowed(b"text")))
svg.write_event(Event::Text(BytesText::new(s)))?;
svg.write_event(Event::End(BytesEnd::new("text")))
}

// Imported from the `enquote` crate @ 1.0.3.
Expand Down
4 changes: 3 additions & 1 deletion tests/data/flamegraph/austin/flame.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion tests/data/flamegraph/colors/async-profiler-java.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion tests/data/flamegraph/colors/deterministic.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion tests/data/flamegraph/differential/diff-negated.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion tests/data/flamegraph/differential/diff.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion tests/data/flamegraph/flamechart/flame.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion tests/data/flamegraph/grey-frames/grey-frames.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion tests/data/flamegraph/narrow-blocks/narrow-blocks.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 24ab2e9

Please sign in to comment.