Skip to content

feat(es/minifier): Implement partial evaluation of array join #10758

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
merged 6 commits into from
Jul 3, 2025
Merged

Conversation

kdy1
Copy link
Member

@kdy1 kdy1 commented Jul 2, 2025

Description:

@kdy1 kdy1 added this to the Planned milestone Jul 2, 2025
@kdy1 kdy1 self-assigned this Jul 2, 2025
Copy link

changeset-bot bot commented Jul 2, 2025

⚠️ No Changeset found

Latest commit: 481b60b

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

codspeed-hq bot commented Jul 2, 2025

CodSpeed Performance Report

Merging #10758 will not alter performance

Comparing kdy1/min-3 (481b60b) with main (4304f91)

Summary

✅ 141 untouched benchmarks

@kdy1 kdy1 requested a review from Copilot July 2, 2025 14:58
Copy link

@Copilot 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 enhances the minifier’s handling of Array.prototype.join by introducing partial optimizations for mixed literal/expression arrays, adds empty-array join handling, and updates expected outputs and size snapshots throughout the test suite.

  • Add compress_array_join_partial (and empty-array handling) in misc.rs to transform .join() calls into more efficient concatenations.
  • Update numerous test fixtures and snapshot files to reflect new string-concatenation outputs and tiny size reductions.
  • Introduce a GroupType enum to aid grouping logic in the new partial join optimization.

Reviewed Changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
crates/swc_ecma_minifier/src/compress/pure/misc.rs Added empty-array join handling, compress_array_join_partial, and GroupType enum
crates/swc_ecma_minifier/tests/terser/compress/arrays/constant_join_3/output.js Updated expected output from .join() to explicit string concatenation
crates/swc_ecma_minifier/tests/projects-size.snapshot.md Adjusted compressed size for jquery.mobile-1.4.2.js
crates/swc_ecma_minifier/tests/libs-size.snapshot.md Minor size adjustments for echarts.js, terser.js, and three.js
crates/swc_ecma_minifier/tests/fixture/** Many fixture outputs updated to use string concatenations instead of .join()
Comments suppressed due to low confidence (2)

crates/swc_ecma_minifier/src/compress/pure/misc.rs:99

  • [nitpick] The enum variant names Literals and Expression are somewhat ambiguous. Consider renaming them to LiteralGroup and ExpressionItem (or similar) to clarify their roles.
enum GroupType<'a> {

crates/swc_ecma_minifier/src/compress/pure/misc.rs:702

  • Add unit tests covering scenarios where: separators are longer than one character, comma separators with under six consecutive literals, and cases mixing literals and expressions, to verify both optimized and non-optimized paths.
            // Only optimize for single-character separators to avoid bloating the code

@@ -625,6 +649,270 @@ impl Pure<'_> {
.into()
}

/// Performs partial optimization on array.join() when there are mixed literals and expressions.
/// Groups consecutive literals into string concatenations.
fn compress_array_join_partial(
Copy link
Preview

Copilot AI Jul 2, 2025

Choose a reason for hiding this comment

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

[nitpick] This function exceeds 250 lines and handles multiple concerns (grouping, threshold logic, two build strategies). Consider extracting smaller helper functions (e.g., for grouping literals, building concatenations) to improve readability and testability.

Copilot uses AI. Check for mistakes.

Comment on lines 698 to 709
if max_consecutive < 2 {
return None;
}

// Only optimize for single-character separators to avoid bloating the code
// Long separators like "really-long-separator" should not be optimized
if separator.len() > 1 {
return None;
}

// For comma separator, require a higher threshold to avoid infinite loops
if separator == "," && max_consecutive < 6 {
Copy link
Preview

Copilot AI Jul 2, 2025

Choose a reason for hiding this comment

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

[nitpick] Thresholds like max_consecutive < 2, separator.len() > 1, and the comma-specific < 6 check are magic numbers. Extract them as named constants or document their rationale to make the code easier to understand and adjust.

Suggested change
if max_consecutive < 2 {
return None;
}
// Only optimize for single-character separators to avoid bloating the code
// Long separators like "really-long-separator" should not be optimized
if separator.len() > 1 {
return None;
}
// For comma separator, require a higher threshold to avoid infinite loops
if separator == "," && max_consecutive < 6 {
const MIN_CONSECUTIVE_LITERALS: usize = 2;
const MAX_CONSECUTIVE_FOR_COMMA: usize = 6;
const MAX_SEPARATOR_LENGTH: usize = 1;
if max_consecutive < MIN_CONSECUTIVE_LITERALS {
return None;
}
// Only optimize for single-character separators to avoid bloating the code
// Long separators like "really-long-separator" should not be optimized
if separator.len() > MAX_SEPARATOR_LENGTH {
return None;
}
// For comma separator, require a higher threshold to avoid infinite loops
if separator == "," && max_consecutive < MAX_CONSECUTIVE_FOR_COMMA {

Copilot uses AI. Check for mistakes.

@kdy1 kdy1 marked this pull request as ready for review July 3, 2025 08:38
@kdy1 kdy1 requested a review from a team as a code owner July 3, 2025 08:38
kodiakhq[bot]
kodiakhq bot previously approved these changes Jul 3, 2025
kodiakhq[bot]
kodiakhq bot previously approved these changes Jul 3, 2025
@kdy1 kdy1 enabled auto-merge (squash) July 3, 2025 08:43
@kdy1 kdy1 disabled auto-merge July 3, 2025 08:43
@kdy1 kdy1 changed the title feat(es/minifier): Implement more array evaluation feat(es/minifier): Implement partial evaluation of array join Jul 3, 2025
@kdy1 kdy1 enabled auto-merge (squash) July 3, 2025 08:44
@kdy1 kdy1 merged commit bdf3a98 into main Jul 3, 2025
168 checks passed
@kdy1 kdy1 deleted the kdy1/min-3 branch July 3, 2025 09:16
@kdy1 kdy1 modified the milestones: Planned, v1.12.10 Jul 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant