Skip to content

Commit

Permalink
[Turbopack] replace EnvLayer with a faster filter (#73614)
Browse files Browse the repository at this point in the history
### What?

The default `EnvFilter` is very flexible, but also very slow.

This replaces it with a custom one that is a lot faster.
  • Loading branch information
sokra authored Dec 9, 2024
1 parent 9b85292 commit 7ce5b8d
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 8 deletions.
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.

5 changes: 3 additions & 2 deletions crates/napi/src/next_api/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use once_cell::sync::Lazy;
use rand::Rng;
use tokio::{io::AsyncWriteExt, time::Instant};
use tracing::Instrument;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Registry};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, Registry};
use turbo_rcstr::RcStr;
use turbo_tasks::{
get_effects, Completion, Effects, ReadRef, ResolvedVc, TransientInstance, UpdateInfo, Vc,
Expand All @@ -41,6 +41,7 @@ use turbopack_core::{
use turbopack_ecmascript_hmr_protocol::{ClientUpdateInstruction, ResourceIdentifier};
use turbopack_trace_utils::{
exit::{ExitHandler, ExitReceiver},
filter_layer::FilterLayer,
raw_trace::RawTraceLayer,
trace_writer::TraceWriter,
};
Expand Down Expand Up @@ -317,7 +318,7 @@ pub async fn project_new(

let subscriber = Registry::default();

let subscriber = subscriber.with(EnvFilter::builder().parse(trace).unwrap());
let subscriber = subscriber.with(FilterLayer::try_new(&trace).unwrap());
let dist_dir = options.dist_dir.clone();

let internal_dir = PathBuf::from(&options.project_path).join(dist_dir);
Expand Down
9 changes: 5 additions & 4 deletions crates/next-build-test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ use next_core::tracing_presets::{
TRACING_NEXT_OVERVIEW_TARGETS, TRACING_NEXT_TARGETS, TRACING_NEXT_TURBOPACK_TARGETS,
TRACING_NEXT_TURBO_TASKS_TARGETS,
};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Registry};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, Registry};
use turbo_tasks::TurboTasks;
use turbo_tasks_malloc::TurboMalloc;
use turbo_tasks_memory::MemoryBackend;
use turbopack_trace_utils::{exit::ExitGuard, raw_trace::RawTraceLayer, trace_writer::TraceWriter};
use turbopack_trace_utils::{
exit::ExitGuard, filter_layer::FilterLayer, raw_trace::RawTraceLayer, trace_writer::TraceWriter,
};

#[global_allocator]
static ALLOC: TurboMalloc = TurboMalloc;
Expand Down Expand Up @@ -99,8 +101,7 @@ fn main() {

let subscriber = Registry::default();

let subscriber =
subscriber.with(EnvFilter::builder().parse(trace).unwrap());
let subscriber = subscriber.with(FilterLayer::try_new(&trace).unwrap());
let trace_file = "trace.log";
let trace_writer = std::fs::File::create(trace_file).unwrap();
let (trace_writer, guard) = TraceWriter::new(trace_writer);
Expand Down
5 changes: 3 additions & 2 deletions turbopack/crates/turbopack-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ use std::path::Path;

use anyhow::{Context, Result};
use clap::Parser;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Registry};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, Registry};
use turbo_tasks_malloc::TurboMalloc;
use turbopack_cli::{arguments::Arguments, register};
use turbopack_trace_utils::{
exit::ExitHandler,
filter_layer::FilterLayer,
raw_trace::RawTraceLayer,
trace_writer::TraceWriter,
tracing_presets::{
Expand Down Expand Up @@ -55,7 +56,7 @@ async fn main_inner(args: Arguments) -> Result<()> {

let subscriber = Registry::default();

let subscriber = subscriber.with(EnvFilter::builder().parse(trace).unwrap());
let subscriber = subscriber.with(FilterLayer::try_new(&trace).unwrap());

let internal_dir = args
.dir()
Expand Down
1 change: 1 addition & 0 deletions turbopack/crates/turbopack-trace-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ anyhow = { workspace = true }
crossbeam-channel = { workspace = true }
once_cell = { workspace = true }
postcard = { workspace = true, features = ["alloc", "use-std"] }
rustc-hash = { workspace = true }
serde = { workspace = true, features = ["derive"] }
tokio = { workspace = true, features = ["macros", "signal", "sync", "rt"] }
tracing = { workspace = true }
Expand Down
56 changes: 56 additions & 0 deletions turbopack/crates/turbopack-trace-utils/src/filter_layer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use std::{collections::HashMap, hash::BuildHasherDefault, str::FromStr};

use anyhow::Result;
use rustc_hash::FxHasher;
use tracing::{level_filters::LevelFilter, Subscriber};
use tracing_subscriber::Layer;

pub struct FilterLayer {
config: HashMap<String, LevelFilter, BuildHasherDefault<FxHasher>>,
global_level: LevelFilter,
}

impl FilterLayer {
pub fn try_new(input: &str) -> Result<Self> {
let mut config = HashMap::default();
let mut global_level = LevelFilter::OFF;
for entry in input.split(',') {
if entry.is_empty() {
continue;
}
let mut parts = entry.splitn(2, '=');
let target = parts.next().unwrap();
let level = parts.next().unwrap_or("trace");
let level = LevelFilter::from_str(level).unwrap();
if target == "*" {
global_level = level;
} else {
config.insert(target.to_string(), level);
}
}
Ok(Self {
config,
global_level,
})
}
}

impl<S: Subscriber> Layer<S> for FilterLayer {
fn enabled(
&self,
metadata: &tracing::Metadata<'_>,
_ctx: tracing_subscriber::layer::Context<'_, S>,
) -> bool {
if self.config.is_empty() {
return true;
}
let target = metadata.target().split("::").next().unwrap();
let filter = self.config.get(target).unwrap_or(&self.global_level);
let level = metadata.level();
level <= filter
}

fn max_level_hint(&self) -> Option<LevelFilter> {
self.config.values().copied().min()
}
}
1 change: 1 addition & 0 deletions turbopack/crates/turbopack-trace-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#![feature(arbitrary_self_types_pointers)]

pub mod exit;
pub mod filter_layer;
mod flavor;
pub mod raw_trace;
pub mod trace_writer;
Expand Down

0 comments on commit 7ce5b8d

Please sign in to comment.