Skip to content

Commit 9e8a1ea

Browse files
committed
Added benchmark, added verbose mode
1 parent 1466c72 commit 9e8a1ea

File tree

15 files changed

+679
-77
lines changed

15 files changed

+679
-77
lines changed

package-lock.json

Lines changed: 408 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"build": "vite build",
4646
"pretest": "npm run build:test",
4747
"test": "vitest run",
48+
"benchmark": "node test/benchmark.js",
4849
"build:test:esbuild": "node test/esbuild.js",
4950
"build:test:rollup": "rollup -c test/rollup.config.ts --configPlugin typescript",
5051
"build:test:vite": "vite build --config test/vite.config.ts",
@@ -61,6 +62,8 @@
6162
"devDependencies": {
6263
"@rollup/plugin-typescript": "^11.1.2",
6364
"@rollup/pluginutils": "^5.0.2",
65+
"benchmark": "^2.1.4",
66+
"benny": "^3.7.1",
6467
"esbuild": "^0.17.19",
6568
"rollup": "^3.26.1",
6669
"tslib": "^2.6.0",

src/esbuild.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ import { getCompiler } from "./utils";
55
import { Options } from "./types";
66
export * from "./types";
77

8-
export default function inlineFunction({ macros = {} }: Options): Plugin {
8+
export default function inlineFunction({
9+
macros = {},
10+
verbose = false,
11+
}: Options): Plugin {
912
return {
1013
name,
1114
setup(build) {
12-
const compile = getCompiler(macros);
15+
const compile = getCompiler(macros, verbose);
1316
build.onLoad({ filter: /.*/ }, async (args) => {
1417
const source = await fs.promises.readFile(args.path, "utf8");
1518
// Replace the calculate function calls with 42

src/rollup.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ import { Plugin } from "rollup";
33
import { getCompiler } from "./utils";
44
import { Options } from "./types";
55

6-
export default function inlineFunction({ macros = {} }: Options): Plugin {
7-
const compile = getCompiler(macros);
6+
export default function inlineFunction({
7+
macros = {},
8+
verbose = false,
9+
}: Options): Plugin {
10+
const compile = getCompiler(macros, verbose);
811
return {
912
name,
1013
transform(code) {

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ export type Macros = (...args: unknown[]) => string;
22
export type MacrosMap = Record<string, (...args: unknown[]) => string>;
33
export interface Options {
44
macros: MacrosMap;
5+
verbose?: boolean;
56
}

src/utils.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,20 @@ import { MacrosMap } from "./types";
22

33
const CALLS_RE = /^( *)(\w+)\((.+)\);/gm;
44
const replaceMacros =
5-
(macros: MacrosMap) => (_, indent: string, id: string, args: string) => {
6-
return macros[id](...args.split(/, +/))
5+
(macros: MacrosMap, verbose: boolean) =>
6+
(_, indent: string, id: string, args: string) => {
7+
const selectedMacros = macros[id];
8+
if (!selectedMacros) return _;
9+
const replacement = selectedMacros(...args.split(/, +/))
710
.split("\n")
811
.filter((str) => str.match(/\S/))
912
.map((str) => indent + str.trim())
1013
.join("\n");
14+
if (verbose)
15+
console.log(`\n${id}(${args}):\n====\n${replacement}\n=====\n`);
16+
return replacement;
1117
};
1218

13-
export const getCompiler = (macros: MacrosMap) => (src: string) =>
14-
src.replace(CALLS_RE, replaceMacros(macros));
19+
export const getCompiler =
20+
(macros: MacrosMap, verbose: boolean) => (src: string) =>
21+
src.replace(CALLS_RE, replaceMacros(macros, verbose));

src/vite.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ import { transformWithEsbuild, Plugin } from "vite";
33
import { Options } from "./types";
44
import { getCompiler } from "./utils";
55

6-
export default function inlineFunction({ macros = {} }: Options): Plugin {
7-
const compile = getCompiler(macros);
6+
export default function inlineFunction({
7+
macros = {},
8+
verbose = false,
9+
}: Options): Plugin {
10+
const compile = getCompiler(macros, verbose);
811
return {
912
name,
1013
enforce: "pre",
Lines changed: 163 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,80 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

33
exports[`plugins > esbuild 1`] = `
4-
"(() => {
5-
// test/utils.ts
6-
var fn2 = (a, b) => a + b;
4+
"// test/utils.ts
5+
function add(u, v) {
6+
u[0] = u[0] + v[0];
7+
u[1] = u[1] + v[1];
8+
}
9+
var fn2 = (a, b) => a + b;
710
8-
// test/test.ts
9-
var foo = [4, 5];
10-
var bar = [5, fn2(6, 12)];
11-
var ZERO = () => [0, 0];
12-
var baz = [33, 22];
13-
var _a0 = baz;
14-
var _b1 = ZERO();
15-
_a0[0] = _a0[0] + _b1[0];
16-
_a0[1] = _a0[1] + _b1[1];
17-
console.log(foo, baz);
18-
var _a2 = ZERO();
19-
var _b3 = bar;
20-
_a2[0] = _a2[0] + _b3[0];
21-
_a2[1] = _a2[1] + _b3[1];
22-
})();
11+
// test/test.ts
12+
var foo = [4, 5];
13+
var bar = [5, fn2(6, 12)];
14+
var ZERO = () => [0, 0];
15+
var baz = [33, 22];
16+
var _a0 = baz;
17+
var _b1 = ZERO();
18+
_a0[0] = _a0[0] + _b1[0];
19+
_a0[1] = _a0[1] + _b1[1];
20+
console.log(foo, baz);
21+
var _a2 = ZERO();
22+
var _b3 = bar;
23+
_a2[0] = _a2[0] + _b3[0];
24+
_a2[1] = _a2[1] + _b3[1];
25+
var crossProduct = (u, v, dest) => u[0] * v[1] - u[1] * v[0];
26+
var crossProduct2 = crossProduct;
27+
var add2 = add;
28+
function inlined() {
29+
const foo2 = [4, 5];
30+
const bar2 = [5, fn2(6, 12)];
31+
const baz2 = [33, 22];
32+
for (let i = 0; i < 100; i++) {
33+
const _a4 = foo2;
34+
const _b5 = bar2;
35+
_a4[0] = _a4[0] + _b5[0];
36+
_a4[1] = _a4[1] + _b5[1];
37+
const _a6 = baz2;
38+
const _b7 = bar2;
39+
_a6[0] = _a6[0] + _b7[0];
40+
_a6[1] = _a6[1] + _b7[1];
41+
const _a8 = foo2;
42+
const _b9 = baz2;
43+
_a8[0] = _a8[0] + _b9[0];
44+
_a8[1] = _a8[1] + _b9[1];
45+
const _a10 = foo2;
46+
const _b11 = bar2;
47+
const _d12 = baz2;
48+
_d12[0] = _a10[0] * _b11[0];
49+
_d12[1] = _a10[1] * _b11[1];
50+
}
51+
return foo2;
52+
}
53+
function notInlined() {
54+
const foo2 = [4, 5];
55+
const bar2 = [5, fn2(6, 12)];
56+
const baz2 = [33, 22];
57+
for (let i = 0; i < 100; i++) {
58+
add2(foo2, bar2);
59+
add2(baz2, bar2);
60+
add2(foo2, baz2);
61+
crossProduct2(foo2, bar2, baz2);
62+
}
63+
return foo2;
64+
}
65+
export {
66+
inlined,
67+
notInlined
68+
};
2369
"
2470
`;
2571

2672
exports[`plugins > rollup 1`] = `
27-
"'use strict';
73+
"function add(u, v) {
74+
u[0] = u[0] + v[0];
75+
u[1] = u[1] + v[1];
76+
}
77+
var fn2 = function (a, b) { return a + b; };
2878
2979
var foo = [4, 5];
3080
var ZERO = function () { return [0, 0]; };
@@ -37,17 +87,110 @@ _a0[0] = _a0[0] + _b1[0];
3787
_a0[1] = _a0[1] + _b1[1];
3888
// so that dead code removal doesn't remove the function altogether
3989
console.log(foo, baz);
90+
var crossProduct = function (u, v, dest) {
91+
return u[0] * v[1] - u[1] * v[0];
92+
};
93+
var crossProduct2 = crossProduct;
94+
var add2 = add;
95+
function inlined() {
96+
var foo = [4, 5];
97+
var bar = [5, fn2(6, 12)];
98+
var baz = [33, 22];
99+
for (var i = 0; i < 100; i++) {
100+
const _a4 = foo;
101+
const _b5 = bar;
102+
_a4[0] = _a4[0] + _b5[0];
103+
_a4[1] = _a4[1] + _b5[1];
104+
const _a6 = baz;
105+
const _b7 = bar;
106+
_a6[0] = _a6[0] + _b7[0];
107+
_a6[1] = _a6[1] + _b7[1];
108+
const _a8 = foo;
109+
const _b9 = baz;
110+
_a8[0] = _a8[0] + _b9[0];
111+
_a8[1] = _a8[1] + _b9[1];
112+
const _a10 = foo;
113+
const _b11 = bar;
114+
const _d12 = baz;
115+
_d12[0] = _a10[0] * _b11[0];
116+
_d12[1] = _a10[1] * _b11[1];
117+
}
118+
return foo;
119+
}
120+
function notInlined() {
121+
var foo = [4, 5];
122+
var bar = [5, fn2(6, 12)];
123+
var baz = [33, 22];
124+
for (var i = 0; i < 100; i++) {
125+
add2(foo, bar);
126+
add2(baz, bar);
127+
add2(foo, baz);
128+
crossProduct2(foo, bar);
129+
}
130+
return foo;
131+
}
132+
133+
export { inlined, notInlined };
40134
"
41135
`;
42136

43137
exports[`plugins > vite 1`] = `
44-
"const foo = [4, 5];
138+
"function add(u, v) {
139+
u[0] = u[0] + v[0];
140+
u[1] = u[1] + v[1];
141+
}
142+
const fn2 = (a, b) => a + b;
143+
const foo = [4, 5];
45144
const ZERO = () => [0, 0];
46145
const baz = [33, 22];
47146
const _a0 = baz;
48147
const _b1 = ZERO();
49148
_a0[0] = _a0[0] + _b1[0];
50149
_a0[1] = _a0[1] + _b1[1];
51150
console.log(foo, baz);
151+
const crossProduct = (u, v, dest) => u[0] * v[1] - u[1] * v[0];
152+
const crossProduct2 = crossProduct;
153+
const add2 = add;
154+
function inlined() {
155+
const foo2 = [4, 5];
156+
const bar2 = [5, fn2(6, 12)];
157+
const baz2 = [33, 22];
158+
for (let i = 0; i < 100; i++) {
159+
const _a4 = foo2;
160+
const _b5 = bar2;
161+
_a4[0] = _a4[0] + _b5[0];
162+
_a4[1] = _a4[1] + _b5[1];
163+
const _a6 = baz2;
164+
const _b7 = bar2;
165+
_a6[0] = _a6[0] + _b7[0];
166+
_a6[1] = _a6[1] + _b7[1];
167+
const _a8 = foo2;
168+
const _b9 = baz2;
169+
_a8[0] = _a8[0] + _b9[0];
170+
_a8[1] = _a8[1] + _b9[1];
171+
const _a10 = foo2;
172+
const _b11 = bar2;
173+
const _d12 = baz2;
174+
_d12[0] = _a10[0] * _b11[0];
175+
_d12[1] = _a10[1] * _b11[1];
176+
}
177+
return foo2;
178+
}
179+
function notInlined() {
180+
const foo2 = [4, 5];
181+
const bar2 = [5, fn2(6, 12)];
182+
const baz2 = [33, 22];
183+
for (let i = 0; i < 100; i++) {
184+
add2(foo2, bar2);
185+
add2(baz2, bar2);
186+
add2(foo2, baz2);
187+
crossProduct2(foo2, bar2);
188+
}
189+
return foo2;
190+
}
191+
export {
192+
inlined,
193+
notInlined
194+
};
52195
"
53196
`;

test/benchmark.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import bench from "benny";
2+
import { inlined, notInlined } from "./dist/esbuild.js";
3+
4+
bench.suite(
5+
"My suite",
6+
bench.add("Inlined", inlined),
7+
bench.add("Not inlined", notInlined),
8+
bench.cycle(),
9+
bench.complete()
10+
);

test/esbuild.js

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,13 @@
11
import esbuild from "esbuild";
22
import inlineFunction from "../dist/esbuild.js";
3-
4-
let counter = 0;
5-
const macros = {
6-
add: (a, b) => {
7-
const _a = `_a${counter++}`;
8-
const _b = `_b${counter++}`;
9-
return `
10-
const ${_a} = ${a};
11-
const ${_b} = ${b};
12-
${_a}[0] = ${_a}[0] + ${_b}[0];
13-
${_a}[1] = ${_a}[1] + ${_b}[1];
14-
`;
15-
},
16-
};
3+
import { macros } from "./macros.js";
174

185
esbuild
196
.build({
207
entryPoints: ["./test/test.ts"],
218
bundle: true,
9+
format: "esm",
2210
outfile: "./test/dist/esbuild.js",
23-
plugins: [inlineFunction({ macros })],
11+
plugins: [inlineFunction({ macros, verbose: false })],
2412
})
2513
.catch(() => process.exit(1));

0 commit comments

Comments
 (0)