Commit 07836384 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig

histogram

parent b8f40b4b
......@@ -250,6 +250,86 @@ impl Effect {
Effect::Unreachable { .. } => {}
}
}
/// Normalizes all contained values.
pub fn values(&self) -> Vec<&JsValue> {
match self {
Effect::Call { func, args, .. } => {
let mut result = vec![&**func];
result.extend(args.iter().flat_map(|arg| {
match arg {
EffectArg::Value(val) => vec![val],
EffectArg::Closure(func, block) => std::iter::once(func)
.chain(block.effects.iter().flat_map(|e| e.values()))
.collect(),
EffectArg::Spread => vec![],
}
}));
result
}
Effect::MemberCall {
obj, prop, args, ..
} => {
let mut result = vec![&**obj, &**prop];
result.extend(args.iter().flat_map(|arg| {
match arg {
EffectArg::Value(val) => vec![val],
EffectArg::Closure(func, block) => std::iter::once(func)
.chain(block.effects.iter().flat_map(|e| e.values()))
.collect(),
EffectArg::Spread => vec![],
}
}));
result
}
Effect::Conditional {
condition, kind, ..
} => {
let mut result = vec![&**condition];
match &**kind {
ConditionalKind::If { then: block }
| ConditionalKind::Else { r#else: block }
| ConditionalKind::And { expr: block, .. }
| ConditionalKind::Or { expr: block, .. }
| ConditionalKind::NullishCoalescing { expr: block, .. }
| ConditionalKind::Labeled { body: block } => {
result.extend(block.effects.iter().flat_map(|e| e.values()));
}
ConditionalKind::IfElse { then, r#else, .. }
| ConditionalKind::Ternary { then, r#else, .. } => {
result.extend(
then.effects
.iter()
.chain(r#else.effects.iter())
.flat_map(|e| e.values()),
);
}
ConditionalKind::IfElseMultiple { then, r#else, .. } => {
result.extend(
then.iter()
.flat_map(|b| b.effects.iter().flat_map(|e| e.values()))
.chain(
r#else
.iter()
.flat_map(|b| b.effects.iter().flat_map(|e| e.values())),
),
);
}
};
result
}
Effect::Member { obj, prop, .. } => {
vec![obj, prop]
}
Effect::TypeOf { arg, .. } => {
vec![arg]
}
Effect::FreeVar { .. }
| Effect::ImportedBinding { .. }
| Effect::ImportMeta { .. }
| Effect::Unreachable { .. } => vec![],
}
}
}
#[derive(Debug)]
......
......@@ -951,6 +951,42 @@ pub async fn analyse_ecmascript_module_internal(
.instrument(span)
.await?;
static HISTOGRAM: once_cell::sync::Lazy<Option<String>> =
once_cell::sync::Lazy::new(|| std::env::var("JSVALUE_HISTOGRAM").ok());
if let Some(p) = HISTOGRAM.as_ref()
&& path.path.ends_with(p)
{
let mut histogram = vec![];
let mut add = |v: u32| {
let scaled = (v / 10) as usize;
if histogram.len() < scaled + 1 {
histogram.resize(scaled + 1, 0);
}
histogram[scaled] += 1usize;
};
for (name, v) in &var_graph.values {
let size = v.total_nodes();
add(size);
if size > 100 {
println!("big jsvalue {name:?} {size} {v}")
}
}
for effect in &var_graph.effects {
for v in effect.values() {
let size = v.total_nodes();
add(size);
if size > 100 {
println!("big jsvalue in effect {size} {v}")
}
}
}
let scale = 150f32 / (*histogram.iter().max().unwrap() as f32);
for (i, count) in histogram.into_iter().enumerate() {
let bar = "*".repeat((count as f32 * scale) as usize);
println!("{i:3}: {bar} ({count})");
}
}
let span = tracing::info_span!("effects processing", count = var_graph.effects.len());
async {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment