-
Notifications
You must be signed in to change notification settings - Fork 27.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Turbopack] replace EnvLayer with a faster filter (#73614)
### 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
Showing
7 changed files
with
70 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
turbopack/crates/turbopack-trace-utils/src/filter_layer.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters