Skip to content

Run doc examples in parallel#3031

Open
maciektr wants to merge 3 commits intomaciektr/runnable-doc-snippetsfrom
maciektr/runnable-doc-examples-parallel
Open

Run doc examples in parallel#3031
maciektr wants to merge 3 commits intomaciektr/runnable-doc-snippetsfrom
maciektr/runnable-doc-examples-parallel

Conversation

@maciektr
Copy link
Copy Markdown
Contributor

@maciektr maciektr commented Mar 5, 2026

No description provided.

@maciektr
Copy link
Copy Markdown
Contributor Author

maciektr commented Mar 5, 2026

This change is part of the following stack:

Change managed by git-spice.

@maciektr maciektr force-pushed the maciektr/runnable-doc-snippets branch from f72aae9 to a7398d5 Compare March 6, 2026 13:44
@maciektr maciektr force-pushed the maciektr/runnable-doc-examples-parallel branch 2 times, most recently from d03914e to ee71d9c Compare March 6, 2026 17:55
@maciektr maciektr marked this pull request as ready for review March 6, 2026 17:55
@maciektr maciektr requested a review from a team as a code owner March 6, 2026 17:55
@maciektr maciektr requested review from Arcticae and integraledelebesgue and removed request for a team March 6, 2026 17:55
Copy link
Copy Markdown
Member

@Arcticae Arcticae left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, with some style suggestions from me

target_dir: TempDir,
/// Build profile passed to `scarb build` / `scarb execute` and used to locate the
/// incremental cache directory (`target/<profile>/`).
profile: String,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does it need it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It copies incremental cache to speed subsequent builds up - we need profile for it's location.

Ok(())
}

fn copy_dir_contents(from: &Path, to: &Path) -> Result<()> {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't this a recursive dir copy? why do we reimplement such stuff?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, we can use fs_extra crate too

let strategy = block.run_strategy();
let total_in_item = *blocks_per_item.get(&block.id.item_full_path).unwrap_or(&1);
let display_name = block.id.display_name(total_in_item);
let execution_blocks: Vec<(usize, &CodeBlock)> = indexed_blocks
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why isn't this typed as IndexedBlock as well?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed

},
Err(e) => {
_ => match run_results.remove(&order) {
Some(Ok(res)) => match res.status {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Three nested matches is a bit much for me

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored

Ok(r) if r.outcome != ExecutionOutcome::CompileError
);
run_results.insert(*order, result);
if build_succeeded {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just inline the condition

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd need to do a clone on result then 😓

// Run non-compile_fail blocks sequentially until the first successful compilation to warm
// the incremental cache. Compile_fail blocks are skipped here and always run in parallel.
for (order, block) in &execution_blocks {
if block.expected_outcome() == ExecutionOutcome::CompileError {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit]: Don't we want to filter out those beforehand? I would do it in a functional manner here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't!
This loop is a trick we do - we compile the first block that compiles without an error, only for it to produce the incremental caches we can copy to all subsequent builds.
So we cannot filter builds that are expected to fail out, as we still need to compile (and assert they actually fail) later only. We skip them here, as they won't produce incremental caches we won't to grab if they fail.

.filter(|(order, _)| !run_results.contains_key(order))
.collect_vec()
.into_par_iter()
.map(|(order, block)| {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be extracted into its' own function

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

pub fn new(metadata: &'a AdditionalMetadata, has_lib_target: bool, ui: Ui) -> Result<Self> {
let target_dir =
tempdir().context("failed to create directory for doc tests target directory")?;
let profile = std::env::var("SCARB_PROFILE").unwrap_or_else(|_| "dev".to_string());
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why bother having it as a parameter here if we're getting this from env anyway

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think having a centralized source for this is fine 🤔 Don't see a problem here

@maciektr maciektr force-pushed the maciektr/runnable-doc-snippets branch from a7398d5 to 4bc11cc Compare March 31, 2026 07:15
@maciektr maciektr force-pushed the maciektr/runnable-doc-examples-parallel branch from ee71d9c to df4546c Compare March 31, 2026 07:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants