Skip to content

Commit ceedc94

Browse files
authored
Add criterion benchmark to forc-doc + initial code quality pass (#7242)
## Description Doing some general code quality changes at the same time. ## Checklist - [ ] I have linked to any relevant issues. - [ ] I have commented my code, particularly in hard-to-understand areas. - [ ] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [ ] If my change requires substantial documentation changes, I have [requested support from the DevRel team](https://github.com/FuelLabs/devrel-requests/issues/new/choose) - [ ] I have added tests that prove my fix is effective or that my feature works. - [ ] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [ ] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [ ] I have requested a review from the relevant team or maintainers.
1 parent 0ac606c commit ceedc94

File tree

42 files changed

+394
-368
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+394
-368
lines changed

.github/workflows/benchmark.yml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ on:
1010
- sway-lsp/**
1111
- forc-pkg/**
1212
- sway-core/**
13-
13+
- forc-plugins/forc-doc/**
1414
# `workflow_dispatch` allows CodSpeed to trigger backtest
1515
# performance analysis in order to generate initial data.
1616
workflow_dispatch:
@@ -38,11 +38,15 @@ jobs:
3838
with:
3939
tool: cargo-codspeed
4040

41-
- name: Build the benchmark target(s)
42-
run: cargo codspeed build
41+
- name: Build the benchmark targets
42+
run: |
43+
cargo codspeed build -p sway-lsp
44+
cargo codspeed build -p forc-doc
4345
4446
- name: Run the benchmarks
4547
uses: CodSpeedHQ/action@v3
4648
with:
47-
run: cargo codspeed run
49+
run: |
50+
cargo codspeed run -p sway-lsp
51+
cargo codspeed run -p forc-doc
4852
token: ${{ secrets.CODSPEED_TOKEN }}

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

forc-plugins/forc-doc/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,13 @@ sway-types.workspace = true
2929
swayfmt.workspace = true
3030

3131
[dev-dependencies]
32+
codspeed-criterion-compat.workspace = true
3233
dir_indexer.workspace = true
3334
expect-test.workspace = true
35+
36+
[[bench]]
37+
name = "bench_main"
38+
harness = false
39+
40+
[lib]
41+
bench = false
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use codspeed_criterion_compat::{criterion_group, criterion_main, Criterion};
2+
use forc_doc::{compile, compile_html, Command, DocContext};
3+
use std::path::Path;
4+
5+
fn benchmarks(c: &mut Criterion) {
6+
let path = Path::new("./../../sway-lib-std");
7+
let opts = Command {
8+
path: Some(path.to_str().unwrap().to_string()),
9+
..Default::default()
10+
};
11+
let ctx = DocContext::from_options(&opts).unwrap();
12+
let compile_results = compile(&ctx, &opts).unwrap().collect::<Vec<_>>();
13+
14+
c.bench_function("build_std_lib_docs", |b| {
15+
b.iter(|| {
16+
let mut results = compile_results.clone();
17+
let _ = compile_html(&opts, &ctx, &mut results);
18+
});
19+
});
20+
}
21+
22+
criterion_group! {
23+
name = benches;
24+
config = Criterion::default().measurement_time(std::time::Duration::from_secs(10));
25+
targets = benchmarks
26+
}
27+
28+
criterion_main!(benches);

forc-plugins/forc-doc/src/cli.rs

Lines changed: 0 additions & 57 deletions
This file was deleted.

forc-plugins/forc-doc/src/doc/descriptor.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use crate::{
77
context::{Context, ContextType, ItemContext},
88
documentable_type::DocumentableType,
99
},
10-
util::format::{code_block::trim_fn_body, docstring::DocStrings},
10+
util::format::docstring::DocStrings,
1111
},
1212
};
1313
use anyhow::Result;
@@ -23,6 +23,7 @@ use swayfmt::parse;
2323
trait RequiredMethods {
2424
fn to_methods(&self, decl_engine: &DeclEngine) -> Vec<TyTraitFn>;
2525
}
26+
2627
impl RequiredMethods for Vec<DeclRefTraitFn> {
2728
fn to_methods(&self, decl_engine: &DeclEngine) -> Vec<TyTraitFn> {
2829
self.iter()
@@ -380,3 +381,11 @@ impl Descriptor {
380381
}
381382
}
382383
}
384+
385+
/// Takes a formatted function signature & body and returns only the signature.
386+
fn trim_fn_body(f: String) -> String {
387+
match f.find('{') {
388+
Some(index) => f.split_at(index).0.to_string(),
389+
None => f,
390+
}
391+
}

forc-plugins/forc-doc/src/doc/mod.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
//! Handles conversion of compiled typed Sway programs into [Document]s that can be rendered into HTML.
2+
mod descriptor;
3+
pub mod module;
4+
25
use crate::{
36
doc::{descriptor::Descriptor, module::ModuleInfo},
47
render::{
@@ -22,11 +25,7 @@ use sway_core::{
2225
Engines,
2326
};
2427
use sway_features::ExperimentalFeatures;
25-
use sway_types::BaseIdent;
26-
use sway_types::Spanned;
27-
28-
mod descriptor;
29-
pub mod module;
28+
use sway_types::{BaseIdent, Spanned};
3029

3130
#[derive(Default, Clone)]
3231
pub struct Documentation(pub Vec<Document>);
@@ -86,7 +85,7 @@ impl Documentation {
8685
if let Ok(Descriptor::Documentable(doc)) =
8786
Descriptor::from_type_info(impl_for_type.as_ref(), engines, module_info.clone())
8887
{
89-
if !docs.iter().any(|existing_doc| *existing_doc == doc) {
88+
if !docs.contains(&doc) {
9089
docs.push(doc);
9190
}
9291
}

forc-plugins/forc-doc/src/doc/module.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! Handles the gathering of module information used in navigation and documentation of modules.
2-
use crate::render::{constant::INDEX_FILENAME, util::format::docstring::create_preview};
2+
use crate::render::{util::format::docstring::create_preview, INDEX_FILENAME};
33
use anyhow::Result;
44
use horrorshow::{box_html, Template};
55
use std::{fmt::Write, path::PathBuf};

0 commit comments

Comments
 (0)