Skip to content

perf(transformer/tagged-template-transform): improve performance#15834

Merged
graphite-app[bot] merged 1 commit intomainfrom
11-18-perf_transformer_tagged-template-transform_improve_performance
Jan 15, 2026
Merged

perf(transformer/tagged-template-transform): improve performance#15834
graphite-app[bot] merged 1 commit intomainfrom
11-18-perf_transformer_tagged-template-transform_improve_performance

Conversation

@Dunqing
Copy link
Member

@Dunqing Dunqing commented Nov 18, 2025

Made some improvements based on the suggestions in #15664

@github-actions github-actions bot added A-transformer Area - Transformer / Transpiler C-performance Category - Solution not expected to change functional behavior, only performance labels Nov 18, 2025
Copy link
Member Author

Dunqing commented Nov 18, 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.

@codspeed-hq
Copy link

codspeed-hq bot commented Nov 18, 2025

CodSpeed Performance Report

Merging this PR will not alter performance

Comparing 11-18-perf_transformer_tagged-template-transform_improve_performance (8a8575a) with main (fdd1e1e)

Summary

✅ 38 untouched benchmarks
⏩ 7 skipped benchmarks1

Footnotes

  1. 7 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@Dunqing Dunqing marked this pull request as ready for review November 18, 2025 12:31
Copy link
Member

@overlookmotel overlookmotel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think to avoid a perf hit in enter_expression, you want it to be:

impl<'a> Traverse<'a, TransformState<'a>> for TaggedTemplateTransform<'a, '_> {
    // `#[inline]` because this is a hot path and most `Expression`s are not `TaggedTemplateExpression`s,
    // so we want this inlined to handle the common case without a function call
    #[inline]
    fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
        if matches!(expr, Expression::TaggedTemplateExpression(_)) {
          self.transform_tagged_template(expr, ctx);
        }
    }
}

The idea is that probably 0.01% of Expressions are TaggedTemplateExpressions, so for the common case all you want in the hot path is "Is this a TaggedTemplateExpression? No it's not. Exit." That's a very small function, so it can be inlined, which means no overhead of a function call on the hot path.

@graphite-app graphite-app bot changed the base branch from 11-18-fix_transformer_handle_escape_sequences_in_tagged_template_transform to graphite-base/15834 November 19, 2025 23:36
@overlookmotel
Copy link
Member

overlookmotel commented Nov 19, 2025

Another possible improvement (though it doesn't really matter because TaggedTemplateExpressions are so rare anyway):

There are 2 loops at start of transform_template_literal:

  1. Check if all cooked == raw.
  2. Convert each cooked to StringLiteral.

You could merge them into 1 loop by doing the cooked == raw check at same time as transforming cooked to strings.

Also the logic for creating template_arguments could avoid vec_from_iter (slow):

let template_arguments = if needs_raw_array {
    let raws_argument = /* ... */;
    ctx.ast.vec_from_array([cooked_argument, raws_argument])
} else {
    ctx.ast.vec1(cooked_argument)
};

@graphite-app graphite-app bot force-pushed the 11-18-perf_transformer_tagged-template-transform_improve_performance branch from ff8f872 to e4487e0 Compare November 19, 2025 23:42
@graphite-app graphite-app bot force-pushed the graphite-base/15834 branch from d973355 to 7c46a9e Compare November 19, 2025 23:42
@graphite-app graphite-app bot changed the base branch from graphite-base/15834 to main November 19, 2025 23:42
@graphite-app graphite-app bot force-pushed the 11-18-perf_transformer_tagged-template-transform_improve_performance branch from e4487e0 to 2a720db Compare November 19, 2025 23:43
@Dunqing Dunqing force-pushed the 11-18-perf_transformer_tagged-template-transform_improve_performance branch from 2a720db to 374c619 Compare January 12, 2026 02:57
Copilot AI review requested due to automatic review settings January 12, 2026 02:57
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements performance optimizations for the tagged template transform plugin based on suggestions from PR #15664. The changes focus on improving the efficiency of detecting </script tags in template literals.

Changes:

  • Moved SCRIPT_TAG constant to module level and added SCRIPT_TAG_LEN constant for reusability
  • Refactored the contains_closing_script_tag loop to be more efficient by checking for < first before doing full tag comparison
  • Extracted the case-insensitive script tag check into a separate is_script_close_tag helper function (copied from oxc_codegen/src/str.rs)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@Dunqing Dunqing force-pushed the 11-18-perf_transformer_tagged-template-transform_improve_performance branch from 374c619 to 8a8575a Compare January 12, 2026 03:07
@Dunqing Dunqing requested a review from overlookmotel January 12, 2026 03:07
@Dunqing
Copy link
Member Author

Dunqing commented Jan 12, 2026

I've applied all your suggestions. Thank you!

@overlookmotel overlookmotel added the 0-merge Merge with Graphite Merge Queue label Jan 15, 2026
Copy link
Member

overlookmotel commented Jan 15, 2026

Merge activity

)

Made some improvements based on the suggestions in #15664
@graphite-app graphite-app bot force-pushed the 11-18-perf_transformer_tagged-template-transform_improve_performance branch from 8a8575a to 23449e0 Compare January 15, 2026 12:30
@graphite-app graphite-app bot merged commit 23449e0 into main Jan 15, 2026
21 checks passed
@graphite-app graphite-app bot deleted the 11-18-perf_transformer_tagged-template-transform_improve_performance branch January 15, 2026 12:36
@graphite-app graphite-app bot removed the 0-merge Merge with Graphite Merge Queue label Jan 15, 2026
graphite-app bot pushed a commit that referenced this pull request Jan 15, 2026
Follow-on after #15834. Pure refactor. Merge 2 small functions. The separation was only required previously due to inlining `transform_tagged_template`, but we don't do that any more.
graphite-app bot pushed a commit that referenced this pull request Jan 15, 2026
Follow-on after #15834. Pure refactor. Rename a var so it better describes what it contains.
graphite-app bot pushed a commit that referenced this pull request Jan 15, 2026
Follow-on after #15834. Pure refactor. Re-order functions so the main entry to the transform is at the top.

Also add a `debug_assert!`.
graphite-app bot pushed a commit that referenced this pull request Jan 15, 2026
Follow-on after #15834. Nit. Just reformat a comment.
graphite-app bot pushed a commit that referenced this pull request Jan 15, 2026
#18034)

Follow-on after #15834. Tiny perf optimization. Tagged template literals are pretty rare. Add `#[cold]` attr to `transform_tagged_template` to hint to compiler that the branch for "yes, it's a tagged template" in `enter_expression` is unlikely to be taken. This should make the compiler make "not a tagged template" the default path.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-transformer Area - Transformer / Transpiler C-performance Category - Solution not expected to change functional behavior, only performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants