Skip to content

refactor: improve error handling and optimize memory allocation #2354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/build/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub fn execute_build_program(
let cmd = if args.docker {
create_docker_command(args, &program_dir, &program_metadata)?
} else {
create_local_command(args, &program_dir, &program_metadata)
create_local_command(args, &program_dir, &program_metadata)?
};

let target_elf_paths = generate_elf_paths(&program_metadata, Some(args))?;
Expand Down
4 changes: 3 additions & 1 deletion crates/build/src/command/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ pub(crate) fn create_docker_command(
}

super::utils::parse_rustc_version(&stdout_string)
.map_err(|e| anyhow::anyhow!("Failed to parse rustc version in docker: {}", e))?
};

std::thread::sleep(std::time::Duration::from_secs(2));
Expand Down Expand Up @@ -161,7 +162,8 @@ pub(crate) fn create_docker_command(
docker_args.extend_from_slice(&get_program_build_args(args));

let mut command = Command::new("docker");
command.current_dir(canonicalized_program_dir.clone()).args(&docker_args);
command
.current_dir(canonicalized_program_dir.clone()).args(&docker_args);
Ok(command)
}

Expand Down
6 changes: 4 additions & 2 deletions crates/build/src/command/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{env, path::PathBuf, process::Command};
use crate::{BuildArgs, WarningLevel, HELPER_TARGET_SUBDIR};
use cargo_metadata::camino::Utf8PathBuf;
use dirs::home_dir;
use anyhow::Result;

use super::utils::{get_program_build_args, get_rust_compiler_flags};

Expand All @@ -11,7 +12,7 @@ pub(crate) fn create_local_command(
args: &BuildArgs,
program_dir: &Utf8PathBuf,
program_metadata: &cargo_metadata::Metadata,
) -> Command {
) -> Result<Command> {
let mut command = Command::new("cargo");
let canonicalized_program_dir =
program_dir.canonicalize().expect("Failed to canonicalize program directory");
Expand Down Expand Up @@ -54,6 +55,7 @@ pub(crate) fn create_local_command(
}

super::utils::parse_rustc_version(&stdout_string)
.map_err(|e| anyhow::anyhow!("Failed to parse rustc version: {}", e))?
};

let rustc_bin = {
Expand Down Expand Up @@ -94,5 +96,5 @@ pub(crate) fn create_local_command(
.map(|v| v.0)
.filter(|v| v.starts_with("CARGO_FEATURE_") || v.starts_with("CARGO_CFG_"))
.fold(&mut command, Command::env_remove);
command
Ok(command)
}
14 changes: 9 additions & 5 deletions crates/build/src/command/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,13 @@ pub(crate) fn execute_command(mut command: Command, docker: bool) -> Result<()>
Ok(())
}

pub(crate) fn parse_rustc_version(version: &str) -> semver::Version {
let version_string =
version.split(" ").nth(1).expect("Can't parse rustc --version stdout").trim();

semver::Version::parse(version_string).expect("Can't parse rustc --version stdout")
pub(crate) fn parse_rustc_version(version: &str) -> Result<semver::Version, anyhow::Error> {
let version_string = version
.split(" ")
.nth(1)
.ok_or_else(|| anyhow::anyhow!("Failed to parse rustc version: missing version component"))?
.trim();

semver::Version::parse(version_string)
.map_err(|e| anyhow::anyhow!("Failed to parse rustc version '{}': {}", version_string, e))
}
14 changes: 12 additions & 2 deletions crates/core/machine/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,22 @@ pub fn next_power_of_two(n: usize, fixed_power: Option<usize>) -> usize {
}

pub fn chunk_vec<T>(mut vec: Vec<T>, chunk_size: usize) -> Vec<Vec<T>> {
let mut result = Vec::new();
if chunk_size == 0 || vec.is_empty() {
return Vec::new();
}

// Pre-allocate the result vector with the correct capacity
let num_chunks = (vec.len() + chunk_size - 1) / chunk_size;
let mut result = Vec::with_capacity(num_chunks);

// Reserve capacity for each chunk to avoid reallocations
while !vec.is_empty() {
let current_chunk_size = std::cmp::min(chunk_size, vec.len());
let current_chunk = vec.drain(..current_chunk_size).collect::<Vec<T>>();
let mut current_chunk = Vec::with_capacity(current_chunk_size);
current_chunk.extend(vec.drain(..current_chunk_size));
result.push(current_chunk);
}

result
}

Expand Down
Loading