Skip to content

Commit

Permalink
Added benchmark, added verbose mode
Browse files Browse the repository at this point in the history
  • Loading branch information
w8r committed Jul 5, 2023
1 parent 1466c72 commit 9e8a1ea
Show file tree
Hide file tree
Showing 15 changed files with 679 additions and 77 deletions.
408 changes: 408 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"build": "vite build",
"pretest": "npm run build:test",
"test": "vitest run",
"benchmark": "node test/benchmark.js",
"build:test:esbuild": "node test/esbuild.js",
"build:test:rollup": "rollup -c test/rollup.config.ts --configPlugin typescript",
"build:test:vite": "vite build --config test/vite.config.ts",
Expand All @@ -61,6 +62,8 @@
"devDependencies": {
"@rollup/plugin-typescript": "^11.1.2",
"@rollup/pluginutils": "^5.0.2",
"benchmark": "^2.1.4",
"benny": "^3.7.1",
"esbuild": "^0.17.19",
"rollup": "^3.26.1",
"tslib": "^2.6.0",
Expand Down
7 changes: 5 additions & 2 deletions src/esbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import { getCompiler } from "./utils";
import { Options } from "./types";
export * from "./types";

export default function inlineFunction({ macros = {} }: Options): Plugin {
export default function inlineFunction({
macros = {},
verbose = false,
}: Options): Plugin {
return {
name,
setup(build) {
const compile = getCompiler(macros);
const compile = getCompiler(macros, verbose);
build.onLoad({ filter: /.*/ }, async (args) => {
const source = await fs.promises.readFile(args.path, "utf8");
// Replace the calculate function calls with 42
Expand Down
7 changes: 5 additions & 2 deletions src/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import { Plugin } from "rollup";
import { getCompiler } from "./utils";
import { Options } from "./types";

export default function inlineFunction({ macros = {} }: Options): Plugin {
const compile = getCompiler(macros);
export default function inlineFunction({
macros = {},
verbose = false,
}: Options): Plugin {
const compile = getCompiler(macros, verbose);
return {
name,
transform(code) {
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export type Macros = (...args: unknown[]) => string;
export type MacrosMap = Record<string, (...args: unknown[]) => string>;
export interface Options {
macros: MacrosMap;
verbose?: boolean;
}
15 changes: 11 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@ import { MacrosMap } from "./types";

const CALLS_RE = /^( *)(\w+)\((.+)\);/gm;
const replaceMacros =
(macros: MacrosMap) => (_, indent: string, id: string, args: string) => {
return macros[id](...args.split(/, +/))
(macros: MacrosMap, verbose: boolean) =>
(_, indent: string, id: string, args: string) => {
const selectedMacros = macros[id];
if (!selectedMacros) return _;
const replacement = selectedMacros(...args.split(/, +/))
.split("\n")
.filter((str) => str.match(/\S/))
.map((str) => indent + str.trim())
.join("\n");
if (verbose)
console.log(`\n${id}(${args}):\n====\n${replacement}\n=====\n`);
return replacement;
};

export const getCompiler = (macros: MacrosMap) => (src: string) =>
src.replace(CALLS_RE, replaceMacros(macros));
export const getCompiler =
(macros: MacrosMap, verbose: boolean) => (src: string) =>
src.replace(CALLS_RE, replaceMacros(macros, verbose));
7 changes: 5 additions & 2 deletions src/vite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import { transformWithEsbuild, Plugin } from "vite";
import { Options } from "./types";
import { getCompiler } from "./utils";

export default function inlineFunction({ macros = {} }: Options): Plugin {
const compile = getCompiler(macros);
export default function inlineFunction({
macros = {},
verbose = false,
}: Options): Plugin {
const compile = getCompiler(macros, verbose);
return {
name,
enforce: "pre",
Expand Down
183 changes: 163 additions & 20 deletions test/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,30 +1,80 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`plugins > esbuild 1`] = `
"(() => {
// test/utils.ts
var fn2 = (a, b) => a + b;
"// test/utils.ts
function add(u, v) {
u[0] = u[0] + v[0];
u[1] = u[1] + v[1];
}
var fn2 = (a, b) => a + b;
// test/test.ts
var foo = [4, 5];
var bar = [5, fn2(6, 12)];
var ZERO = () => [0, 0];
var baz = [33, 22];
var _a0 = baz;
var _b1 = ZERO();
_a0[0] = _a0[0] + _b1[0];
_a0[1] = _a0[1] + _b1[1];
console.log(foo, baz);
var _a2 = ZERO();
var _b3 = bar;
_a2[0] = _a2[0] + _b3[0];
_a2[1] = _a2[1] + _b3[1];
})();
// test/test.ts
var foo = [4, 5];
var bar = [5, fn2(6, 12)];
var ZERO = () => [0, 0];
var baz = [33, 22];
var _a0 = baz;
var _b1 = ZERO();
_a0[0] = _a0[0] + _b1[0];
_a0[1] = _a0[1] + _b1[1];
console.log(foo, baz);
var _a2 = ZERO();
var _b3 = bar;
_a2[0] = _a2[0] + _b3[0];
_a2[1] = _a2[1] + _b3[1];
var crossProduct = (u, v, dest) => u[0] * v[1] - u[1] * v[0];
var crossProduct2 = crossProduct;
var add2 = add;
function inlined() {
const foo2 = [4, 5];
const bar2 = [5, fn2(6, 12)];
const baz2 = [33, 22];
for (let i = 0; i < 100; i++) {
const _a4 = foo2;
const _b5 = bar2;
_a4[0] = _a4[0] + _b5[0];
_a4[1] = _a4[1] + _b5[1];
const _a6 = baz2;
const _b7 = bar2;
_a6[0] = _a6[0] + _b7[0];
_a6[1] = _a6[1] + _b7[1];
const _a8 = foo2;
const _b9 = baz2;
_a8[0] = _a8[0] + _b9[0];
_a8[1] = _a8[1] + _b9[1];
const _a10 = foo2;
const _b11 = bar2;
const _d12 = baz2;
_d12[0] = _a10[0] * _b11[0];
_d12[1] = _a10[1] * _b11[1];
}
return foo2;
}
function notInlined() {
const foo2 = [4, 5];
const bar2 = [5, fn2(6, 12)];
const baz2 = [33, 22];
for (let i = 0; i < 100; i++) {
add2(foo2, bar2);
add2(baz2, bar2);
add2(foo2, baz2);
crossProduct2(foo2, bar2, baz2);
}
return foo2;
}
export {
inlined,
notInlined
};
"
`;

exports[`plugins > rollup 1`] = `
"'use strict';
"function add(u, v) {
u[0] = u[0] + v[0];
u[1] = u[1] + v[1];
}
var fn2 = function (a, b) { return a + b; };
var foo = [4, 5];
var ZERO = function () { return [0, 0]; };
Expand All @@ -37,17 +87,110 @@ _a0[0] = _a0[0] + _b1[0];
_a0[1] = _a0[1] + _b1[1];
// so that dead code removal doesn't remove the function altogether
console.log(foo, baz);
var crossProduct = function (u, v, dest) {
return u[0] * v[1] - u[1] * v[0];
};
var crossProduct2 = crossProduct;
var add2 = add;
function inlined() {
var foo = [4, 5];
var bar = [5, fn2(6, 12)];
var baz = [33, 22];
for (var i = 0; i < 100; i++) {
const _a4 = foo;
const _b5 = bar;
_a4[0] = _a4[0] + _b5[0];
_a4[1] = _a4[1] + _b5[1];
const _a6 = baz;
const _b7 = bar;
_a6[0] = _a6[0] + _b7[0];
_a6[1] = _a6[1] + _b7[1];
const _a8 = foo;
const _b9 = baz;
_a8[0] = _a8[0] + _b9[0];
_a8[1] = _a8[1] + _b9[1];
const _a10 = foo;
const _b11 = bar;
const _d12 = baz;
_d12[0] = _a10[0] * _b11[0];
_d12[1] = _a10[1] * _b11[1];
}
return foo;
}
function notInlined() {
var foo = [4, 5];
var bar = [5, fn2(6, 12)];
var baz = [33, 22];
for (var i = 0; i < 100; i++) {
add2(foo, bar);
add2(baz, bar);
add2(foo, baz);
crossProduct2(foo, bar);
}
return foo;
}
export { inlined, notInlined };
"
`;

exports[`plugins > vite 1`] = `
"const foo = [4, 5];
"function add(u, v) {
u[0] = u[0] + v[0];
u[1] = u[1] + v[1];
}
const fn2 = (a, b) => a + b;
const foo = [4, 5];
const ZERO = () => [0, 0];
const baz = [33, 22];
const _a0 = baz;
const _b1 = ZERO();
_a0[0] = _a0[0] + _b1[0];
_a0[1] = _a0[1] + _b1[1];
console.log(foo, baz);
const crossProduct = (u, v, dest) => u[0] * v[1] - u[1] * v[0];
const crossProduct2 = crossProduct;
const add2 = add;
function inlined() {
const foo2 = [4, 5];
const bar2 = [5, fn2(6, 12)];
const baz2 = [33, 22];
for (let i = 0; i < 100; i++) {
const _a4 = foo2;
const _b5 = bar2;
_a4[0] = _a4[0] + _b5[0];
_a4[1] = _a4[1] + _b5[1];
const _a6 = baz2;
const _b7 = bar2;
_a6[0] = _a6[0] + _b7[0];
_a6[1] = _a6[1] + _b7[1];
const _a8 = foo2;
const _b9 = baz2;
_a8[0] = _a8[0] + _b9[0];
_a8[1] = _a8[1] + _b9[1];
const _a10 = foo2;
const _b11 = bar2;
const _d12 = baz2;
_d12[0] = _a10[0] * _b11[0];
_d12[1] = _a10[1] * _b11[1];
}
return foo2;
}
function notInlined() {
const foo2 = [4, 5];
const bar2 = [5, fn2(6, 12)];
const baz2 = [33, 22];
for (let i = 0; i < 100; i++) {
add2(foo2, bar2);
add2(baz2, bar2);
add2(foo2, baz2);
crossProduct2(foo2, bar2);
}
return foo2;
}
export {
inlined,
notInlined
};
"
`;
10 changes: 10 additions & 0 deletions test/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import bench from "benny";
import { inlined, notInlined } from "./dist/esbuild.js";

bench.suite(
"My suite",
bench.add("Inlined", inlined),
bench.add("Not inlined", notInlined),
bench.cycle(),
bench.complete()
);
18 changes: 3 additions & 15 deletions test/esbuild.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
import esbuild from "esbuild";
import inlineFunction from "../dist/esbuild.js";

let counter = 0;
const macros = {
add: (a, b) => {
const _a = `_a${counter++}`;
const _b = `_b${counter++}`;
return `
const ${_a} = ${a};
const ${_b} = ${b};
${_a}[0] = ${_a}[0] + ${_b}[0];
${_a}[1] = ${_a}[1] + ${_b}[1];
`;
},
};
import { macros } from "./macros.js";

esbuild
.build({
entryPoints: ["./test/test.ts"],
bundle: true,
format: "esm",
outfile: "./test/dist/esbuild.js",
plugins: [inlineFunction({ macros })],
plugins: [inlineFunction({ macros, verbose: false })],
})
.catch(() => process.exit(1));
25 changes: 25 additions & 0 deletions test/macros.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
let counter = 0;
export const macros = {
add: (a, b) => {
const _a = `_a${counter++}`;
const _b = `_b${counter++}`;
return `
const ${_a} = ${a};
const ${_b} = ${b};
${_a}[0] = ${_a}[0] + ${_b}[0];
${_a}[1] = ${_a}[1] + ${_b}[1];
`;
},
crossProduct: (a, b, d) => {
const _a = `_a${counter++}`;
const _b = `_b${counter++}`;
const _d = `_d${counter++}`;
return `
const ${_a} = ${a};
const ${_b} = ${b};
const ${_d} = ${d};
${_d}[0] = ${_a}[0] * ${_b}[0];
${_d}[1] = ${_a}[1] * ${_b}[1];
`;
},
};
Loading

0 comments on commit 9e8a1ea

Please sign in to comment.