From 26dbc257e76010c89fbbd675108945f5b7ba29d7 Mon Sep 17 00:00:00 2001 From: Jonathan Cammisuli Date: Tue, 20 Jun 2023 11:29:36 -0400 Subject: [PATCH] fix(core): use par_sort --- packages/nx/src/native/hasher.rs | 21 +++++-------------- .../workspace/get_nx_workspace_files.rs | 2 +- 2 files changed, 6 insertions(+), 17 deletions(-) diff --git a/packages/nx/src/native/hasher.rs b/packages/nx/src/native/hasher.rs index 833c657fab825e..0b3fd9e3080b87 100644 --- a/packages/nx/src/native/hasher.rs +++ b/packages/nx/src/native/hasher.rs @@ -1,31 +1,20 @@ -#![allow(unused)] - use crate::native::types::FileData; use crate::native::utils::glob::build_glob_set; use crate::native::utils::path::Normalize; use crate::native::walker::nx_walker; -use anyhow::anyhow; -use crossbeam_channel::unbounded; -use globset::{Glob, GlobSetBuilder}; -use ignore::WalkBuilder; -use itertools::Itertools; -use std::cmp::Ordering; +use rayon::prelude::*; use std::collections::HashMap; -use std::path::Path; -use std::thread::available_parallelism; use xxhash_rust::xxh3; -type FileHashes = HashMap; - #[napi] -fn hash_array(input: Vec) -> String { +pub fn hash_array(input: Vec) -> String { let joined = input.join(","); let content = joined.as_bytes(); xxh3::xxh3_64(content).to_string() } #[napi] -fn hash_file(file: String) -> Option { +pub fn hash_file(file: String) -> Option { let Ok(content) = std::fs::read(&file) else { return None; }; @@ -36,7 +25,7 @@ fn hash_file(file: String) -> Option { } #[napi] -fn hash_files(workspace_root: String) -> HashMap { +pub fn hash_files(workspace_root: String) -> HashMap { nx_walker(workspace_root, |rec| { let mut collection: HashMap = HashMap::new(); for (path, content) in rec { @@ -74,7 +63,7 @@ fn hash_files_matching_globs( } // Sort the file data so that its in deterministically ordered by file path - hashes.sort(); + hashes.par_sort(); let sorted_file_hashes: Vec = hashes.into_iter().map(|file_data| file_data.hash).collect(); diff --git a/packages/nx/src/native/workspace/get_nx_workspace_files.rs b/packages/nx/src/native/workspace/get_nx_workspace_files.rs index 1a2d0ea292370d..bb9f018df86c76 100644 --- a/packages/nx/src/native/workspace/get_nx_workspace_files.rs +++ b/packages/nx/src/native/workspace/get_nx_workspace_files.rs @@ -40,7 +40,7 @@ pub fn get_workspace_files_native( trace!(?root_map); // Files need to be sorted each time because when we do hashArray in the TaskHasher.js, the order of the files should be deterministic - file_data.sort(); + file_data.par_sort(); let file_locations = file_data .into_par_iter()