typenative 0.4.1 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +171 -0
- package/README.md +62 -22
- package/ROUND2-HANDOFF.md +200 -0
- package/bin/cli.js +1 -0
- package/bin/index.js +13 -2
- package/bin/main.js +1 -0
- package/bin/node/assert.js +2 -2
- package/bin/node/async_hooks.js +1 -1
- package/bin/node/buffer.js +16 -5
- package/bin/node/child_process.js +7 -7
- package/bin/node/cluster.js +3 -3
- package/bin/node/crypto_keys.js +21 -21
- package/bin/node/dgram.js +1 -1
- package/bin/node/diagnostics_channel.js +3 -3
- package/bin/node/dns.js +2 -2
- package/bin/node/events.js +3 -3
- package/bin/node/fetch.js +2 -2
- package/bin/node/fs.js +3 -3
- package/bin/node/fs_watch.js +5 -5
- package/bin/node/http.js +3 -3
- package/bin/node/http2.js +3 -3
- package/bin/node/net.js +8 -8
- package/bin/node/path.js +17 -3
- package/bin/node/process.js +34 -11
- package/bin/node/readline.js +1 -1
- package/bin/node/registry.js +4 -1
- package/bin/node/sqlite.js +6 -6
- package/bin/node/stream.js +81 -15
- package/bin/node/stream_web.js +10 -10
- package/bin/node/system_extra.js +3 -3
- package/bin/node/test.js +674 -366
- package/bin/node/test_coverage.js +187 -0
- package/bin/node/test_report.js +1392 -0
- package/bin/node/test_run.js +575 -0
- package/bin/node/timers.js +4 -4
- package/bin/node/tls.js +5 -5
- package/bin/node/util.js +31 -7
- package/bin/node/util_extra.js +7 -7
- package/bin/node/webview.js +654 -0
- package/bin/node/worker_threads.js +1 -1
- package/bin/node/zlib.js +3 -3
- package/bin/parse-node.js +4 -0
- package/bin/transpiler.js +10390 -1178
- package/bin/version.js +1 -1
- package/conformance.md +468 -340
- package/examples/README.md +47 -10
- package/examples/desktop-notes/README.md +108 -0
- package/examples/desktop-notes/bench.mjs +125 -0
- package/examples/desktop-notes/main.ts +131 -0
- package/examples/desktop-notes/package-lock.json +726 -0
- package/examples/desktop-notes/package.json +9 -0
- package/examples/desktop-notes/ui.ts +176 -0
- package/examples/dupe-finder/README.md +76 -0
- package/examples/dupe-finder/bench.mjs +120 -0
- package/examples/dupe-finder/main.ts +87 -0
- package/examples/mandelbrot-png/README.md +65 -0
- package/examples/mandelbrot-png/bench.mjs +95 -0
- package/examples/mandelbrot-png/main.ts +98 -0
- package/examples/mandelbrot-png/preview.png +0 -0
- package/examples/marked-cli/README.md +159 -0
- package/examples/marked-cli/bench.mjs +178 -0
- package/examples/marked-cli/main.ts +292 -0
- package/examples/marked-cli/parity.mjs +185 -0
- package/examples/marked-cli/vendor/marked/LICENSE +44 -0
- package/examples/marked-cli/vendor/marked/VENDOR.md +26 -0
- package/examples/todo-cli/README.md +69 -0
- package/examples/todo-cli/bench.mjs +89 -0
- package/examples/todo-cli/main.ts +92 -0
- package/examples/yaml-cli/README.md +136 -0
- package/examples/yaml-cli/bench.mjs +265 -0
- package/examples/yaml-cli/main.ts +29 -0
- package/examples/yaml-cli/parity.mjs +156 -0
- package/examples/yaml-cli/vendor/yaml/LICENSE +13 -0
- package/examples/yaml-cli/vendor/yaml/VENDOR.md +25 -0
- package/govendor/github.com/jchv/go-webview2/LICENSE +22 -0
- package/govendor/github.com/jchv/go-webview2/README.md +21 -0
- package/govendor/github.com/jchv/go-webview2/common.go +84 -0
- package/govendor/github.com/jchv/go-webview2/go.mod +3 -0
- package/govendor/github.com/jchv/go-webview2/internal/w32/w32.go +194 -0
- package/govendor/github.com/jchv/go-webview2/internal/w32/w32_386.go +14 -0
- package/govendor/github.com/jchv/go-webview2/internal/w32/w32_64bit.go +15 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_COLOR.go +8 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_HOST_RESOURCE_ACCESS_KIND.go +9 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_KEY_EVENT_KIND.go +10 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_MOVE_FOCUS_REASON.go +9 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_PHYSICAL_KEY_STATUS.go +10 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/COREWEBVIEW2_WEB_RESOURCE_CONTEXT.go +23 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2AcceleratorKeyPressedEventArgs.go +78 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2AcceleratorKeyPressedEventHandler.go +52 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2Controller.go +132 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2Controller2.go +74 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2CreateCoreWebView2ControllerCompletedHandler.go +48 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2NavigationCompletedEventArgs.go +17 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2NavigationCompletedEventHandler.go +48 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2Settings.go +270 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2WebResourceRequest.go +43 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2WebResourceRequestedEventArgs.go +51 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2WebResourceRequestedEventHandler.go +48 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2WebResourceResponse.go +21 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2_2.go +21 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebView2_3.go +60 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/ICoreWebViewSettings.go +396 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/chromium.go +361 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/chromium_386.go +24 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/chromium_amd64.go +22 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/chromium_arm64.go +26 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/comproc.go +13 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/comproc_go17.go +51 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/comproc_go18.go +16 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/corewebview2.go +411 -0
- package/govendor/github.com/jchv/go-webview2/pkg/edge/guid.go +223 -0
- package/govendor/github.com/jchv/go-webview2/webview.go +482 -0
- package/govendor/github.com/jchv/go-webview2/webviewloader/README.md +3 -0
- package/govendor/github.com/jchv/go-webview2/webviewloader/module.go +153 -0
- package/govendor/github.com/jchv/go-webview2/webviewloader/module_386.go +6 -0
- package/govendor/github.com/jchv/go-webview2/webviewloader/module_amd64.go +6 -0
- package/govendor/github.com/jchv/go-webview2/webviewloader/module_arm64.go +6 -0
- package/govendor/github.com/jchv/go-webview2/webviewloader/sdk/arm64/WebView2Loader.dll +0 -0
- package/govendor/github.com/jchv/go-webview2/webviewloader/sdk/x64/WebView2Loader.dll +0 -0
- package/govendor/github.com/jchv/go-webview2/webviewloader/sdk/x86/WebView2Loader.dll +0 -0
- package/govendor/github.com/jchv/go-winloader/LICENSE.md +7 -0
- package/govendor/github.com/jchv/go-winloader/README.md +124 -0
- package/govendor/github.com/jchv/go-winloader/common.go +11 -0
- package/govendor/github.com/jchv/go-winloader/go.mod +3 -0
- package/govendor/github.com/jchv/go-winloader/internal/loader/loader.go +25 -0
- package/govendor/github.com/jchv/go-winloader/internal/loader/machine.go +53 -0
- package/govendor/github.com/jchv/go-winloader/internal/memloader/cache.go +42 -0
- package/govendor/github.com/jchv/go-winloader/internal/memloader/loader.go +253 -0
- package/govendor/github.com/jchv/go-winloader/internal/pe/export.go +76 -0
- package/govendor/github.com/jchv/go-winloader/internal/pe/format.go +522 -0
- package/govendor/github.com/jchv/go-winloader/internal/pe/import.go +106 -0
- package/govendor/github.com/jchv/go-winloader/internal/pe/module.go +91 -0
- package/govendor/github.com/jchv/go-winloader/internal/pe/reloc.go +177 -0
- package/govendor/github.com/jchv/go-winloader/internal/pe/util.go +40 -0
- package/govendor/github.com/jchv/go-winloader/internal/vmem/common.go +55 -0
- package/govendor/github.com/jchv/go-winloader/internal/vmem/memory_other.go +71 -0
- package/govendor/github.com/jchv/go-winloader/internal/vmem/memory_windows.go +179 -0
- package/govendor/github.com/jchv/go-winloader/internal/winloader/hacks_other.go +17 -0
- package/govendor/github.com/jchv/go-winloader/internal/winloader/hacks_windows.go +73 -0
- package/govendor/github.com/jchv/go-winloader/internal/winloader/loader_windows.go +64 -0
- package/govendor/github.com/jchv/go-winloader/internal/winloader/machine_windows.go +32 -0
- package/govendor/github.com/jchv/go-winloader/internal/winloader/nativearch_386.go +9 -0
- package/govendor/github.com/jchv/go-winloader/internal/winloader/nativearch_amd64.go +9 -0
- package/govendor/github.com/jchv/go-winloader/internal/winloader/nativearch_arm64.go +9 -0
- package/govendor/github.com/jchv/go-winloader/loader_other.go +24 -0
- package/govendor/github.com/jchv/go-winloader/loader_windows.go +33 -0
- package/govendor/manifest.txt +3 -1
- package/package.json +1 -1
- package/tsparser/main.go +4 -0
- package/types/typenative-npm.d.ts +190 -2
- package/types/typenative.d.ts +98 -31
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// Renders the Mandelbrot set to a PNG file. The PNG encoder is written here
|
|
2
|
+
// by hand on top of node:zlib (deflate + CRC-32), so the program needs no
|
|
3
|
+
// image library under Node.js or TypeNative.
|
|
4
|
+
//
|
|
5
|
+
// node main.ts [width=1920] [height=1080] [out=mandelbrot.png]
|
|
6
|
+
// ./mandelbrot [width] [height] [out]
|
|
7
|
+
import fs from 'node:fs';
|
|
8
|
+
import zlib from 'node:zlib';
|
|
9
|
+
|
|
10
|
+
const width = Number(process.argv[2] ?? '1920');
|
|
11
|
+
const height = Number(process.argv[3] ?? '1080');
|
|
12
|
+
const out = process.argv[4] ?? 'mandelbrot.png';
|
|
13
|
+
const maxIter = 500;
|
|
14
|
+
|
|
15
|
+
// Smooth escape-time colouring: a cosine palette indexed by the
|
|
16
|
+
// continuous iteration count, black inside the set
|
|
17
|
+
function colorAt(cx: number, cy: number): [number, number, number] {
|
|
18
|
+
let x = 0;
|
|
19
|
+
let y = 0;
|
|
20
|
+
let i = 0;
|
|
21
|
+
while (x * x + y * y <= 256 && i < maxIter) {
|
|
22
|
+
const xt = x * x - y * y + cx;
|
|
23
|
+
y = 2 * x * y + cy;
|
|
24
|
+
x = xt;
|
|
25
|
+
i++;
|
|
26
|
+
}
|
|
27
|
+
if (i === maxIter) return [0, 0, 0];
|
|
28
|
+
const smooth = i + 1 - Math.log2(Math.log2(x * x + y * y) / 2);
|
|
29
|
+
const t = 0.55 + Math.sqrt(smooth) * 0.11;
|
|
30
|
+
const r = Math.round(255 * (0.5 + 0.5 * Math.cos(6.2832 * (t + 0.0))));
|
|
31
|
+
const g = Math.round(255 * (0.5 + 0.5 * Math.cos(6.2832 * (t + 0.1))));
|
|
32
|
+
const b = Math.round(255 * (0.5 + 0.5 * Math.cos(6.2832 * (t + 0.2))));
|
|
33
|
+
return [r, g, b];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Raw PNG scanlines: each row is a filter byte (0 = none) then RGB triples
|
|
37
|
+
function render(): Uint8Array {
|
|
38
|
+
const stride = width * 3 + 1;
|
|
39
|
+
const pixels = new Uint8Array(stride * height);
|
|
40
|
+
const scale = 3 / height;
|
|
41
|
+
for (let py = 0; py < height; py++) {
|
|
42
|
+
const cy = (py - height / 2) * scale;
|
|
43
|
+
let offset = py * stride + 1;
|
|
44
|
+
for (let px = 0; px < width; px++) {
|
|
45
|
+
const cx = (px - width * 0.65) * scale;
|
|
46
|
+
const [r, g, b] = colorAt(cx, cy);
|
|
47
|
+
pixels[offset] = r;
|
|
48
|
+
pixels[offset + 1] = g;
|
|
49
|
+
pixels[offset + 2] = b;
|
|
50
|
+
offset += 3;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return pixels;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// One PNG chunk: length, type, data, CRC-32 of type + data
|
|
57
|
+
function chunk(type: string, data: Uint8Array): Uint8Array {
|
|
58
|
+
const typed = new Uint8Array(4 + data.length);
|
|
59
|
+
for (let i = 0; i < 4; i++) typed[i] = type.charCodeAt(i);
|
|
60
|
+
typed.set(data, 4);
|
|
61
|
+
const result = new Uint8Array(12 + data.length);
|
|
62
|
+
const view = new DataView(result.buffer);
|
|
63
|
+
view.setUint32(0, data.length);
|
|
64
|
+
result.set(typed, 4);
|
|
65
|
+
view.setUint32(8 + data.length, zlib.crc32(typed));
|
|
66
|
+
return result;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function encodePng(pixels: Uint8Array): Uint8Array {
|
|
70
|
+
const header = new Uint8Array(13);
|
|
71
|
+
const view = new DataView(header.buffer);
|
|
72
|
+
view.setUint32(0, width);
|
|
73
|
+
view.setUint32(4, height);
|
|
74
|
+
header[8] = 8; // bit depth
|
|
75
|
+
header[9] = 2; // colour type: truecolour RGB
|
|
76
|
+
const parts = [
|
|
77
|
+
new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
|
|
78
|
+
chunk('IHDR', header),
|
|
79
|
+
chunk('IDAT', new Uint8Array(zlib.deflateSync(pixels))),
|
|
80
|
+
chunk('IEND', new Uint8Array(0))
|
|
81
|
+
];
|
|
82
|
+
const total = parts.reduce((sum, p) => sum + p.length, 0);
|
|
83
|
+
const png = new Uint8Array(total);
|
|
84
|
+
let offset = 0;
|
|
85
|
+
for (const part of parts) {
|
|
86
|
+
png.set(part, offset);
|
|
87
|
+
offset += part.length;
|
|
88
|
+
}
|
|
89
|
+
return png;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const t0 = performance.now();
|
|
93
|
+
const pixels = render();
|
|
94
|
+
const t1 = performance.now();
|
|
95
|
+
const png = encodePng(pixels);
|
|
96
|
+
fs.writeFileSync(out, png);
|
|
97
|
+
const t2 = performance.now();
|
|
98
|
+
console.log(`Rendered ${width}x${height} in ${Math.round(t1 - t0)} ms, encoded ${png.length} bytes in ${Math.round(t2 - t1)} ms -> ${out}`);
|
|
Binary file
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
# marked CLI
|
|
2
|
+
|
|
3
|
+
The [`marked`](https://github.com/markedjs/marked) package's own command-line
|
|
4
|
+
tool, compiled from its **unmodified** upstream TypeScript source: about 4,000
|
|
5
|
+
lines across 13 files, vendored as is in [vendor/marked/](vendor/marked/) (see
|
|
6
|
+
[VENDOR.md](vendor/marked/VENDOR.md): no patches). [main.ts](main.ts) ports
|
|
7
|
+
upstream's `bin/main.js` to TypeScript. It parses the same options, reads the
|
|
8
|
+
markdown from a string, a file or stdin, and prints the HTML or the lexer's
|
|
9
|
+
token list.
|
|
10
|
+
|
|
11
|
+
This is the second test of TypeNative on real code nobody wrote for it. marked
|
|
12
|
+
is a markdown parser built on regexes: 66 definitions in `rules.ts`, with GFM
|
|
13
|
+
and pedantic variants, many composed at run time by a fluent
|
|
14
|
+
`edit(…).replace(…)` builder (44 `edit` calls). Many use lookarounds or backreferences, which
|
|
15
|
+
Go's RE2 engine doesn't support, so they run on TypeNative's backtracking
|
|
16
|
+
engine. Tokens are structural object types, arrays of tokens are
|
|
17
|
+
filled in later through a queue that holds the same arrays, and the `--tokens`
|
|
18
|
+
JSON must match V8's key order and leave out `undefined` fields, such as the
|
|
19
|
+
title of a link definition that has none.
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
```sh
|
|
24
|
+
marked < in.md # render markdown to HTML
|
|
25
|
+
marked -s '*hi*' # render a string
|
|
26
|
+
marked -i in.md -o out.html # read a file, write a file
|
|
27
|
+
marked --tokens < in.md # print the token list as JSON
|
|
28
|
+
marked --no-gfm --pedantic # any boolean option of marked.defaults
|
|
29
|
+
marked --help # all options
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Differences from `bin/main.js` (listed in main.ts's header): `--help` prints an
|
|
33
|
+
option summary instead of opening the man page, `--version` prints the vendored
|
|
34
|
+
version, and a config file (`-c`, `~/.marked.json`) must be JSON. A JavaScript
|
|
35
|
+
config is a module Node would import and run, which a native binary can't. The
|
|
36
|
+
HTML is also flushed before the process exits, under Node too: `bin/main.js`
|
|
37
|
+
exits right after writing, which cuts output to a pipe off at 64 KB.
|
|
38
|
+
|
|
39
|
+
## Run it
|
|
40
|
+
|
|
41
|
+
With Node.js (24 or later runs the TypeScript directly):
|
|
42
|
+
|
|
43
|
+
```sh
|
|
44
|
+
node main.ts < README.md
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
With TypeNative:
|
|
48
|
+
|
|
49
|
+
```sh
|
|
50
|
+
npx typenative --source main.ts --output dist/marked
|
|
51
|
+
./dist/marked < README.md
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
From a checkout of this repository:
|
|
55
|
+
|
|
56
|
+
```sh
|
|
57
|
+
node bin/cli.js --source examples/marked-cli/main.ts --output dist/marked
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Parity with Node.js
|
|
61
|
+
|
|
62
|
+
Reproduce with `node parity.mjs [filter]`. The script fetches marked's own spec
|
|
63
|
+
suites (`test/specs` at tag `v18.1.2`) into `.cache/` (it isn't committed) and
|
|
64
|
+
compiles `main.ts`. It then feeds each case's markdown on stdin to both
|
|
65
|
+
runtimes in three modes: default (GFM), `--no-gfm` and `--tokens`. A case's
|
|
66
|
+
front matter (`gfm`, `pedantic`, `breaks`) becomes CLI flags, as marked's test
|
|
67
|
+
runner turns it into options. stdout, stderr and the exit code must match byte
|
|
68
|
+
for byte. The only normalization is on stderr: Node's ` at …` stack frames
|
|
69
|
+
are dropped (a native binary has no JS frames to print). Each run has a 10 s
|
|
70
|
+
timeout. Failures are written to `.cache/failures/`.
|
|
71
|
+
|
|
72
|
+
```
|
|
73
|
+
2859/2859 cases match (100.00%)
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
That's 953 inputs × 3 modes: CommonMark 0.31.2 (652 examples), GFM 0.29 (28),
|
|
77
|
+
marked's own `new` (223) and `original` (22) tests, and its 28 ReDoS
|
|
78
|
+
regression inputs. The ReDoS inputs are pathological on purpose, such as 10,000
|
|
79
|
+
underscores in a row. Every one of them finishes within the timeout natively
|
|
80
|
+
too (see the benchmark notes below for how long they take).
|
|
81
|
+
|
|
82
|
+
## Benchmark: Node.js vs TypeNative
|
|
83
|
+
|
|
84
|
+
Reproduce with `node bench.mjs [runs=30] [throughputRuns=5] [megabytes=10]`.
|
|
85
|
+
The script compiles `main.ts`, then times **complete process runs** (spawn to
|
|
86
|
+
exit) of `marked` under both runtimes, alternating Node and native runs, and
|
|
87
|
+
checks that the two HTML outputs are identical:
|
|
88
|
+
|
|
89
|
+
- **(a) per-call latency:** marked's own README.md (3.5 KB), the kind of file
|
|
90
|
+
a docs build or a git hook renders once per file.
|
|
91
|
+
- **(b) throughput:** 10 MB of generated markdown (headings, paragraphs with
|
|
92
|
+
emphasis, links and code spans, lists, tables, code fences and block quotes,
|
|
93
|
+
from a fixed-seed generator, so every machine gets the same input).
|
|
94
|
+
|
|
95
|
+
**Environment:** Linux 6.18 container, 4 vCPUs of an Intel Xeon @ 2.10GHz,
|
|
96
|
+
Node v24.22.1, Go 1.26.0, TypeNative 0.4.2 (unreleased changes), default
|
|
97
|
+
(non-`--release`) build. The binary is 6.3 MB.
|
|
98
|
+
|
|
99
|
+
(a) `marked < README.md` (3.5 KB), 30 runs each:
|
|
100
|
+
|
|
101
|
+
| Runtime | min | median | mean | max |
|
|
102
|
+
| ---------- | --------- | --------- | --------- | --------- |
|
|
103
|
+
| Node.js | 129.00 ms | 148.15 ms | 148.71 ms | 191.32 ms |
|
|
104
|
+
| TypeNative | 18.04 ms | 19.46 ms | 19.71 ms | 25.27 ms |
|
|
105
|
+
|
|
106
|
+
(b) `marked < generated.md` (10.0 MB, 114,893 lines), 5 runs each:
|
|
107
|
+
|
|
108
|
+
| Runtime | min | median | mean | max | MB/s (median) |
|
|
109
|
+
| ---------- | --------- | --------- | --------- | --------- | ------------- |
|
|
110
|
+
| Node.js | 1,969 ms | 2,053 ms | 2,056 ms | 2,164 ms | 4.87 |
|
|
111
|
+
| TypeNative | 10,378 ms | 10,573 ms | 10,685 ms | 11,006 ms | 0.95 |
|
|
112
|
+
|
|
113
|
+
**Per call, TypeNative is 7.6x faster** (19 ms vs 148 ms, median). Node's
|
|
114
|
+
cost here is mostly startup: it loads and type-strips the 13 TypeScript files
|
|
115
|
+
and compiles marked's regexes on every run. That's what you feel when a docs
|
|
116
|
+
build or a hook renders one file per process.
|
|
117
|
+
|
|
118
|
+
**On bulk throughput, TypeNative is 5.2x slower.** On a long input V8's JIT
|
|
119
|
+
and its regex engine (Irregexp compiles hot patterns to machine code) run
|
|
120
|
+
marked faster than the compiled Go does. In a profile of the native run on
|
|
121
|
+
1 MB, lexing takes half the CPU and TypeNative's backtracking regex engine
|
|
122
|
+
about a third of it: marked's patterns use lookarounds and backreferences, so
|
|
123
|
+
most of them can't run on Go's RE2, and the backtracker interprets them node
|
|
124
|
+
by node. Garbage collection takes about a third more on the other cores (each
|
|
125
|
+
token is an object, each match a few strings). Both costs grow linearly with
|
|
126
|
+
the input, while Node's head start is fixed, so the native binary finishes
|
|
127
|
+
first up to about 150–200 KB of markdown per process:
|
|
128
|
+
|
|
129
|
+
| Input | Node.js | TypeNative |
|
|
130
|
+
| ------ | ------- | ---------- |
|
|
131
|
+
| 16 KB | 153 ms | 37 ms |
|
|
132
|
+
| 64 KB | 167 ms | 96 ms |
|
|
133
|
+
| 128 KB | 182 ms | 149 ms |
|
|
134
|
+
| 256 KB | 215 ms | 287 ms |
|
|
135
|
+
| 1 MB | 377 ms | 1,051 ms |
|
|
136
|
+
|
|
137
|
+
(Medians of 5 full process runs each, same generator.)
|
|
138
|
+
|
|
139
|
+
The ReDoS inputs show the same split. The 28 of them take 3.4 s natively in
|
|
140
|
+
total and 4.1 s under Node, but the worst one, 10,000 underscores
|
|
141
|
+
(`quadratic_emstrong_delim`), takes 1.4 s natively against Node's 0.25 s. Each
|
|
142
|
+
underscore restarts a pattern that scans the rest of the run, so the work is
|
|
143
|
+
quadratic in both engines and the backtracker's constant factor shows.
|
|
144
|
+
|
|
145
|
+
Getting here fixed performance bugs in TypeNative's runtime and compiler, all
|
|
146
|
+
general:
|
|
147
|
+
|
|
148
|
+
- The backtracking engine converted the whole input to runes on every match
|
|
149
|
+
attempt, making a lexer that matches at each position quadratic. It now
|
|
150
|
+
works on byte offsets.
|
|
151
|
+
- A greedy repeat of one character class no longer allocates, and ASCII is
|
|
152
|
+
tested against a table, which took the worst ReDoS input from 6.3 s to 1.4 s.
|
|
153
|
+
- A regex whose match must start with certain bytes skips ahead to them, and
|
|
154
|
+
an anchored one fails at once, instead of RE2 scanning the rest of a long
|
|
155
|
+
input on every call.
|
|
156
|
+
- Strings built with `+=` in a loop are `strings.Builder`s, so rendering a
|
|
157
|
+
long document is linear.
|
|
158
|
+
- Type inference is memoized per query. marked's
|
|
159
|
+
`edit(re).replace(…).replace(…)…` chains took exponential time to compile.
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
// Benchmark: Node.js vs TypeNative for the marked CLI, both rendering
|
|
2
|
+
// markdown to HTML (`marked < in.md`).
|
|
3
|
+
//
|
|
4
|
+
// Compiles main.ts to a native binary with the TypeNative CLI from this repo
|
|
5
|
+
// checkout (../../bin/cli.js), then measures:
|
|
6
|
+
// (a) per-call latency: complete process runs (spawn to exit) on marked's
|
|
7
|
+
// own README.md, the kind of file a docs build or a hook renders once;
|
|
8
|
+
// (b) throughput: one run on about 10 MB of generated markdown (headings,
|
|
9
|
+
// paragraphs with emphasis, links and code spans, lists, tables, code
|
|
10
|
+
// fences and block quotes).
|
|
11
|
+
// Runs alternate Node and native, so drift on the machine hits both alike.
|
|
12
|
+
// Both runtimes' HTML must be identical, or the benchmark stops.
|
|
13
|
+
//
|
|
14
|
+
// Usage: node bench.mjs [runs=30] [throughputRuns=5] [megabytes=10]
|
|
15
|
+
|
|
16
|
+
import { spawnSync } from 'node:child_process';
|
|
17
|
+
import fs from 'node:fs';
|
|
18
|
+
import path from 'node:path';
|
|
19
|
+
import { fileURLToPath } from 'node:url';
|
|
20
|
+
|
|
21
|
+
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
22
|
+
const repoRoot = path.resolve(here, '..', '..');
|
|
23
|
+
const cli = path.join(repoRoot, 'bin', 'cli.js');
|
|
24
|
+
const cache = path.join(here, '.cache');
|
|
25
|
+
const upstream = path.join(cache, 'marked-src');
|
|
26
|
+
const exePath = path.join(cache, process.platform === 'win32' ? 'marked.exe' : 'marked');
|
|
27
|
+
const runs = Number(process.argv[2] ?? '30') || 30;
|
|
28
|
+
const throughputRuns = Number(process.argv[3] ?? '5') || 5;
|
|
29
|
+
const megabytes = Number(process.argv[4] ?? '10') || 10;
|
|
30
|
+
|
|
31
|
+
// marked's README.md at the vendored tag (parity.mjs fetches the same checkout)
|
|
32
|
+
function readme() {
|
|
33
|
+
const file = path.join(upstream, 'README.md');
|
|
34
|
+
if (!fs.existsSync(file)) {
|
|
35
|
+
fs.mkdirSync(cache, { recursive: true });
|
|
36
|
+
console.log('Fetching marked v18.1.2 ...');
|
|
37
|
+
const clone = spawnSync('git', ['clone', '--quiet', '--depth', '1', '--branch', 'v18.1.2', '--filter=blob:none', '--sparse', 'https://github.com/markedjs/marked.git', upstream], { stdio: 'inherit' });
|
|
38
|
+
if (clone.status !== 0) process.exit(1);
|
|
39
|
+
spawnSync('git', ['-C', upstream, 'sparse-checkout', 'set', 'test/specs'], { stdio: 'inherit' });
|
|
40
|
+
}
|
|
41
|
+
return fs.readFileSync(file);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Deterministic pseudo-random numbers (a fixed-seed LCG): the same document
|
|
45
|
+
// on every run and every machine
|
|
46
|
+
let seed = 42;
|
|
47
|
+
function rand(n) {
|
|
48
|
+
seed = (seed * 1103515245 + 12345) % 2147483648;
|
|
49
|
+
return Math.floor(seed / 65536) % n;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const WORDS = ['markdown', 'parser', 'native', 'binary', 'token', 'lexer', 'render', 'inline', 'block', 'heading',
|
|
53
|
+
'paragraph', 'emphasis', 'table', 'fence', 'quote', 'list', 'link', 'image', 'escape', 'entity', 'spec', 'html'];
|
|
54
|
+
|
|
55
|
+
function word() {
|
|
56
|
+
return WORDS[rand(WORDS.length)];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function sentence() {
|
|
60
|
+
const parts = [];
|
|
61
|
+
const n = 6 + rand(14);
|
|
62
|
+
for (let i = 0; i < n; i++) {
|
|
63
|
+
const w = word();
|
|
64
|
+
switch (rand(12)) {
|
|
65
|
+
case 0: parts.push(`*${w}*`); break;
|
|
66
|
+
case 1: parts.push(`**${w}**`); break;
|
|
67
|
+
case 2: parts.push(`\`${w}()\``); break;
|
|
68
|
+
case 3: parts.push(`[${w}](https://example.com/${w}/${rand(1000)} "${w}")`); break;
|
|
69
|
+
case 4: parts.push(`_${w}_`); break;
|
|
70
|
+
default: parts.push(w);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return parts.join(' ') + '.';
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function section(i) {
|
|
77
|
+
const lines = [`${'#'.repeat(1 + rand(3))} ${word()} ${word()} ${i}`, ''];
|
|
78
|
+
for (let p = 0; p < 2 + rand(3); p++) lines.push(`${sentence()} ${sentence()}\n${sentence()}`, '');
|
|
79
|
+
switch (rand(5)) {
|
|
80
|
+
case 0:
|
|
81
|
+
for (let k = 0; k < 3 + rand(5); k++) lines.push(`- ${sentence()}`);
|
|
82
|
+
lines.push('');
|
|
83
|
+
break;
|
|
84
|
+
case 1:
|
|
85
|
+
for (let k = 1; k <= 3 + rand(4); k++) lines.push(`${k}. ${sentence()}`);
|
|
86
|
+
lines.push('');
|
|
87
|
+
break;
|
|
88
|
+
case 2:
|
|
89
|
+
lines.push(`| ${word()} | ${word()} | ${word()} |`, '| --- | :---: | ---: |');
|
|
90
|
+
for (let k = 0; k < 3 + rand(6); k++) lines.push(`| ${word()} | ${rand(1000)} | *${word()}* |`);
|
|
91
|
+
lines.push('');
|
|
92
|
+
break;
|
|
93
|
+
case 3:
|
|
94
|
+
lines.push('```js', `function ${word()}(${word()}) {`, ` return ${rand(100)} < 3 && "${word()}";`, '}', '```', '');
|
|
95
|
+
break;
|
|
96
|
+
default:
|
|
97
|
+
lines.push(`> ${sentence()}`, `> ${sentence()}`, '');
|
|
98
|
+
}
|
|
99
|
+
return lines.join('\n');
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function document(bytes) {
|
|
103
|
+
const out = [];
|
|
104
|
+
let size = 0;
|
|
105
|
+
for (let i = 0; size < bytes; i++) {
|
|
106
|
+
const s = section(i);
|
|
107
|
+
out.push(s);
|
|
108
|
+
size += s.length + 1;
|
|
109
|
+
}
|
|
110
|
+
return out.join('\n');
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function stats(samples) {
|
|
114
|
+
const sorted = [...samples].sort((a, b) => a - b);
|
|
115
|
+
const mean = samples.reduce((sum, x) => sum + x, 0) / samples.length;
|
|
116
|
+
return { min: sorted[0], median: sorted[Math.floor(sorted.length / 2)], mean, max: sorted[sorted.length - 1] };
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function fmt(ms) {
|
|
120
|
+
return `${ms.toFixed(2)} ms`.padStart(13);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function runOnce(command, args, input) {
|
|
124
|
+
const t0 = performance.now();
|
|
125
|
+
const result = spawnSync(command, args, { cwd: here, input, maxBuffer: 1 << 30 });
|
|
126
|
+
const elapsed = performance.now() - t0;
|
|
127
|
+
if (result.status !== 0) {
|
|
128
|
+
console.error(`run failed: ${command} ${args.join(' ')}\n${result.stderr}`);
|
|
129
|
+
process.exit(1);
|
|
130
|
+
}
|
|
131
|
+
return { elapsed, stdout: result.stdout };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Alternating Node and native runs; the two outputs must match
|
|
135
|
+
function timeBoth(input, count) {
|
|
136
|
+
const node = [];
|
|
137
|
+
const native = [];
|
|
138
|
+
for (let i = 0; i < count; i++) {
|
|
139
|
+
const a = runOnce(process.execPath, ['main.ts'], input);
|
|
140
|
+
const b = runOnce(exePath, [], input);
|
|
141
|
+
if (!a.stdout.equals(b.stdout)) {
|
|
142
|
+
console.error('Node and native HTML differ');
|
|
143
|
+
process.exit(1);
|
|
144
|
+
}
|
|
145
|
+
node.push(a.elapsed);
|
|
146
|
+
native.push(b.elapsed);
|
|
147
|
+
}
|
|
148
|
+
return [stats(node), stats(native)];
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function table(nodeStats, nativeStats, extra = () => '') {
|
|
152
|
+
console.log(`| Runtime | min | median | mean | max |${extra('head')}`);
|
|
153
|
+
console.log(`|------------|--------------|--------------|--------------|--------------|${extra('rule')}`);
|
|
154
|
+
console.log(`| Node.js |${fmt(nodeStats.min)} |${fmt(nodeStats.median)} |${fmt(nodeStats.mean)} |${fmt(nodeStats.max)} |${extra(nodeStats)}`);
|
|
155
|
+
console.log(`| TypeNative |${fmt(nativeStats.min)} |${fmt(nativeStats.median)} |${fmt(nativeStats.mean)} |${fmt(nativeStats.max)} |${extra(nativeStats)}`);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// 1. Compile the CLI to a native binary.
|
|
159
|
+
fs.mkdirSync(cache, { recursive: true });
|
|
160
|
+
const small = readme();
|
|
161
|
+
console.log('Compiling main.ts to a native binary with TypeNative...');
|
|
162
|
+
const build = spawnSync(process.execPath, [cli, '--source', 'main.ts', '--output', exePath], { cwd: here, stdio: 'inherit' });
|
|
163
|
+
if (build.status !== 0) process.exit(build.status ?? 1);
|
|
164
|
+
|
|
165
|
+
// 2. Per-call latency on marked's README.md.
|
|
166
|
+
console.log(`\n(a) \`marked < README.md\` (${small.length} bytes), ${runs} runs each (full process: spawn -> exit):\n`);
|
|
167
|
+
const [nodeCall, nativeCall] = timeBoth(small, runs);
|
|
168
|
+
table(nodeCall, nativeCall);
|
|
169
|
+
console.log(`\nTypeNative is ${(nodeCall.median / nativeCall.median).toFixed(1)}x faster per call (median).`);
|
|
170
|
+
|
|
171
|
+
// 3. Throughput on generated markdown.
|
|
172
|
+
const big = Buffer.from(document(megabytes * 1024 * 1024));
|
|
173
|
+
const mb = big.length / (1024 * 1024);
|
|
174
|
+
console.log(`\n(b) \`marked < generated.md\` (${mb.toFixed(1)} MB, ${big.toString().split('\n').length} lines), ${throughputRuns} runs each:\n`);
|
|
175
|
+
const [nodeBig, nativeBig] = timeBoth(big, throughputRuns);
|
|
176
|
+
table(nodeBig, nativeBig, (s) => (s === 'head' ? ' MB/s (median) |' : s === 'rule' ? '---------------|' : ` ${(mb / (s.median / 1000)).toFixed(2).padStart(13)} |`));
|
|
177
|
+
const ratio = nodeBig.median / nativeBig.median;
|
|
178
|
+
console.log(`\nTypeNative is ${ratio >= 1 ? `${ratio.toFixed(1)}x faster` : `${(1 / ratio).toFixed(1)}x slower`} on throughput (median).`);
|