Skip to content
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

Sourcemap performance improvements #668

Merged
merged 3 commits into from
May 6, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ log = "0.4.14"
maplit = "1.0.2"
notify = "4.0.17"
opener = "0.5.0"
rayon = "1.7.0"
reqwest = { version = "0.11.10", features = ["blocking", "json", "native-tls-vendored"] }
ritz = "0.1.0"
roblox_install = "1.0.0"
Expand Down
39 changes: 23 additions & 16 deletions src/cli/sourcemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{
use clap::Parser;
use fs_err::File;
use memofs::Vfs;
use rayon::prelude::*;
use rbx_dom_weak::types::Ref;
use serde::Serialize;
use tokio::runtime::Runtime;
Expand All @@ -23,15 +24,15 @@ const PATH_STRIP_FAILED_ERR: &str = "Failed to create relative paths for project
/// Representation of a node in the generated sourcemap tree.
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct SourcemapNode {
name: String,
class_name: String,
struct SourcemapNode<'a> {
name: &'a str,
class_name: &'a str,

#[serde(skip_serializing_if = "Vec::is_empty")]
file_paths: Vec<PathBuf>,

#[serde(skip_serializing_if = "Vec::is_empty")]
children: Vec<SourcemapNode>,
children: Vec<SourcemapNode<'a>>,
}

/// Generates a sourcemap file from the Rojo project.
Expand Down Expand Up @@ -75,6 +76,13 @@ impl SourcemapCommand {
filter_non_scripts
};

// Pre-build a rayon threadpool with a low number of threads to avoid
// dynamic creation overhead on systems with a high number of cpus.
rayon::ThreadPoolBuilder::new()
.num_threads(6)
.build_global()
.unwrap();

write_sourcemap(&session, self.output.as_deref(), filter)?;

if self.watch {
Expand Down Expand Up @@ -108,20 +116,19 @@ fn filter_non_scripts(instance: &InstanceWithMeta) -> bool {
)
}

fn recurse_create_node(
tree: &RojoTree,
fn recurse_create_node<'a>(
tree: &'a RojoTree,
referent: Ref,
project_dir: &Path,
filter: fn(&InstanceWithMeta) -> bool,
) -> Option<SourcemapNode> {
) -> Option<SourcemapNode<'a>> {
let instance = tree.get_instance(referent).expect("instance did not exist");

let mut children = Vec::new();
for &child_id in instance.children() {
if let Some(child_node) = recurse_create_node(tree, child_id, project_dir, filter) {
children.push(child_node);
}
}
let children: Vec<_> = instance
.children()
.par_iter()
.filter_map(|&child_id| recurse_create_node(tree, child_id, project_dir, filter))
.collect();

// If this object has no children and doesn't pass the filter, it doesn't
// contain any information we're looking for.
Expand All @@ -140,8 +147,8 @@ fn recurse_create_node(
.collect();

Some(SourcemapNode {
name: instance.name().to_string(),
class_name: instance.class_name().to_string(),
name: instance.name(),
class_name: instance.class_name(),
file_paths,
children,
})
Expand All @@ -157,7 +164,7 @@ fn write_sourcemap(
let root_node = recurse_create_node(&tree, tree.get_root_id(), session.root_dir(), filter);

if let Some(output_path) = output {
let mut file = BufWriter::new(File::create(&output_path)?);
let mut file = BufWriter::new(File::create(output_path)?);
serde_json::to_writer(&mut file, &root_node)?;
file.flush()?;

Expand Down