From 4fd4fb4d0042102703b091fdb1a35b057fbdd98c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Kijewski?= Date: Wed, 15 May 2024 20:02:40 +0200 Subject: [PATCH] Add simple parser benchmark --- askama_parser/Cargo.toml | 7 + askama_parser/benches/from_str.rs | 44 ++++++ askama_parser/benches/librustdoc/LICENSE.md | 7 + .../benches/librustdoc/item_info.html | 7 + .../benches/librustdoc/item_union.html | 24 ++++ askama_parser/benches/librustdoc/page.html | 128 ++++++++++++++++++ .../benches/librustdoc/print_item.html | 26 ++++ .../benches/librustdoc/short_item_info.html | 23 ++++ askama_parser/benches/librustdoc/sidebar.html | 34 +++++ askama_parser/benches/librustdoc/source.html | 21 +++ .../benches/librustdoc/type_layout.html | 63 +++++++++ .../benches/librustdoc/type_layout_size.html | 12 ++ 12 files changed, 396 insertions(+) create mode 100644 askama_parser/benches/from_str.rs create mode 100644 askama_parser/benches/librustdoc/LICENSE.md create mode 100644 askama_parser/benches/librustdoc/item_info.html create mode 100644 askama_parser/benches/librustdoc/item_union.html create mode 100644 askama_parser/benches/librustdoc/page.html create mode 100644 askama_parser/benches/librustdoc/print_item.html create mode 100644 askama_parser/benches/librustdoc/short_item_info.html create mode 100644 askama_parser/benches/librustdoc/sidebar.html create mode 100644 askama_parser/benches/librustdoc/source.html create mode 100644 askama_parser/benches/librustdoc/type_layout.html create mode 100644 askama_parser/benches/librustdoc/type_layout_size.html diff --git a/askama_parser/Cargo.toml b/askama_parser/Cargo.toml index fc7fc435..0b11abdf 100644 --- a/askama_parser/Cargo.toml +++ b/askama_parser/Cargo.toml @@ -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 diff --git a/askama_parser/benches/from_str.rs b/askama_parser/benches/from_str.rs new file mode 100644 index 00000000..fa1f5877 --- /dev/null +++ b/askama_parser/benches/from_str.rs @@ -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")), +]; diff --git a/askama_parser/benches/librustdoc/LICENSE.md b/askama_parser/benches/librustdoc/LICENSE.md new file mode 100644 index 00000000..6f4d51be --- /dev/null +++ b/askama_parser/benches/librustdoc/LICENSE.md @@ -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]: +[their Git history]: diff --git a/askama_parser/benches/librustdoc/item_info.html b/askama_parser/benches/librustdoc/item_info.html new file mode 100644 index 00000000..9e65ae95 --- /dev/null +++ b/askama_parser/benches/librustdoc/item_info.html @@ -0,0 +1,7 @@ +{% if !items.is_empty() %} + + {% for item in items %} + {{item|safe}} {# #} + {% endfor %} + +{% endif %} diff --git a/askama_parser/benches/librustdoc/item_union.html b/askama_parser/benches/librustdoc/item_union.html new file mode 100644 index 00000000..b1c1d5a6 --- /dev/null +++ b/askama_parser/benches/librustdoc/item_union.html @@ -0,0 +1,24 @@ +

+    {{ self.render_attributes_in_pre()|safe }}
+    {{ self.render_union()|safe }}
+
+{{ self.document()|safe }} +{% if self.fields_iter().peek().is_some() %} +

