Skip to content

refactor(minifier): make Ctx take &mut TraverseCtx #11771

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

Merged

Conversation

Boshen
Copy link
Member

@Boshen Boshen commented Jun 17, 2025

No description provided.

@github-actions github-actions bot added A-minifier Area - Minifier C-cleanup Category - technical debt or refactoring. Solution not expected to change behavior labels Jun 17, 2025
Copy link
Member Author

Boshen commented Jun 17, 2025


How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent hot fixes, skip the queue and merge this PR next

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@github-actions github-actions bot added the A-transformer Area - Transformer / Transpiler label Jun 17, 2025
Copy link

codspeed-hq bot commented Jun 17, 2025

CodSpeed Instrumentation Performance Report

Merging #11771 will degrade performances by 13.01%

Comparing 06-17-refactor_minifier_make_ctx_take_mut_traversectx_ (d5a8f18) with 06-17-feat_traverse_introduce_traversectx_a_state_ (8ef1be2)

Summary

❌ 1 regressions
✅ 37 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark BASE HEAD Change
mangler[cal.com.tsx] 3.1 ms 3.5 ms -13.01%

@Boshen Boshen force-pushed the 06-17-feat_traverse_introduce_traversectx_a_state_ branch from da742a9 to ad85cc4 Compare June 17, 2025 09:58
@Boshen Boshen force-pushed the 06-17-refactor_minifier_make_ctx_take_mut_traversectx_ branch from 87a3ed1 to ce25a71 Compare June 17, 2025 09:59
@overlookmotel
Copy link
Member

overlookmotel commented Jun 17, 2025

Ctx is a wrapper around &mut TraverseCtx, so it's unfortunate to have to pass around &Ctx and &mut Ctx - these are double-references & &mut TraverseCtx / &mut &mut TraverseCtx.

A couple of possible alternatives:

Remove Ctx

Would it be viable to remove the Ctx wrapper and implement Ctx's methods as an extension trait instead? Then you pass around &mut TraverseCtx<MinifierState>, instead of passing Ctx / &Ctx / &mut Ctx.

We could implement IsGlobalReference on TraverseCtx in oxc_traverse crate.

impl<'_, State> IsGlobalReference for TraverseCtx<State> {
    fn is_global_reference(&self, ident: &IdentifierReference<'_>) -> Option<bool> {
        Some(ident.is_global_reference(self.scoping()))
    }
}

MayHaveSideEffectsContext and ConstantEvaluationCtx are more tricky, but I think could make it work one of 2 ways:

1. In minifier

struct MinifierEvalContext<'a, 's> {
    scoping: &'s Scoping,
    ast: AstBuilder<'a>,
}

impl IsGlobalReference for MinifierEvalContext<'_, '_> {
    fn is_global_reference(&self, ident: &IdentifierReference<'_>) -> Option<bool> {
        Some(ident.is_global_reference(self.scoping))
    }
}

impl MayHaveSideEffectsContext for MinifierEvalContext<'_> {
    fn respect_annotations(&self) -> bool {
        true
    }
    // ... etc ...
}

impl<'a> ConstantEvaluationCtx<'a> for MinifierEvalContext<'a, '_> {
    fn ast(&self) -> AstBuilder<'a> {
        self.ast
    }
}

impl<'a> ExtensionTrait for TraverseCtx<'a, MinifierState> {
    fn eval_ctx(&self) -> MinifierEvalContext<'a, '_> {
        MinifierEvalContext { scoping: self.scoping(), ast: self.ast() }
    }
}

Then when you need to pass an &impl MayHaveSideEffectsContext or &impl ConstantEvaluationCtx to a method, you pass it &ctx.eval_ctx().

2. In oxc_traverse

// oxc_traverse
pub trait StateEvalContext {
    fn respect_annotations(&self) -> bool;
    // ... etc ...
}

impl<'a, State> MayHaveSideEffectsContext for TraverseCtx<'a, State>
    where State: StateEvalContext
{
    fn respect_annotations(&self) -> bool {
        self.state.respect_annotations()
    }
    // ... etc ...
}

impl<'a, State> ConstantEvaluationCtx<'a> for TraverseCtx<'a, State>
    where State: StateEvalContext
{
    fn ast(&self) -> AstBuilder<'a> {
        self.ast
    }
    // ... etc ...
}
// oxc_minifier
impl StateEvalContext for MinifierState {
    fn respect_annotations(&self) -> bool {
        true
    }
    // ... etc ...
}

Now TraverseCtx<MinifierState> implements MayHaveSideEffectsContext and ConstantEvaluationCtx. Ctx wrapper is no longer necessary.

Personally I think this is best solution.

borrow_mut method

Alternatively, could add a borrow_mut method to Ctx which doesn't consume the Ctx and generates a new one:

impl<'a, 'b> Ctx<'a, 'b> {
    #[inline(always)] // Because it's a no-op at runtime
    pub fn borrow_mut<'borrow>(&'borrow mut self) -> Ctx<'a, 'borrow> {
        Ctx(self.0)
    }
}

That'd solve the double-reference problem. Ctx stays, and all Ctx's methods take self, same as now. Unfortunately, it'd introduce the need for .borrow_mut() calls in a lot of places.

@Boshen Boshen force-pushed the 06-17-feat_traverse_introduce_traversectx_a_state_ branch from ad85cc4 to e74c20c Compare June 18, 2025 05:58
@Boshen Boshen force-pushed the 06-17-refactor_minifier_make_ctx_take_mut_traversectx_ branch from ce25a71 to a48d137 Compare June 18, 2025 05:58
@graphite-app graphite-app bot added the 0-merge Merge with Graphite Merge Queue label Jun 18, 2025
Copy link
Contributor

graphite-app bot commented Jun 18, 2025

Merge activity

@graphite-app graphite-app bot force-pushed the 06-17-feat_traverse_introduce_traversectx_a_state_ branch from e74c20c to 8ef1be2 Compare June 18, 2025 06:42
@graphite-app graphite-app bot force-pushed the 06-17-refactor_minifier_make_ctx_take_mut_traversectx_ branch from a48d137 to d5a8f18 Compare June 18, 2025 06:42
Base automatically changed from 06-17-feat_traverse_introduce_traversectx_a_state_ to main June 18, 2025 06:48
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label Jun 18, 2025
@graphite-app graphite-app bot merged commit d5a8f18 into main Jun 18, 2025
25 checks passed
@graphite-app graphite-app bot deleted the 06-17-refactor_minifier_make_ctx_take_mut_traversectx_ branch June 18, 2025 06:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-minifier Area - Minifier A-transformer Area - Transformer / Transpiler C-cleanup Category - technical debt or refactoring. Solution not expected to change behavior
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants