Skip to content

Commit

Permalink
feat(es/minifier): Turn 1 * v into +v (#9903)
Browse files Browse the repository at this point in the history
  • Loading branch information
Austaras authored Jan 20, 2025
1 parent 09b3b37 commit a228347
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .changeset/tall-apples-cross.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
swc_core: patch
swc_ecma_minifier: patch
---

feat(es/minifier): Turn '1 *' into '+'
1 change: 1 addition & 0 deletions crates/swc_ecma_minifier/src/compress/pure/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ impl VisitMut for Pure<'_> {
}

self.lift_minus(e);
self.optimize_to_number(e);

if e.is_seq() {
debug_assert_valid(e);
Expand Down
20 changes: 20 additions & 0 deletions crates/swc_ecma_minifier/src/compress/pure/numbers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,24 @@ impl Pure<'_> {
}
}
}

pub(super) fn optimize_to_number(&mut self, e: &mut Expr) {
if let Expr::Bin(bin) = e {
if bin.op == op!("*")
&& matches!(&*bin.left, Expr::Lit(Lit::Num(Number { value: 1.0, .. })))
{
report_change!("numbers: Turn '1 *' into '+'");
self.changed = true;

let value = bin.right.take();
let span = bin.span;

*e = Expr::Unary(UnaryExpr {
span,
op: op!(unary, "+"),
arg: value,
})
}
}
}
}
2 changes: 1 addition & 1 deletion crates/swc_ecma_minifier/tests/benches-full/d3.js
Original file line number Diff line number Diff line change
Expand Up @@ -1481,7 +1481,7 @@ function(global, factory) {
},
rgb: function() {
var y = (this.l + 16) / 116, x = isNaN(this.a) ? y : y + this.a / 500, z = isNaN(this.b) ? y : y - this.b / 200;
return new Rgb(lrgb2rgb(3.1338561 * (x = 0.96422 * lab2xyz(x)) - 1.6168667 * (y = 1 * lab2xyz(y)) - 0.4906146 * (z = 0.82521 * lab2xyz(z))), lrgb2rgb(-0.9787684 * x + 1.9161415 * y + 0.0334540 * z), lrgb2rgb(0.0719453 * x - 0.2289914 * y + 1.4052427 * z), this.opacity);
return new Rgb(lrgb2rgb(3.1338561 * (x = 0.96422 * lab2xyz(x)) - 1.6168667 * (y = +lab2xyz(y)) - 0.4906146 * (z = 0.82521 * lab2xyz(z))), lrgb2rgb(-0.9787684 * x + 1.9161415 * y + 0.0334540 * z), lrgb2rgb(0.0719453 * x - 0.2289914 * y + 1.4052427 * z), this.opacity);
}
})), define1(Hcl, hcl, extend(Color, {
brighter: function(k) {
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_minifier/tests/benches-full/jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@
*/ function(window1) {
var i, support, Expr, getText, isXML, tokenize, compile, select, outermostContext, sortInput, hasDuplicate, // Local document vars
setDocument, document, docElem, documentIsHTML, rbuggyQSA, rbuggyMatches, matches, contains, // Instance-specific data
expando = "sizzle" + 1 * new Date(), preferredDoc = window1.document, dirruns = 0, done = 0, classCache = createCache(), tokenCache = createCache(), compilerCache = createCache(), nonnativeSelectorCache = createCache(), sortOrder = function(a, b) {
expando = "sizzle" + +new Date(), preferredDoc = window1.document, dirruns = 0, done = 0, classCache = createCache(), tokenCache = createCache(), compilerCache = createCache(), nonnativeSelectorCache = createCache(), sortOrder = function(a, b) {
return a === b && (hasDuplicate = !0), 0;
}, // Instance methods
hasOwn = {}.hasOwnProperty, arr = [], pop = arr.pop, pushNative = arr.push, push = arr.push, slice = arr.slice, // Use a stripped-down indexOf as it's faster than native
Expand Down
2 changes: 1 addition & 1 deletion crates/swc_ecma_minifier/tests/benches-full/victory.js
Original file line number Diff line number Diff line change
Expand Up @@ -1302,7 +1302,7 @@
},
rgb: function() {
var y = (this.l + 16) / 116, x = isNaN(this.a) ? y : y + this.a / 500, z = isNaN(this.b) ? y : y - this.b / 200;
return x = 0.96422 * lab2xyz(x), y = 1 * lab2xyz(y), z = 0.82521 * lab2xyz(z), new _color_js__WEBPACK_IMPORTED_MODULE_1__.Rgb(lrgb2rgb(3.1338561 * x - 1.6168667 * y - 0.4906146 * z), lrgb2rgb(-0.9787684 * x + 1.9161415 * y + 0.0334540 * z), lrgb2rgb(0.0719453 * x - 0.2289914 * y + 1.4052427 * z), this.opacity);
return x = 0.96422 * lab2xyz(x), y = +lab2xyz(y), z = 0.82521 * lab2xyz(z), new _color_js__WEBPACK_IMPORTED_MODULE_1__.Rgb(lrgb2rgb(3.1338561 * x - 1.6168667 * y - 0.4906146 * z), lrgb2rgb(-0.9787684 * x + 1.9161415 * y + 0.0334540 * z), lrgb2rgb(0.0719453 * x - 0.2289914 * y + 1.4052427 * z), this.opacity);
}
})), Object(_define_js__WEBPACK_IMPORTED_MODULE_0__.default)(Hcl, hcl, Object(_define_js__WEBPACK_IMPORTED_MODULE_0__.extend)(_color_js__WEBPACK_IMPORTED_MODULE_1__.Color, {
brighter: function(k) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19341,7 +19341,7 @@
if (-1 === t && (t = 6), n < 0 ? (u = 0, n = -n) : n > 15 && (u = 2, n -= 16), o < 1 || o > 9 || 8 !== r || n < 8 || n > 15 || t < 0 || t > 9 || a < 0 || a > 4) return s(e, -2);
8 === n && (n = 9);
var l = new w();
return e.state = l, l.strm = e, l.wrap = u, l.gzhead = null, l.w_bits = n, l.w_size = 1 << l.w_bits, l.w_mask = l.w_size - 1, l.hash_bits = o + 7, l.hash_size = 1 << l.hash_bits, l.hash_mask = l.hash_size - 1, l.hash_shift = ~~((l.hash_bits + 3 - 1) / 3), l.window = new i.Buf8(2 * l.w_size), l.head = new i.Buf16(l.hash_size), l.prev = new i.Buf16(l.w_size), l.lit_bufsize = 1 << o + 6, l.pending_buf_size = 4 * l.lit_bufsize, l.pending_buf = new i.Buf8(l.pending_buf_size), l.d_buf = 1 * l.lit_bufsize, l.l_buf = 3 * l.lit_bufsize, l.level = t, l.strategy = a, l.method = r, _(e);
return e.state = l, l.strm = e, l.wrap = u, l.gzhead = null, l.w_bits = n, l.w_size = 1 << l.w_bits, l.w_mask = l.w_size - 1, l.hash_bits = o + 7, l.hash_size = 1 << l.hash_bits, l.hash_mask = l.hash_size - 1, l.hash_shift = ~~((l.hash_bits + 3 - 1) / 3), l.window = new i.Buf8(2 * l.w_size), l.head = new i.Buf16(l.hash_size), l.prev = new i.Buf16(l.w_size), l.lit_bufsize = 1 << o + 6, l.pending_buf_size = 4 * l.lit_bufsize, l.pending_buf = new i.Buf8(l.pending_buf_size), l.d_buf = +l.lit_bufsize, l.l_buf = 3 * l.lit_bufsize, l.level = t, l.strategy = a, l.method = r, _(e);
}
n = [
new D(0, 0, 0, 0, function(e, t) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4109,7 +4109,7 @@
case 58:
return d.trim() + c.replace(F, "$1" + d.trim());
default:
if (0 < 1 * e && 0 < c.indexOf("\f")) return c.replace(F, (58 === d.charCodeAt(0) ? "" : "$1") + d.trim());
if (0 < +e && 0 < c.indexOf("\f")) return c.replace(F, (58 === d.charCodeAt(0) ? "" : "$1") + d.trim());
}
return d + c;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5834,7 +5834,7 @@
}
this.index++;
}
number *= 1, this.tokens.push({
number = +number, this.tokens.push({
index: start,
text: number,
json: !0,
Expand Down

0 comments on commit a228347

Please sign in to comment.