{# #} + Fields§ {# #} +

+ {% for (field, ty) in self.fields_iter() %} + {% let name = field.name.expect("union field name") %} + {# #} + § {# #} + {{ name }}: {{+ self.print_ty(ty)|safe }} {# #} + + {% if let Some(stability_class) = self.stability_field(field) %} + + {% endif %} + {{ self.document_field(field)|safe }} + {% endfor %} +{% endif %} +{{ self.render_assoc_items()|safe }} +{{ self.document_type_layout()|safe }} diff --git a/askama_parser/benches/librustdoc/page.html b/askama_parser/benches/librustdoc/page.html new file mode 100644 index 00000000..cdf01fa7 --- /dev/null +++ b/askama_parser/benches/librustdoc/page.html @@ -0,0 +1,128 @@ + {# #} + {# #} + {# #} + {# #} + {# #} + {# #} + {# #} + {{page.title}} {# #} + {# #} + {# #} + {# #} + {% if !layout.default_settings.is_empty() %} + {# #} + {% endif %} + {# #} + {# #} + {% if page.css_class.contains("crate") %} + {# #} + {% else if page.css_class == "src" %} + {# #} + {# #} + {% else if !page.css_class.contains("mod") %} + {# #} + {% else if !page.css_class.contains("sys") %} + {# #} + {% endif %} + {# #} + {% if layout.scrape_examples_extension %} + {# #} + {% endif %} + {# #} + {% if layout.css_file_extension.is_some() %} + {# #} + {% endif %} + {% if !layout.favicon.is_empty() %} + {# #} + {% else %} + {# #} + {# #} + {% endif %} + {{ layout.external_html.in_header|safe }} + {# #} + {# #} + {# #} + {{ layout.external_html.before_content|safe }} + {% if page.css_class != "src" %} + + {% endif %} + {# #} + {# #} +
{# #} + {% if page.css_class != "src" %}
{% 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 #} + {# #} +
{{ content|safe }}
{# #} + {% if page.css_class != "src" %}
{% endif %} +
{# #} + {{ layout.external_html.after_content|safe }} + {# #} + {# #} diff --git a/askama_parser/benches/librustdoc/print_item.html b/askama_parser/benches/librustdoc/print_item.html new file mode 100644 index 00000000..76e77045 --- /dev/null +++ b/askama_parser/benches/librustdoc/print_item.html @@ -0,0 +1,26 @@ +
{# #} +

+ {{typ}} + {# The breadcrumbs of the item path, like std::string #} + {% for component in path_components %} + {{component.name}}:: + {% endfor %} + {{name}} {# #} + {# #} +

{# #} + + {% if !stability_since_raw.is_empty() %} + {{ stability_since_raw|safe +}} · {#+ #} + {% endif %} + {% match src_href %} + {% when Some with (href) %} + source · {#+ #} + {% else %} + {% endmatch %} + {# #} + {# #} +
{# #} diff --git a/askama_parser/benches/librustdoc/short_item_info.html b/askama_parser/benches/librustdoc/short_item_info.html new file mode 100644 index 00000000..75d155e9 --- /dev/null +++ b/askama_parser/benches/librustdoc/short_item_info.html @@ -0,0 +1,23 @@ +{% match self %} + {% when Self::Deprecation with { message } %} +
{# #} + 👎 {# #} + {{message|safe}} {# #} +
{# #} + {% when Self::Unstable with { feature, tracking } %} +
{# #} + 🔬 {# #} + {# #} + This is a nightly-only experimental API. ({# #} + {{feature}} {# #} + {% match tracking %} + {% when Some with ((url, num)) %} +  #{{num}} {# #} + {% when None %} + {% endmatch %} + ) {# #} + {# #} +
{# #} + {% when Self::Portability with { message } %} +
{{message|safe}}
{# #} +{% endmatch %} diff --git a/askama_parser/benches/librustdoc/sidebar.html b/askama_parser/benches/librustdoc/sidebar.html new file mode 100644 index 00000000..3251b4c1 --- /dev/null +++ b/askama_parser/benches/librustdoc/sidebar.html @@ -0,0 +1,34 @@ +{% if !title.is_empty() %} +

{# #} + {{title_prefix}}{{title}} {# #} +

+{% endif %} + diff --git a/askama_parser/benches/librustdoc/source.html b/askama_parser/benches/librustdoc/source.html new file mode 100644 index 00000000..42d01277 --- /dev/null +++ b/askama_parser/benches/librustdoc/source.html @@ -0,0 +1,21 @@ +
{# #} + {# 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. #} +
+        {% for line in lines.clone() %}
+            {% if embedded %}
+                {{line|safe}}
+            {%~ else %}
+                {{line|safe}}
+            {%~ endif %}
+        {% endfor %}
+    
{# #} +
 {# #}
+        
+            {% if needs_expansion %}
+                
+            {% endif %}
+            {{code_html|safe}}
+         {# #}
+    
{# #} +
diff --git a/askama_parser/benches/librustdoc/type_layout.html b/askama_parser/benches/librustdoc/type_layout.html new file mode 100644 index 00000000..e0516bb4 --- /dev/null +++ b/askama_parser/benches/librustdoc/type_layout.html @@ -0,0 +1,63 @@ +

{# #} + Layout§ {# #} +

{# #} +
{# #} + {% match type_layout_size %} + {% when Ok(type_layout_size) %} +
{# #} +

{# #} + Note: Most layout information is completely {#+ #} + unstable and may even differ between compilations. {#+ #} + The only exception is types with certain repr(...) {#+ #} + attributes. Please see the Rust Reference's {#+ #} + “Type Layout” {#+ #} + chapter for details on type layout guarantees. {# #} +

{# #} +
{# #} +

Size: {{+ type_layout_size|safe }}

{# #} + {% if !variants.is_empty() %} +

{# #} + Size for each variant: {# #} +

{# #} +
    {# #} + {% for (name, layout_size) in variants %} +
  • {# #} + {{ name }}: {#+ #} + {{ layout_size|safe }} +
  • {# #} + {% endfor %} +
{# #} + {% 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`. #} + {% when Err(LayoutError::Unknown(_)) %} +

{# #} + Note: Unable to compute type layout, {#+ #} + possibly due to this type having generic parameters. {#+ #} + Layout can only be computed for concrete, fully-instantiated types. {# #} +

{# #} + {# 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(_)) %} +

{# #} + Note: Encountered an error during type layout; {#+ #} + the type was too big. {# #} +

{# #} + {% when Err(LayoutError::ReferencesError(_)) %} +

{# #} + Note: Encountered an error during type layout; {#+ #} + the type references errors. {# #} +

{# #} + {% when Err(LayoutError::NormalizationFailure(_, _)) %} +

{# #} + Note: Encountered an error during type layout; {#+ #} + the type failed to be normalized. {# #} +

{# #} + {% when Err(LayoutError::Cycle(_)) %} +

{# #} + Note: Encountered an error during type layout; {#+ #} + the type's layout depended on the type's layout itself. {# #} +

{# #} + {% endmatch %} +
{# #} diff --git a/askama_parser/benches/librustdoc/type_layout_size.html b/askama_parser/benches/librustdoc/type_layout_size.html new file mode 100644 index 00000000..9c2b39ed --- /dev/null +++ b/askama_parser/benches/librustdoc/type_layout_size.html @@ -0,0 +1,12 @@ +{% if is_unsized %} + (unsized) +{% else %} + {% if size == 1 %} + 1 byte + {% else %} + {{ size +}} bytes + {% endif %} + {% if is_uninhabited %} + {# +#} (uninhabited) + {% endif %} +{% endif %}