Skip to content

Commit

Permalink
feat: move opcode counters to uint64 (#4)
Browse files Browse the repository at this point in the history
* feat: move the counter to 64bit

* fix test

* increase mocha timeout

* fix prettier
  • Loading branch information
leanmendoza authored Dec 12, 2022
1 parent ea3d5d7 commit 1808aa1
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 17 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
"tsc": "node_modules/.bin/tsc",
"build": "make dist",
"doc": "typedoc",
"test": "TS_NODE_TYPE_CHECK=false mocha",
"test-dist": "cd dist && TS_NODE_TYPE_CHECK=false mocha --require source-map-support/register *.test.js",
"test-fast": "TS_NODE_TYPE_CHECK=false TEST_NO_ASYNC=true mocha",
"test": "TS_NODE_TYPE_CHECK=false mocha --timeout 15000",
"test-dist": "cd dist && TS_NODE_TYPE_CHECK=false mocha --require source-map-support/register *.test.js --timeout 15000",
"test-fast": "TS_NODE_TYPE_CHECK=false TEST_NO_ASYNC=true mocha --timeout 15000",
"test-all": "TEST_LEAK=1 yarn test && yarn test-dist",
"prettier": "prettier --write .",
"prettier-check": "prettier --check .",
Expand Down
9 changes: 3 additions & 6 deletions quickjs/quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1258,17 +1258,14 @@ static const JSClassExoticMethods js_module_ns_exotic_methods;
static JSClassID js_class_id_alloc = JS_CLASS_INIT_COUNT;


uint32_t opcode_counter[256];
uint64_t opcode_counter[256];

void js_reset_opcode_counter() {
// TODO: move to memset
for (int i = 0; i < 256; i++) {
opcode_counter[i] = 0;
}
memset(opcode_counter, 0, 256 * 8);
}


uint32_t* js_get_opcode_counter() {
uint64_t* js_get_opcode_counter() {
return opcode_counter;
}

Expand Down
2 changes: 1 addition & 1 deletion quickjs/quickjs.h
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ void JS_AddIntrinsicOperators(JSContext *ctx);
void JS_EnableBignumExt(JSContext *ctx, JS_BOOL enable);

void js_reset_opcode_counter();
uint32_t* js_get_opcode_counter();
uint64_t* js_get_opcode_counter();


JSValue js_string_codePointRange(JSContext *ctx, JSValueConst this_val,
Expand Down
Binary file modified ts/generated/emscripten-module.WASM_DEBUG_ASYNCIFY.wasm
Binary file not shown.

Large diffs are not rendered by default.

Binary file modified ts/generated/emscripten-module.WASM_DEBUG_SYNC.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion ts/generated/emscripten-module.WASM_RELEASE_ASYNCIFY.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion ts/generated/emscripten-module.WASM_RELEASE_SYNC.js

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions ts/module-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,9 @@ export class TestQuickJSWASMModule implements Pick<QuickJSWASMModule, keyof Quic
getFFI() {
return this.parent.getFFI()
}

/** @private */
getOpcodeInfo() {
return this.parent.getOpcodeInfo()
}
}
15 changes: 15 additions & 0 deletions ts/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
RuntimeOptions,
RuntimeOptionsBase,
} from "./types"
import { QuickJSOpcodeInfo } from "./opcodeinfo"

type EmscriptenCallback<BaseArgs extends any[], Result> = (
...args: [Asyncify | undefined, ...BaseArgs]
Expand Down Expand Up @@ -322,12 +323,15 @@ export class QuickJSWASMModule {
protected callbacks: QuickJSModuleCallbacks
/** @private */
protected module: EitherModule
/** @private */
protected opcodeInfo: QuickJSOpcodeInfo

/** @private */
constructor(module: EitherModule, ffi: EitherFFI) {
this.module = module
this.ffi = ffi
this.callbacks = new QuickJSModuleCallbacks(module)
this.opcodeInfo = new QuickJSOpcodeInfo(module, ffi)
}

/**
Expand Down Expand Up @@ -429,4 +433,15 @@ export class QuickJSWASMModule {
getFFI(): EitherFFI {
return this.ffi
}

/**
* Get a low-level interface to the QuickJS functions in this WebAssembly
* module.
* @experimental
* @unstable No warranty is provided with this API. It could change at any time.
* @private
*/
getOpcodeInfo(): QuickJSOpcodeInfo {
return this.opcodeInfo
}
}
24 changes: 24 additions & 0 deletions ts/opcodeinfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { EitherModule } from "./emscripten-types"
import { EitherFFI } from "./types"

export class QuickJSOpcodeInfo {
private module: EitherModule
private ffi: EitherFFI

constructor(module: EitherModule, ffi: EitherFFI) {
this.module = module
this.ffi = ffi
}

public resetOpcodeCounters() {
this.ffi.QTS_ResetOpcodeCounter()
}

public getOpcodesCount() {
const opcodesArrayPtr = this.ffi.QTS_GetOpcodeCounter()
const values = new BigInt64Array(this.module.HEAP8.buffer, opcodesArrayPtr, 256)
const mappedArray = Array.from(values).map((count, opcode) => ({ count, opcode }))

return mappedArray.filter((item) => item.count > 0)
}
}
4 changes: 0 additions & 4 deletions ts/variants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,6 @@ export async function newQuickJSWASMModule(
wasmModule.type = "sync"
const ffi = new QuickJSFFI(wasmModule)

// call once to compile it
ffi.QTS_ResetOpcodeCounter()
ffi.QTS_GetOpcodeCounter()

return new QuickJSWASMModule(wasmModule, ffi)
}

Expand Down

0 comments on commit 1808aa1

Please sign in to comment.