Skip to content

Commit

Permalink
feat(es/minifier): Inline into the arguments of new using seq inlin…
Browse files Browse the repository at this point in the history
…er (#8127)
  • Loading branch information
Austaras committed Oct 18, 2023
1 parent 748a7fe commit 4f67794
Show file tree
Hide file tree
Showing 20 changed files with 177 additions and 212 deletions.
22 changes: 21 additions & 1 deletion crates/swc_ecma_minifier/src/compress/optimize/sequences.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1855,13 +1855,33 @@ impl Optimizer<'_> {
}

Expr::New(NewExpr {
callee: b_callee, ..
callee: b_callee,
args: b_args,
..
}) => {
trace_op!("seq: Try callee of new");
if self.merge_sequential_expr(a, b_callee)? {
return Ok(true);
}

if !self.is_skippable_for_seq(Some(a), b_callee) {
return Ok(false);
}

if let Some(b_args) = b_args {
for arg in b_args {
trace_op!("seq: Try arg of new exp");

if self.merge_sequential_expr(a, &mut arg.expr)? {
return Ok(true);
}

if !self.is_skippable_for_seq(Some(a), &arg.expr) {
return Ok(false);
}
}
}

return Ok(false);
}

Expand Down
4 changes: 2 additions & 2 deletions crates/swc_ecma_minifier/tests/benches-full/d3.js
Original file line number Diff line number Diff line change
Expand Up @@ -1294,7 +1294,7 @@
return a <= 0 && (r = g = b = NaN), new Rgb(r, g, b, a);
}
function rgbConvert(o) {
return (o instanceof Color || (o = color(o)), o) ? (o = o.rgb(), new Rgb(o.r, o.g, o.b, o.opacity)) : new Rgb;
return (o instanceof Color || (o = color(o)), o) ? new Rgb((o = o.rgb()).r, o.g, o.b, o.opacity) : new Rgb;
}
function rgb(r, g, b, opacity) {
return 1 == arguments.length ? rgbConvert(r) : new Rgb(r, g, b, null == opacity ? 1 : opacity);
Expand Down Expand Up @@ -1433,7 +1433,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 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 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);
}
})), define1(Hcl, hcl, extend(Color, {
brighter: function(k) {
Expand Down
138 changes: 64 additions & 74 deletions crates/swc_ecma_minifier/tests/benches-full/echarts.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions crates/swc_ecma_minifier/tests/benches-full/terser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1523,10 +1523,10 @@
}
{
const AccessorClass = "get" === accessor_type ? AST_ObjectGetter : AST_ObjectSetter;
return name = get_symbol_ast(name), new AccessorClass({
return new AccessorClass({
start,
static: is_static,
key: name,
key: name = get_symbol_ast(name),
quote: name instanceof AST_SymbolMethod ? property_token.quote : void 0,
value: create_accessor(),
end: prev()
Expand Down Expand Up @@ -18003,7 +18003,7 @@
var entries = fs.readdirSync(dir);
} catch (ex) {}
if (entries) {
var pattern = "^" + path.basename(glob).replace(/[.+^$[\]\\(){}]/g, "\\$&").replace(/\*/g, "[^/\\\\]*").replace(/\?/g, "[^/\\\\]") + "$", mod = "win32" === process.platform ? "i" : "", rx = new RegExp(pattern, mod), results = entries.filter(function(name) {
var rx = RegExp("^" + path.basename(glob).replace(/[.+^$[\]\\(){}]/g, "\\$&").replace(/\*/g, "[^/\\\\]*").replace(/\?/g, "[^/\\\\]") + "$", "win32" === process.platform ? "i" : ""), results = entries.filter(function(name) {
return rx.test(name);
}).map(function(name) {
return path.join(dir, name);
Expand Down
46 changes: 17 additions & 29 deletions crates/swc_ecma_minifier/tests/benches-full/three.js
Original file line number Diff line number Diff line change
Expand Up @@ -7232,7 +7232,7 @@
},
clone: function(data) {
void 0 === data.arrayBuffers && (data.arrayBuffers = {}), void 0 === this.array.buffer._uuid && (this.array.buffer._uuid = MathUtils.generateUUID()), void 0 === data.arrayBuffers[this.array.buffer._uuid] && (data.arrayBuffers[this.array.buffer._uuid] = this.array.slice(0).buffer);
var array = new this.array.constructor(data.arrayBuffers[this.array.buffer._uuid]), ib = new InterleavedBuffer(array, this.stride);
var ib = new InterleavedBuffer(new this.array.constructor(data.arrayBuffers[this.array.buffer._uuid]), this.stride);
return ib.setUsage(this.usage), ib;
},
onUpload: function(callback) {
Expand Down Expand Up @@ -7339,7 +7339,7 @@
function Sprite(material) {
if (Object3D.call(this), this.type = 'Sprite', void 0 === _geometry) {
_geometry = new BufferGeometry();
var float32Array = new Float32Array([
var interleavedBuffer = new InterleavedBuffer(new Float32Array([
-0.5,
-0.5,
0,
Expand All @@ -7360,7 +7360,7 @@
0,
0,
1
]), interleavedBuffer = new InterleavedBuffer(float32Array, 5);
]), 5);
_geometry.setIndex([
0,
1,
Expand Down Expand Up @@ -11148,9 +11148,9 @@
return this.curves.push(curve), this.currentPoint.set(aX, aY), this;
},
splineThru: function(pts) {
var npts = [
var curve = new SplineCurve([
this.currentPoint.clone()
].concat(pts), curve = new SplineCurve(npts);
].concat(pts));
return this.curves.push(curve), this.currentPoint.copy(pts[pts.length - 1]), this;
},
arc: function(aX, aY, aRadius, aStartAngle, aEndAngle, aClockwise) {
Expand Down Expand Up @@ -11550,7 +11550,7 @@
if (void 0 !== arrayBufferMap[uuid]) return arrayBufferMap[uuid];
var arrayBuffer = json.arrayBuffers[uuid], ab = new Uint32Array(arrayBuffer).buffer;
return arrayBufferMap[uuid] = ab, ab;
}(json, interleavedBuffer.buffer), array = getTypedArray(interleavedBuffer.type, buffer), ib = new InterleavedBuffer(array, interleavedBuffer.stride);
}(json, interleavedBuffer.buffer), ib = new InterleavedBuffer(getTypedArray(interleavedBuffer.type, buffer), interleavedBuffer.stride);
return ib.uuid = interleavedBuffer.uuid, interleavedBufferMap[uuid] = ib, ib;
}
var geometry = json.isInstancedBufferGeometry ? new InstancedBufferGeometry() : new BufferGeometry(), index = json.data.index;
Expand All @@ -11561,10 +11561,8 @@
var attributes = json.data.attributes;
for(var key in attributes){
var attribute = attributes[key], bufferAttribute = void 0;
if (attribute.isInterleavedBufferAttribute) {
var interleavedBuffer = getInterleavedBuffer(json.data, attribute.data);
bufferAttribute = new InterleavedBufferAttribute(interleavedBuffer, attribute.itemSize, attribute.offset, attribute.normalized);
} else {
if (attribute.isInterleavedBufferAttribute) bufferAttribute = new InterleavedBufferAttribute(getInterleavedBuffer(json.data, attribute.data), attribute.itemSize, attribute.offset, attribute.normalized);
else {
var _typedArray = getTypedArray(attribute.type, attribute.array);
bufferAttribute = new (attribute.isInstancedBufferAttribute ? InstancedBufferAttribute : BufferAttribute)(_typedArray, attribute.itemSize, attribute.normalized);
}
Expand All @@ -11574,14 +11572,7 @@
if (morphAttributes) for(var _key in morphAttributes){
for(var attributeArray = morphAttributes[_key], array = [], i = 0, il = attributeArray.length; i < il; i++){
var _attribute = attributeArray[i], _bufferAttribute = void 0;
if (_attribute.isInterleavedBufferAttribute) {
var _interleavedBuffer = getInterleavedBuffer(json.data, _attribute.data);
_bufferAttribute = new InterleavedBufferAttribute(_interleavedBuffer, _attribute.itemSize, _attribute.offset, _attribute.normalized);
} else {
var _typedArray2 = getTypedArray(_attribute.type, _attribute.array);
_bufferAttribute = new BufferAttribute(_typedArray2, _attribute.itemSize, _attribute.normalized);
}
void 0 !== _attribute.name && (_bufferAttribute.name = _attribute.name), array.push(_bufferAttribute);
_bufferAttribute = _attribute.isInterleavedBufferAttribute ? new InterleavedBufferAttribute(getInterleavedBuffer(json.data, _attribute.data), _attribute.itemSize, _attribute.offset, _attribute.normalized) : new BufferAttribute(getTypedArray(_attribute.type, _attribute.array), _attribute.itemSize, _attribute.normalized), void 0 !== _attribute.name && (_bufferAttribute.name = _attribute.name), array.push(_bufferAttribute);
}
geometry.morphAttributes[_key] = array;
}
Expand Down Expand Up @@ -11792,8 +11783,7 @@
} : null;
}
if (void 0 !== json && json.length > 0) {
var manager = new LoadingManager(onLoad);
(loader = new ImageLoader(manager)).setCrossOrigin(this.crossOrigin);
(loader = new ImageLoader(new LoadingManager(onLoad))).setCrossOrigin(this.crossOrigin);
for(var i = 0, il = json.length; i < il; i++){
var image = json[i], url = image.url;
if (Array.isArray(url)) {
Expand Down Expand Up @@ -11869,10 +11859,10 @@
object = new LightProbe().fromJSON(data);
break;
case 'SkinnedMesh':
geometry = getGeometry(data.geometry), material = getMaterial(data.material), object = new SkinnedMesh(geometry, material), void 0 !== data.bindMode && (object.bindMode = data.bindMode), void 0 !== data.bindMatrix && object.bindMatrix.fromArray(data.bindMatrix), void 0 !== data.skeleton && (object.skeleton = data.skeleton);
object = new SkinnedMesh(geometry = getGeometry(data.geometry), material = getMaterial(data.material)), void 0 !== data.bindMode && (object.bindMode = data.bindMode), void 0 !== data.bindMatrix && object.bindMatrix.fromArray(data.bindMatrix), void 0 !== data.skeleton && (object.skeleton = data.skeleton);
break;
case 'Mesh':
geometry = getGeometry(data.geometry), material = getMaterial(data.material), object = new Mesh(geometry, material);
object = new Mesh(geometry = getGeometry(data.geometry), material = getMaterial(data.material));
break;
case 'InstancedMesh':
geometry = getGeometry(data.geometry), material = getMaterial(data.material);
Expand Down Expand Up @@ -13972,8 +13962,7 @@
new Vector3(-PHI, INV_PHI, 0)
], PMREMGenerator = function() {
function PMREMGenerator(renderer) {
var weights, poleAxis;
this._renderer = renderer, this._pingPongRenderTarget = null, this._blurMaterial = (weights = new Float32Array(20), poleAxis = new Vector3(0, 1, 0), new RawShaderMaterial({
this._renderer = renderer, this._pingPongRenderTarget = null, this._blurMaterial = new RawShaderMaterial({
name: 'SphericalGaussianBlur',
defines: {
n: 20
Expand All @@ -13986,7 +13975,7 @@
value: 1
},
weights: {
value: weights
value: new Float32Array(20)
},
latitudinal: {
value: !1
Expand All @@ -13998,7 +13987,7 @@
value: 0
},
poleAxis: {
value: poleAxis
value: new Vector3(0, 1, 0)
},
inputEncoding: {
value: ENCODINGS[3000]
Expand All @@ -14012,7 +14001,7 @@
blending: 0,
depthTest: !1,
depthWrite: !1
})), this._equirectShader = null, this._cubemapShader = null, this._compileMaterial(this._blurMaterial);
}), this._equirectShader = null, this._cubemapShader = null, this._compileMaterial(this._blurMaterial);
}
var _proto = PMREMGenerator.prototype;
return _proto.fromScene = function(scene, sigma, near, far) {
Expand Down Expand Up @@ -14119,15 +14108,14 @@
target.viewport.set(x, y, width, height), target.scissor.set(x, y, width, height);
}
function _getEquirectShader() {
var texelSize = new Vector2(1, 1);
return new RawShaderMaterial({
name: 'EquirectangularToCubeUV',
uniforms: {
envMap: {
value: null
},
texelSize: {
value: texelSize
value: new Vector2(1, 1)
},
inputEncoding: {
value: ENCODINGS[3000]
Expand Down
29 changes: 7 additions & 22 deletions crates/swc_ecma_minifier/tests/benches-full/victory.js
Original file line number Diff line number Diff line change
Expand Up @@ -944,7 +944,7 @@
return a <= 0 && (r = g = b = NaN), new Rgb(r, g, b, a);
}
function rgbConvert(o) {
return (o instanceof Color || (o = color(o)), o) ? (o = o.rgb(), new Rgb(o.r, o.g, o.b, o.opacity)) : new Rgb;
return (o instanceof Color || (o = color(o)), o) ? new Rgb((o = o.rgb()).r, o.g, o.b, o.opacity) : new Rgb;
}
function rgb(r, g, b, opacity) {
return 1 == arguments.length ? rgbConvert(r) : new Rgb(r, g, b, null == opacity ? 1 : opacity);
Expand Down Expand Up @@ -8436,10 +8436,7 @@
return createChainableTypeChecker(function(props, propName, componentName, location, propFullName) {
if ('function' != typeof typeChecker) return new PropTypeError('Property `' + propFullName + '` of component `' + componentName + '` has invalid PropType notation inside arrayOf.');
var propValue = props[propName];
if (!Array.isArray(propValue)) {
var propType = getPropType(propValue);
return new PropTypeError('Invalid ' + location + ' `' + propFullName + '` of type ' + ('`' + propType) + '` supplied to `' + componentName + '`, expected an array.');
}
if (!Array.isArray(propValue)) return new PropTypeError('Invalid ' + location + ' `' + propFullName + '` of type ' + ('`' + getPropType(propValue)) + '` supplied to `' + componentName + '`, expected an array.');
for(var i = 0; i < propValue.length; i++){
var error = typeChecker(propValue, i, componentName, location, propFullName + '[' + i + ']', ReactPropTypesSecret);
if (error instanceof Error) return error;
Expand All @@ -8449,25 +8446,17 @@
},
element: createChainableTypeChecker(function(props, propName, componentName, location, propFullName) {
var propValue = props[propName];
if (!isValidElement(propValue)) {
var propType = getPropType(propValue);
return new PropTypeError('Invalid ' + location + ' `' + propFullName + '` of type ' + ('`' + propType) + '` supplied to `' + componentName + '`, expected a single ReactElement.');
}
return null;
return isValidElement(propValue) ? null : new PropTypeError('Invalid ' + location + ' `' + propFullName + '` of type ' + ('`' + getPropType(propValue)) + '` supplied to `' + componentName + '`, expected a single ReactElement.');
}),
elementType: createChainableTypeChecker(function(props, propName, componentName, location, propFullName) {
var propValue = props[propName];
if (!ReactIs.isValidElementType(propValue)) {
var propType = getPropType(propValue);
return new PropTypeError('Invalid ' + location + ' `' + propFullName + '` of type ' + ('`' + propType) + '` supplied to `' + componentName + '`, expected a single ReactElement type.');
}
return null;
return ReactIs.isValidElementType(propValue) ? null : new PropTypeError('Invalid ' + location + ' `' + propFullName + '` of type ' + ('`' + getPropType(propValue)) + '` supplied to `' + componentName + '`, expected a single ReactElement type.');
}),
instanceOf: function(expectedClass) {
return createChainableTypeChecker(function(props, propName, componentName, location, propFullName) {
if (!(props[propName] instanceof expectedClass)) {
var propValue, expectedClassName = expectedClass.name || ANONYMOUS, actualClassName = (propValue = props[propName]).constructor && propValue.constructor.name ? propValue.constructor.name : ANONYMOUS;
return new PropTypeError('Invalid ' + location + ' `' + propFullName + '` of type ' + ('`' + actualClassName) + '` supplied to `' + componentName + "`, expected instance of `" + expectedClassName + '`.');
var propValue, expectedClassName = expectedClass.name || ANONYMOUS;
return new PropTypeError('Invalid ' + location + ' `' + propFullName + '` of type ' + ('`' + ((propValue = props[propName]).constructor && propValue.constructor.name ? propValue.constructor.name : ANONYMOUS)) + '` supplied to `' + componentName + "`, expected instance of `" + expectedClassName + '`.');
}
return null;
});
Expand Down Expand Up @@ -8600,11 +8589,7 @@
function createPrimitiveTypeChecker(expectedType) {
return createChainableTypeChecker(function(props, propName, componentName, location, propFullName, secret) {
var propValue = props[propName];
if (getPropType(propValue) !== expectedType) {
var preciseType = getPreciseType(propValue);
return new PropTypeError('Invalid ' + location + ' `' + propFullName + '` of type ' + ('`' + preciseType) + '` supplied to `' + componentName + "`, expected `" + expectedType + '`.');
}
return null;
return getPropType(propValue) !== expectedType ? new PropTypeError('Invalid ' + location + ' `' + propFullName + '` of type ' + ('`' + getPreciseType(propValue)) + '` supplied to `' + componentName + "`, expected `" + expectedType + '`.') : null;
});
}
function getPropType(propValue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4926,7 +4926,7 @@
length = byteLength / BYTES;
} else if (isTypedArray(data)) return fromList(TypedArrayConstructor, data);
else return typedArrayFrom.call(TypedArrayConstructor, data);
} else byteLength = (length = toIndex(data)) * BYTES, buffer = new ArrayBuffer1(byteLength);
} else buffer = new ArrayBuffer1(byteLength = (length = toIndex(data)) * BYTES);
for(setInternalState(that, {
buffer: buffer,
byteOffset: byteOffset,
Expand Down

1 comment on commit 4f67794

@github-actions
Copy link

Choose a reason for hiding this comment

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

Benchmark

Benchmark suite Current: 4f67794 Previous: 1e9b4e0 Ratio
es/full/bugs-1 285618 ns/iter (± 6329) 284926 ns/iter (± 2607) 1.00
es/full/minify/libraries/antd 1386313980 ns/iter (± 24822725) 1377879056 ns/iter (± 10104762) 1.01
es/full/minify/libraries/d3 289848387 ns/iter (± 2129801) 292009844 ns/iter (± 3206656) 0.99
es/full/minify/libraries/echarts 1103835486 ns/iter (± 8002603) 1102503351 ns/iter (± 5334929) 1.00
es/full/minify/libraries/jquery 88444329 ns/iter (± 131550) 88596702 ns/iter (± 135910) 1.00
es/full/minify/libraries/lodash 103694449 ns/iter (± 401752) 103776679 ns/iter (± 227235) 1.00
es/full/minify/libraries/moment 52299663 ns/iter (± 555346) 52180869 ns/iter (± 87058) 1.00
es/full/minify/libraries/react 18875043 ns/iter (± 75554) 18771671 ns/iter (± 51834) 1.01
es/full/minify/libraries/terser 230926211 ns/iter (± 1411456) 230190320 ns/iter (± 2987375) 1.00
es/full/minify/libraries/three 406505582 ns/iter (± 1425320) 403669079 ns/iter (± 1382799) 1.01
es/full/minify/libraries/typescript 2759524618 ns/iter (± 13642008) 2780142776 ns/iter (± 9213412) 0.99
es/full/minify/libraries/victory 598056874 ns/iter (± 11703653) 594842179 ns/iter (± 8311665) 1.01
es/full/minify/libraries/vue 125107901 ns/iter (± 646092) 125369654 ns/iter (± 311010) 1.00
es/full/codegen/es3 34981 ns/iter (± 323) 35565 ns/iter (± 132) 0.98
es/full/codegen/es5 35082 ns/iter (± 136) 35414 ns/iter (± 119) 0.99
es/full/codegen/es2015 35106 ns/iter (± 161) 35390 ns/iter (± 104) 0.99
es/full/codegen/es2016 35030 ns/iter (± 180) 35520 ns/iter (± 104) 0.99
es/full/codegen/es2017 35077 ns/iter (± 203) 35442 ns/iter (± 145) 0.99
es/full/codegen/es2018 34867 ns/iter (± 218) 35479 ns/iter (± 72) 0.98
es/full/codegen/es2019 35128 ns/iter (± 97) 35395 ns/iter (± 81) 0.99
es/full/codegen/es2020 35152 ns/iter (± 168) 35425 ns/iter (± 55) 0.99
es/full/all/es3 178297141 ns/iter (± 1215853) 178793227 ns/iter (± 754484) 1.00
es/full/all/es5 170952382 ns/iter (± 1087977) 169910563 ns/iter (± 815313) 1.01
es/full/all/es2015 129037936 ns/iter (± 880047) 129143321 ns/iter (± 938336) 1.00
es/full/all/es2016 128931320 ns/iter (± 823433) 128348259 ns/iter (± 1275982) 1.00
es/full/all/es2017 127864302 ns/iter (± 698819) 127726040 ns/iter (± 1163911) 1.00
es/full/all/es2018 124681982 ns/iter (± 613298) 126134827 ns/iter (± 1083044) 0.99
es/full/all/es2019 125025589 ns/iter (± 731101) 124411452 ns/iter (± 1066285) 1.00
es/full/all/es2020 121492810 ns/iter (± 763908) 120600293 ns/iter (± 490042) 1.01
es/full/parser 562089 ns/iter (± 3815) 559807 ns/iter (± 3214) 1.00
es/full/base/fixer 18552 ns/iter (± 137) 18188 ns/iter (± 155) 1.02
es/full/base/resolver_and_hygiene 82925 ns/iter (± 172) 83005 ns/iter (± 271) 1.00

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.