Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Emval implementation to avoid JS heap allocations. #21205

Merged
merged 19 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 41 additions & 28 deletions src/embind/emval.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,42 @@
/*jslint sub:true*/ /* The symbols 'fromWireType' and 'toWireType' must be accessed via array notation to be closure-safe since craftInvokerFunction crafts functions as strings that can't be closured. */

// -- jshint doesn't understand library syntax, so we need to mark the symbols exposed here
/*global getStringOrSymbol, emval_handles, Emval, __emval_unregister, count_emval_handles, emval_symbols, __emval_decref*/
/*global getStringOrSymbol, emval_freelist, emval_handles, Emval, __emval_unregister, count_emval_handles, emval_symbols, __emval_decref*/
/*global emval_addMethodCaller, emval_methodCallers, addToLibrary, global, emval_lookupTypes, makeLegalFunctionName*/
/*global emval_get_global*/

// Number of handles reserved for non-use (0) or common values w/o refcount.
{{{
globalThis.EMVAL_RESERVED_HANDLES = 5;
null;
}}}
var LibraryEmVal = {
$emval_handles__deps: ['$HandleAllocator'],
$emval_handles: "new HandleAllocator();",
// Stack of handles available for reuse.
$emval_freelist: [],
// Array of alternating pairs (value, refcount).
$emval_handles: [],
$emval_symbols: {}, // address -> string

$init_emval__deps: ['$count_emval_handles', '$emval_handles'],
$init_emval__postset: 'init_emval();',
$init_emval: () => {
// reserve some special values. These never get de-allocated.
// The HandleAllocator takes care of reserving zero.
emval_handles.allocated.push(
{value: undefined},
{value: null},
{value: true},
{value: false},
// reserve 0 and some special values. These never get de-allocated.
emval_handles.push(
0, 1,
undefined, 1,
null, 1,
true, 1,
false, 1,
);
Object.assign(emval_handles, /** @lends {emval_handles} */ { reserved: emval_handles.allocated.length }),
#if ASSERTIONS
assert(emval_handles.length / 2 === {{{ EMVAL_RESERVED_HANDLES }}});
mrolig5267319 marked this conversation as resolved.
Show resolved Hide resolved
#endif
Module['count_emval_handles'] = count_emval_handles;
},

$count_emval_handles__deps: ['$emval_handles'],
$count_emval_handles__deps: ['$emval_freelist', '$emval_handles'],
$count_emval_handles: () => {
var count = 0;
for (var i = emval_handles.reserved; i < emval_handles.allocated.length; ++i) {
if (emval_handles.allocated[i] !== undefined) {
++count;
}
}
return count;
return emval_handles.length / 2 - {{{ EMVAL_RESERVED_HANDLES }}}- emval_freelist.length;
},

_emval_register_symbol__deps: ['$emval_symbols', '$readLatin1String'],
Expand All @@ -61,13 +64,17 @@ var LibraryEmVal = {
return symbol;
},

$Emval__deps: ['$emval_handles', '$throwBindingError', '$init_emval'],
$Emval__deps: ['$emval_freelist', '$emval_handles', '$throwBindingError', '$init_emval'],
$Emval: {
toValue: (handle) => {
if (!handle) {
throwBindingError('Cannot use deleted val. handle = ' + handle);
}
return emval_handles.get(handle).value;
#if ASSERTIONS
// handle 1 is supposed to be `undefined`.
assert(handle === 1 || emval_handles[handle * 2] !== undefined, `invalid handle: ${handle}`);
mrolig5267319 marked this conversation as resolved.
Show resolved Hide resolved
#endif
return emval_handles[handle * 2];
},

toHandle: (value) => {
Expand All @@ -77,23 +84,29 @@ var LibraryEmVal = {
case true: return 3;
case false: return 4;
default:{
return emval_handles.allocate({refcount: 1, value: value});
const handle = emval_freelist.pop() || emval_handles.length / 2;
emval_handles[handle * 2] = value;
emval_handles[handle * 2 + 1] = 1;
return handle;
}
}
}
},

_emval_incref__deps: ['$emval_handles'],
_emval_incref: (handle) => {
if (handle > 4) {
emval_handles.get(handle).refcount += 1;
}
emval_handles[handle * 2 + 1] += 1;
},

_emval_decref__deps: ['$emval_handles'],
_emval_decref__deps: ['$emval_freelist', '$emval_handles'],
_emval_decref: (handle) => {
if (handle >= emval_handles.reserved && 0 === --emval_handles.get(handle).refcount) {
emval_handles.free(handle);
if (0 === --emval_handles[handle * 2 + 1]) {
#if ASSERTIONS
assert(handle >= {{{ EMVAL_RESERVED_HANDLES }}});
assert(emval_handles[handle * 2] !== undefined);
#endif
emval_handles[handle * 2] = undefined;
emval_freelist.push(handle);
}
},

Expand Down
12 changes: 6 additions & 6 deletions test/code_size/embind_val_wasm.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.html": 673,
"a.html.gz": 431,
"a.js": 7387,
"a.js.gz": 3112,
"a.wasm": 11433,
"a.wasm.gz": 5725,
"total": 19493,
"total_gz": 9268
"a.js": 7178,
"a.js.gz": 3052,
"a.wasm": 11439,
"a.wasm.gz": 5729,
"total": 19290,
"total_gz": 9212
}