Skip to content

Commit 5197559

Browse files
author
Guy Bedford
authored
feat: performance builtin support (#150)
1 parent 8995b35 commit 5197559

File tree

7 files changed

+64
-2
lines changed

7 files changed

+64
-2
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ cmake_minimum_required(VERSION 3.27)
33
include("StarlingMonkey/cmake/add_as_subproject.cmake")
44

55
add_builtin(componentize::embedding SRC embedding/embedding.cpp)
6+
include_directories("StarlingMonkey")
67

78
project(ComponentizeJS)

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ lib/starlingmonkey_embedding.wasm: StarlingMonkey/cmake/* embedding/* StarlingMo
2727
@cp build-release/starling-raw.wasm/starling-raw.wasm $@
2828

2929
lib/starlingmonkey_embedding_weval.wasm: StarlingMonkey/cmake/* embedding/* StarlingMonkey/runtime/* StarlingMonkey/builtins/* StarlingMonkey/builtins/*/* StarlingMonkey/builtins/*/*/* StarlingMonkey/include/* | lib
30-
cmake -B build-release-weval -DCMAKE_BUILD_TYPE=Release -DWEVAL=ON
30+
cmake -B build-release-weval -DCMAKE_BUILD_TYPE=Release -DUSE_WASM_OPT=OFF -DWEVAL=ON
3131
make -j16 -C build-release-weval
3232
@cp build-release-weval/starling-raw.wasm/starling-raw.wasm $@
3333

embedding/embedding.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "embedding.h"
2+
#include "builtins/web/performance.h"
23

34
namespace builtins::web::console {
45

@@ -65,7 +66,8 @@ from_bigint64(JS::MutableHandleValue handle) {
6566
JS::BigInt *arg0 = handle.toBigInt();
6667
uint64_t arg0_uint64;
6768
if (!JS::detail::BigIntIsUint64(arg0, &arg0_uint64)) {
68-
Runtime.engine->abort("Internal bindgen error in coreabi_from_bigint64 validation");
69+
Runtime.engine->abort(
70+
"Internal bindgen error in coreabi_from_bigint64 validation");
6971
}
7072
return arg0_uint64;
7173
}
@@ -148,6 +150,10 @@ __attribute__((export_name("call"))) uint32_t call(uint32_t fn_idx,
148150
if (Runtime.first_call) {
149151
js::ResetMathRandomSeed(Runtime.cx);
150152
Runtime.first_call = false;
153+
if (Runtime.clocks) {
154+
builtins::web::performance::Performance::timeOrigin.emplace(
155+
std::chrono::high_resolution_clock::now());
156+
}
151157
}
152158
if (Runtime.cur_fn_idx != -1) {
153159
LOG("(call) unexpected call state, post_call was not called after last "
@@ -350,6 +356,11 @@ componentize_initialize() {
350356
Runtime.debug = true;
351357
}
352358

359+
uint32_t feature_clocks = atoi(getenv("FEATURE_CLOCKS"));
360+
if (feature_clocks) {
361+
Runtime.clocks = true;
362+
}
363+
353364
__wizer_initialize();
354365
char env_name[100];
355366
LOG("(wizer) retrieve and generate the export bindings");

embedding/embedding.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ namespace componentize::embedding
4646
bool debug = false;
4747
bool first_call = true;
4848

49+
bool clocks = false;
50+
4951
JSContext *cx;
5052

5153
InitError init_err = InitError::OK;

src/componentize.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ export async function componentize(jsSource, witWorld, opts) {
173173
SOURCE_NAME: sourceName,
174174
IMPORT_WRAPPER_CNT: Object.keys(importWrappers).length.toString(),
175175
EXPORT_CNT: exports.length.toString(),
176+
FEATURE_CLOCKS: features.includes('clocks') ? '1' : '',
176177
};
177178

178179
for (const [idx, [export_name, expt]] of exports.entries()) {

test/builtins/performance-disabled.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { ok, strictEqual } from 'node:assert';
2+
3+
export const source = `
4+
export function run () {
5+
performance.now();
6+
};
7+
`;
8+
9+
export const disableFeatures = ['clocks'];
10+
11+
export async function test (run) {
12+
try {
13+
const { stdout, stderr } = await run();
14+
ok(false);
15+
} catch {
16+
// performance builtin just panics
17+
// (yes we can and should do better...)
18+
}
19+
}

test/builtins/performance.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { ok, strictEqual } from 'node:assert';
2+
3+
export const source = `
4+
export function run () {
5+
const start = performance.now();
6+
let previous = 0, cur = 1;
7+
for (let i = 1; i < 1000; i++) {
8+
const tmp = cur;
9+
cur = previous + cur;
10+
previous = tmp;
11+
}
12+
const end = performance.now();
13+
console.log('Calculated fib 1000: ' + cur);
14+
console.error((end - start) + ' ms');
15+
};
16+
`;
17+
18+
export async function test (run) {
19+
const { stdout, stderr } = await run();
20+
strictEqual(stdout, 'Calculated fib 1000: 4.346655768693743e+208\n');
21+
22+
ok(stderr.includes(' ms'));
23+
const time = Number(stderr.split(' ms')[0]);
24+
// TODO: fix back to half a millisecond when Weval fix is added
25+
if (time > 3) {
26+
throw new Error('took more than half a millisecond - ' + time + ' ms');
27+
}
28+
}

0 commit comments

Comments
 (0)