Add simple parser benchmark

This commit is contained in:
René Kijewski 2024-05-15 20:02:40 +02:00 committed by René Kijewski
parent 2b4f264d5a
commit 4fd4fb4d00
12 changed files with 396 additions and 0 deletions

View File

@ -15,3 +15,10 @@ rust-version = "1.65"
[dependencies]
nom = { version = "7", default-features = false, features = ["alloc"] }
[dev-dependencies]
criterion = "0.5"
[[bench]]
name = "from_str"
harness = false

View File

@ -0,0 +1,44 @@
use askama_parser::{Ast, Syntax};
use criterion::{black_box, criterion_group, criterion_main, Criterion, Throughput};
criterion_main!(benches);
criterion_group!(benches, librustdoc);
fn librustdoc(c: &mut Criterion) {
let mut group = c.benchmark_group("librustdoc");
let mut add_benchmark = |name: &str, src: &str| {
group.throughput(Throughput::Bytes(src.len() as u64));
group.bench_function(name, |b| {
let syntax = &Syntax::default();
b.iter(|| Ast::from_str(black_box(src), None, black_box(syntax)).unwrap());
});
};
let all: String = LIBRUSTDOC.iter().map(|&(_, src)| src).collect();
add_benchmark("all", &all);
for (name, src) in LIBRUSTDOC {
add_benchmark(name, src);
}
group.finish();
}
const LIBRUSTDOC: &[(&str, &str)] = &[
("item_info", include_str!("./librustdoc/item_info.html")),
("item_union", include_str!("./librustdoc/item_union.html")),
("page", include_str!("./librustdoc/page.html")),
("print_item", include_str!("./librustdoc/print_item.html")),
(
"short_item_info",
include_str!("./librustdoc/short_item_info.html"),
),
("sidebar", include_str!("./librustdoc/sidebar.html")),
("source", include_str!("./librustdoc/source.html")),
(
"type_layout_size",
include_str!("./librustdoc/type_layout_size.html"),
),
("type_layout", include_str!("./librustdoc/type_layout.html")),
];

View File

@ -0,0 +1,7 @@
The files in this folder were copied verbatim from [librustdoc/html/templates] in version
eeb59f16a5f40e14dc29b95155b7f2569329e3ec. Dual licensed under MIT OR Apache-2.0.
Please find the authors in [their Git history].
[librustdoc/html/templates]: <https://github.com/rust-lang/rust/tree/master/src/librustdoc/html/templates>
[their Git history]: <https://github.com/rust-lang/rust/commits/master/src/librustdoc/html/templates>

View File

@ -0,0 +1,7 @@
{% if !items.is_empty() %}
<span class="item-info">
{% for item in items %}
{{item|safe}} {# #}
{% endfor %}
</span>
{% endif %}

View File

@ -0,0 +1,24 @@
<pre class="rust item-decl"><code>
{{ self.render_attributes_in_pre()|safe }}
{{ self.render_union()|safe }}
</code></pre>
{{ self.document()|safe }}
{% if self.fields_iter().peek().is_some() %}
<h2 id="fields" class="fields section-header"> {# #}
Fields<a href="#fields" class="anchor">§</a> {# #}
</h2>
{% for (field, ty) in self.fields_iter() %}
{% let name = field.name.expect("union field name") %}
<span id="structfield.{{ name }}" {#+ #}
class="{{ ItemType::StructField +}} section-header"> {# #}
<a href="#structfield.{{ name }}" class="anchor field">§</a> {# #}
<code>{{ name }}: {{+ self.print_ty(ty)|safe }}</code> {# #}
</span>
{% if let Some(stability_class) = self.stability_field(field) %}
<span class="stab {{ stability_class }}"></span>
{% endif %}
{{ self.document_field(field)|safe }}
{% endfor %}
{% endif %}
{{ self.render_assoc_items()|safe }}
{{ self.document_type_layout()|safe }}

View File

@ -0,0 +1,128 @@
<!DOCTYPE html> {# #}
<html lang="en"> {# #}
<head> {# #}
<meta charset="utf-8"> {# #}
<meta name="viewport" content="width=device-width, initial-scale=1.0"> {# #}
<meta name="generator" content="rustdoc"> {# #}
<meta name="description" content="{{page.description}}"> {# #}
<title>{{page.title}}</title> {# #}
<script>if(window.location.protocol!=="file:") {# Hack to skip preloading fonts locally - see #98769 #}
document.head.insertAdjacentHTML("beforeend","{{files.source_serif_4_regular}},{{files.fira_sans_regular}},{{files.fira_sans_medium}},{{files.source_code_pro_regular}},{{files.source_code_pro_semibold}}".split(",").map(f=>`<link rel="preload" as="font" type="font/woff2" crossorigin href="{{static_root_path|safe}}${f}">`).join("")) {# #}
</script> {# #}
<link rel="stylesheet" {#+ #}
href="{{static_root_path|safe}}{{files.normalize_css}}"> {# #}
<link rel="stylesheet" {#+ #}
href="{{static_root_path|safe}}{{files.rustdoc_css}}"> {# #}
{% if !layout.default_settings.is_empty() %}
<script id="default-settings" {#+ #}
{%~ for (k, v) in layout.default_settings ~%}
data-{{k}}="{{v}}"
{% endfor %}
></script> {# #}
{% endif %}
<meta name="rustdoc-vars" {#+ #}
data-root-path="{{page.root_path|safe}}" {#+ #}
data-static-root-path="{{static_root_path|safe}}" {#+ #}
data-current-crate="{{layout.krate}}" {#+ #}
data-themes="{{themes|join(",") }}" {#+ #}
data-resource-suffix="{{page.resource_suffix}}" {#+ #}
data-rustdoc-version="{{rustdoc_version}}" {#+ #}
data-channel="{{rust_channel}}" {#+ #}
data-search-js="{{files.search_js}}" {#+ #}
data-settings-js="{{files.settings_js}}" {#+ #}
> {# #}
<script src="{{static_root_path|safe}}{{files.storage_js}}"></script> {# #}
{% if page.css_class.contains("crate") %}
<script defer src="{{page.root_path|safe}}crates{{page.resource_suffix}}.js"></script> {# #}
{% else if page.css_class == "src" %}
<script defer src="{{static_root_path|safe}}{{files.src_script_js}}"></script> {# #}
<script defer src="{{page.root_path|safe}}src-files{{page.resource_suffix}}.js"></script> {# #}
{% else if !page.css_class.contains("mod") %}
<script defer src="sidebar-items{{page.resource_suffix}}.js"></script> {# #}
{% else if !page.css_class.contains("sys") %}
<script defer src="../sidebar-items{{page.resource_suffix}}.js"></script> {# #}
{% endif %}
<script defer src="{{static_root_path|safe}}{{files.main_js}}"></script> {# #}
{% if layout.scrape_examples_extension %}
<script defer src="{{static_root_path|safe}}{{files.scrape_examples_js}}"></script> {# #}
{% endif %}
<noscript> {# #}
<link rel="stylesheet" {#+ #}
href="{{static_root_path|safe}}{{files.noscript_css}}"> {# #}
</noscript> {# #}
{% if layout.css_file_extension.is_some() %}
<link rel="stylesheet" {#+ #}
href="{{page.root_path|safe}}theme{{page.resource_suffix}}.css"> {# #}
{% endif %}
{% if !layout.favicon.is_empty() %}
<link rel="icon" href="{{layout.favicon}}"> {# #}
{% else %}
<link rel="alternate icon" type="image/png" {#+ #}
href="{{static_root_path|safe}}{{files.rust_favicon_png_32}}"> {# #}
<link rel="icon" type="image/svg+xml" {#+ #}
href="{{static_root_path|safe}}{{files.rust_favicon_svg}}"> {# #}
{% endif %}
{{ layout.external_html.in_header|safe }}
</head> {# #}
<body class="rustdoc {{+page.css_class}}"> {# #}
<!--[if lte IE 11]> {# #}
<div class="warning"> {# #}
This old browser is unsupported and will most likely display funky things. {# #}
</div> {# #}
<![endif]--> {# #}
{{ layout.external_html.before_content|safe }}
{% if page.css_class != "src" %}
<nav class="mobile-topbar"> {# #}
<button class="sidebar-menu-toggle" title="show sidebar"></button> {# #}
{% if !layout.logo.is_empty() || page.rust_logo %}
<a class="logo-container" href="{{page.root_path|safe}}{{display_krate_with_trailing_slash|safe}}index.html"> {# #}
{% if page.rust_logo %}
<img class="rust-logo" src="{{static_root_path|safe}}{{files.rust_logo_svg}}" alt=""> {# #}
{% else if !layout.logo.is_empty() %}
<img src="{{layout.logo}}" alt=""> {# #}
{% endif %}
</a> {# #}
{% endif %}
</nav>
{% endif %}
<nav class="sidebar"> {# #}
{% if page.css_class != "src" %}
<div class="sidebar-crate">
{% if !layout.logo.is_empty() || page.rust_logo %}
<a class="logo-container" href="{{page.root_path|safe}}{{display_krate_with_trailing_slash|safe}}index.html"> {# #}
{% if page.rust_logo %}
<img class="rust-logo" src="{{static_root_path|safe}}{{files.rust_logo_svg}}" alt="logo"> {# #}
{% else if !layout.logo.is_empty() %}
<img src="{{layout.logo}}" alt="logo"> {# #}
{% endif %}
</a> {# #}
{% endif %}
<h2> {# #}
<a href="{{page.root_path|safe}}{{display_krate_with_trailing_slash|safe}}index.html">{{display_krate}}</a> {# #}
{% if !display_krate_version_number.is_empty() %}
<span class="version">{{+ display_krate_version_number}}</span>
{% endif %}
</h2> {# #}
</div> {# #}
{% if !display_krate_version_extra.is_empty() %}
<div class="version">{{+ display_krate_version_extra}}</div> {# #}
{% endif %}
{% else %}
<div class="src-sidebar-title"> {# #}
<h2>Files</h2> {# #}
</div> {# #}
{% endif %}
{{ sidebar|safe }}
</nav> {# #}
<div class="sidebar-resizer"></div> {# #}
<main> {# #}
{% if page.css_class != "src" %}<div class="width-limiter">{% endif %}
{# defined in storage.js to avoid duplicating complex UI across every page #}
{# and because the search form only works if JS is enabled anyway #}
<rustdoc-search></rustdoc-search> {# #}
<section id="main-content" class="content">{{ content|safe }}</section> {# #}
{% if page.css_class != "src" %}</div>{% endif %}
</main> {# #}
{{ layout.external_html.after_content|safe }}
</body> {# #}
</html> {# #}

View File

@ -0,0 +1,26 @@
<div class="main-heading"> {# #}
<h1>
{{typ}}
{# The breadcrumbs of the item path, like std::string #}
{% for component in path_components %}
<a href="{{component.path|safe}}index.html">{{component.name}}</a>::<wbr>
{% endfor %}
<a class="{{item_type}}" href="#">{{name}}</a> {# #}
<button id="copy-path" title="Copy item path to clipboard"> {# #}
Copy item path {# #}
</button> {# #}
</h1> {# #}
<span class="out-of-band">
{% if !stability_since_raw.is_empty() %}
{{ stability_since_raw|safe +}} · {#+ #}
{% endif %}
{% match src_href %}
{% when Some with (href) %}
<a class="src" href="{{href|safe}}">source</a> · {#+ #}
{% else %}
{% endmatch %}
<button id="toggle-all-docs" title="collapse all docs"> {# #}
[<span>&#x2212;</span>] {# #}
</button> {# #}
</span> {# #}
</div> {# #}

View File

@ -0,0 +1,23 @@
{% match self %}
{% when Self::Deprecation with { message } %}
<div class="stab deprecated"> {# #}
<span class="emoji">👎</span> {# #}
<span>{{message|safe}}</span> {# #}
</div> {# #}
{% when Self::Unstable with { feature, tracking } %}
<div class="stab unstable"> {# #}
<span class="emoji">🔬</span> {# #}
<span> {# #}
This is a nightly-only experimental API. ({# #}
<code>{{feature}}</code> {# #}
{% match tracking %}
{% when Some with ((url, num)) %}
&nbsp;<a href="{{url}}{{num}}">#{{num}}</a> {# #}
{% when None %}
{% endmatch %}
) {# #}
</span> {# #}
</div> {# #}
{% when Self::Portability with { message } %}
<div class="stab portability">{{message|safe}}</div> {# #}
{% endmatch %}

View File

@ -0,0 +1,34 @@
{% if !title.is_empty() %}
<h2 class="location"> {# #}
<a href="#">{{title_prefix}}{{title}}</a> {# #}
</h2>
{% endif %}
<div class="sidebar-elems">
{% if is_crate %}
<ul class="block"> {# #}
<li><a id="all-types" href="all.html">All Items</a></li> {# #}
</ul>
{% endif %}
{% if self.should_render_blocks() %}
<section>
{% for block in blocks %}
{% if block.should_render() %}
{% if !block.heading.name.is_empty() %}
<h3><a href="#{{block.heading.href|safe}}">{{block.heading.name}}</a></h3>
{% endif %}
{% if !block.links.is_empty() %}
<ul class="block{% if !block.class.is_empty() +%} {{+block.class}}{% endif %}">
{% for link in block.links %}
<li><a href="#{{link.href|safe}}">{{link.name}}</a></li>
{% endfor %}
</ul>
{% endif %}
{% endif %}
{% endfor %}
</section>
{% endif %}
{% if !path.is_empty() %}
<h2><a href="{% if is_mod %}../{% endif %}index.html">In {{+ path}}</a></h2>
{% endif %}
</div>

View File

@ -0,0 +1,21 @@
<div class="example-wrap"> {# #}
{# https://developers.google.com/search/docs/crawling-indexing/robots-meta-tag#data-nosnippet-attr
Do not show "1 2 3 4 5 ..." in web search results. #}
<div data-nosnippet><pre class="src-line-numbers">
{% for line in lines.clone() %}
{% if embedded %}
<span>{{line|safe}}</span>
{%~ else %}
<a href="#{{line|safe}}" id="{{line|safe}}">{{line|safe}}</a>
{%~ endif %}
{% endfor %}
</pre></div> {# #}
<pre class="rust"> {# #}
<code>
{% if needs_expansion %}
<button class="expand">&varr;</button>
{% endif %}
{{code_html|safe}}
</code> {# #}
</pre> {# #}
</div>

View File

@ -0,0 +1,63 @@
<h2 id="layout" class="section-header"> {# #}
Layout<a href="#layout" class="anchor">§</a> {# #}
</h2> {# #}
<div class="docblock"> {# #}
{% match type_layout_size %}
{% when Ok(type_layout_size) %}
<div class="warning"> {# #}
<p> {# #}
<strong>Note:</strong> Most layout information is <strong>completely {#+ #}
unstable</strong> and may even differ between compilations. {#+ #}
The only exception is types with certain <code>repr(...)</code> {#+ #}
attributes. Please see the Rust Reference's {#+ #}
<a href="https://doc.rust-lang.org/reference/type-layout.html">“Type Layout”</a> {#+ #}
chapter for details on type layout guarantees. {# #}
</p> {# #}
</div> {# #}
<p><strong>Size:</strong> {{+ type_layout_size|safe }}</p> {# #}
{% if !variants.is_empty() %}
<p> {# #}
<strong>Size for each variant:</strong> {# #}
</p> {# #}
<ul> {# #}
{% for (name, layout_size) in variants %}
<li> {# #}
<code>{{ name }}</code>: {#+ #}
{{ layout_size|safe }}
</li> {# #}
{% endfor %}
</ul> {# #}
{% endif %}
{# This kind of layout error can occur with valid code, e.g. if you try to
get the layout of a generic type such as `Vec<T>`. #}
{% when Err(LayoutError::Unknown(_)) %}
<p> {# #}
<strong>Note:</strong> Unable to compute type layout, {#+ #}
possibly due to this type having generic parameters. {#+ #}
Layout can only be computed for concrete, fully-instantiated types. {# #}
</p> {# #}
{# This kind of error probably can't happen with valid code, but we don't
want to panic and prevent the docs from building, so we just let the
user know that we couldn't compute the layout. #}
{% when Err(LayoutError::SizeOverflow(_)) %}
<p> {# #}
<strong>Note:</strong> Encountered an error during type layout; {#+ #}
the type was too big. {# #}
</p> {# #}
{% when Err(LayoutError::ReferencesError(_)) %}
<p> {# #}
<strong>Note:</strong> Encountered an error during type layout; {#+ #}
the type references errors. {# #}
</p> {# #}
{% when Err(LayoutError::NormalizationFailure(_, _)) %}
<p> {# #}
<strong>Note:</strong> Encountered an error during type layout; {#+ #}
the type failed to be normalized. {# #}
</p> {# #}
{% when Err(LayoutError::Cycle(_)) %}
<p> {# #}
<strong>Note:</strong> Encountered an error during type layout; {#+ #}
the type's layout depended on the type's layout itself. {# #}
</p> {# #}
{% endmatch %}
</div> {# #}

View File

@ -0,0 +1,12 @@
{% if is_unsized %}
(unsized)
{% else %}
{% if size == 1 %}
1 byte
{% else %}
{{ size +}} bytes
{% endif %}
{% if is_uninhabited %}
{# +#} (<a href="https://doc.rust-lang.org/stable/reference/glossary.html#uninhabited">uninhabited</a>)
{% endif %}
{% endif %}