Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions src/lib/libdylink.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ var LibraryDylink = {
'__stop_em_asm',
'__start_em_js',
'__stop_em_js',
'__rodata_end',
'__rodata_start',
].includes(symName) || symName.startsWith('__em_js__')
#if SPLIT_MODULE
// Exports synthesized by wasm-split should be prefixed with '%'
Expand Down
41 changes: 37 additions & 4 deletions src/lib/libstrings.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ addToLibrary({
#endif // TEXTDECODER == 2
},

#if SHRINK_LEVEL == 0
$UTF8ToString_cache__internal: true,
$UTF8ToString_cache: {},
#endif

$UTF8ToString__docs: `
/**
* Given a pointer 'ptr' to a null-terminated UTF8-encoded string in the
Expand All @@ -106,25 +111,53 @@ addToLibrary({
* @param {boolean=} ignoreNul - If true, the function will not stop on a NUL character.
* @return {string}
*/`,
$UTF8ToString__deps: [
#if SHRINK_LEVEL == 0
'$UTF8ToString_cache',
// These deps don't currently work due to https://github.com/llvm/llvm-project/issues/180632
//'__rodata_end',
//'__rodata_start',
#endif
#if TEXTDECODER == 2
$UTF8ToString__deps: ['$UTF8Decoder', '$findStringEnd'],
'$UTF8Decoder', '$findStringEnd',
#else
$UTF8ToString__deps: ['$UTF8ArrayToString'],
'$UTF8ArrayToString',
#endif
],
$UTF8ToString: (ptr, maxBytesToRead, ignoreNul) => {
#if ASSERTIONS
assert(typeof ptr == 'number', `UTF8ToString expects a number (got ${typeof ptr})`);
#endif
#if CAN_ADDRESS_2GB
ptr >>>= 0;
#endif

#if SHRINK_LEVEL == 0
var useCache = !maxBytesToRead && ptr >= ___rodata_start && ptr < ___rodata_end;
if (useCache) {
var rtn = UTF8ToString_cache[ptr];
if (rtn) {
return rtn;
}
}
#endif

#if TEXTDECODER == 2
if (!ptr) return '';
var end = findStringEnd(HEAPU8, ptr, maxBytesToRead, ignoreNul);
return UTF8Decoder.decode({{{ getUnsharedTextDecoderView('HEAPU8', 'ptr', 'end') }}});
var rtn = UTF8Decoder.decode({{{ getUnsharedTextDecoderView('HEAPU8', 'ptr', 'end') }}});
#else
return ptr ? UTF8ArrayToString(HEAPU8, ptr, maxBytesToRead, ignoreNul) : '';
var rtn = ptr ? UTF8ArrayToString(HEAPU8, ptr, maxBytesToRead, ignoreNul) : '';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it might be better to place this cache inside UTF8ArrayToString()? That way it would apply a bit wider.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we can do this in UTF8ArrayToString because the array bring used might not be the wasm memory.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, maybe it would be overly complex.. all entry points do flow through UTF8ToString already, so sgtm.

#endif

#if SHRINK_LEVEL == 0
if (useCache) {
UTF8ToString_cache[ptr] = rtn;
}
#endif

return rtn;

},

/**
Expand Down
10 changes: 6 additions & 4 deletions test/codesize/test_codesize_cxx_ctors1.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 19555,
"a.out.js.gz": 8102,
"a.out.nodebug.wasm": 132828,
"a.out.nodebug.wasm.gz": 49874,
"total": 152383,
"total_gz": 57976,
"a.out.nodebug.wasm": 132873,
"a.out.nodebug.wasm.gz": 49908,
"total": 152428,
"total_gz": 58010,
"sent": [
"__cxa_throw",
"_abort_js",
Expand All @@ -31,6 +31,8 @@
],
"exports": [
"__indirect_function_table",
"__rodata_end",
"__rodata_start",
"__wasm_call_ctors",
"_emscripten_stack_alloc",
"_emscripten_stack_restore",
Expand Down
10 changes: 6 additions & 4 deletions test/codesize/test_codesize_cxx_ctors2.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 19532,
"a.out.js.gz": 8087,
"a.out.nodebug.wasm": 132248,
"a.out.nodebug.wasm.gz": 49531,
"total": 151780,
"total_gz": 57618,
"a.out.nodebug.wasm": 132293,
"a.out.nodebug.wasm.gz": 49567,
"total": 151825,
"total_gz": 57654,
"sent": [
"__cxa_throw",
"_abort_js",
Expand All @@ -28,6 +28,8 @@
],
"exports": [
"__indirect_function_table",
"__rodata_end",
"__rodata_start",
"_emscripten_stack_alloc",
"_emscripten_stack_restore",
"emscripten_stack_get_current",
Expand Down
10 changes: 6 additions & 4 deletions test/codesize/test_codesize_cxx_except.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 23216,
"a.out.js.gz": 9081,
"a.out.nodebug.wasm": 172758,
"a.out.nodebug.wasm.gz": 57395,
"total": 195974,
"total_gz": 66476,
"a.out.nodebug.wasm": 172803,
"a.out.nodebug.wasm.gz": 57433,
"total": 196019,
"total_gz": 66514,
"sent": [
"__cxa_begin_catch",
"__cxa_end_catch",
Expand Down Expand Up @@ -91,6 +91,8 @@
"__cxa_get_exception_ptr",
"__cxa_increment_exception_refcount",
"__indirect_function_table",
"__rodata_end",
"__rodata_start",
"__wasm_call_ctors",
"_emscripten_stack_alloc",
"_emscripten_stack_restore",
Expand Down
10 changes: 6 additions & 4 deletions test/codesize/test_codesize_cxx_except_wasm.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 19366,
"a.out.js.gz": 8022,
"a.out.nodebug.wasm": 148153,
"a.out.nodebug.wasm.gz": 55275,
"total": 167519,
"total_gz": 63297,
"a.out.nodebug.wasm": 148199,
"a.out.nodebug.wasm.gz": 55309,
"total": 167565,
"total_gz": 63331,
"sent": [
"_abort_js",
"_tzset_js",
Expand All @@ -29,6 +29,8 @@
],
"exports": [
"__indirect_function_table",
"__rodata_end",
"__rodata_start",
"__trap",
"__wasm_call_ctors",
"_emscripten_stack_alloc",
Expand Down
10 changes: 6 additions & 4 deletions test/codesize/test_codesize_cxx_except_wasm_legacy.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 19440,
"a.out.js.gz": 8042,
"a.out.nodebug.wasm": 145959,
"a.out.nodebug.wasm.gz": 54906,
"total": 165399,
"total_gz": 62948,
"a.out.nodebug.wasm": 146005,
"a.out.nodebug.wasm.gz": 54943,
"total": 165445,
"total_gz": 62985,
"sent": [
"_abort_js",
"_tzset_js",
Expand All @@ -29,6 +29,8 @@
],
"exports": [
"__indirect_function_table",
"__rodata_end",
"__rodata_start",
"__trap",
"__wasm_call_ctors",
"_emscripten_stack_alloc",
Expand Down
10 changes: 6 additions & 4 deletions test/codesize/test_codesize_cxx_mangle.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 23266,
"a.out.js.gz": 9101,
"a.out.nodebug.wasm": 239192,
"a.out.nodebug.wasm.gz": 79781,
"total": 262458,
"total_gz": 88882,
"a.out.nodebug.wasm": 239237,
"a.out.nodebug.wasm.gz": 79811,
"total": 262503,
"total_gz": 88912,
"sent": [
"__cxa_begin_catch",
"__cxa_end_catch",
Expand Down Expand Up @@ -92,6 +92,8 @@
"__cxa_get_exception_ptr",
"__cxa_increment_exception_refcount",
"__indirect_function_table",
"__rodata_end",
"__rodata_start",
"__wasm_call_ctors",
"_emscripten_stack_alloc",
"_emscripten_stack_restore",
Expand Down
10 changes: 6 additions & 4 deletions test/codesize/test_codesize_cxx_noexcept.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 19555,
"a.out.js.gz": 8102,
"a.out.nodebug.wasm": 134835,
"a.out.nodebug.wasm.gz": 50701,
"total": 154390,
"total_gz": 58803,
"a.out.nodebug.wasm": 134880,
"a.out.nodebug.wasm.gz": 50737,
"total": 154435,
"total_gz": 58839,
"sent": [
"__cxa_throw",
"_abort_js",
Expand All @@ -31,6 +31,8 @@
],
"exports": [
"__indirect_function_table",
"__rodata_end",
"__rodata_start",
"__wasm_call_ctors",
"_emscripten_stack_alloc",
"_emscripten_stack_restore",
Expand Down
14 changes: 8 additions & 6 deletions test/codesize/test_codesize_cxx_wasmfs.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 7053,
"a.out.js.gz": 3325,
"a.out.nodebug.wasm": 172904,
"a.out.nodebug.wasm.gz": 63288,
"total": 179957,
"total_gz": 66613,
"a.out.js": 7198,
"a.out.js.gz": 3394,
"a.out.nodebug.wasm": 172949,
"a.out.nodebug.wasm.gz": 63318,
"total": 180147,
"total_gz": 66712,
"sent": [
"__cxa_throw",
"_abort_js",
Expand Down Expand Up @@ -49,6 +49,8 @@
],
"exports": [
"__indirect_function_table",
"__rodata_end",
"__rodata_start",
"__wasm_call_ctors",
"_emscripten_stack_alloc",
"_emscripten_stack_restore",
Expand Down
21 changes: 19 additions & 2 deletions test/codesize/test_codesize_file_preload.expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -2982,6 +2982,8 @@ var FS = {
}
};

var UTF8ToString_cache = {};

/**
* Given a pointer 'ptr' to a null-terminated UTF8-encoded string in the
* emscripten HEAP, returns a copy of that string as a Javascript String object.
Expand All @@ -2994,7 +2996,20 @@ var FS = {
* string will cut short at that byte index.
* @param {boolean=} ignoreNul - If true, the function will not stop on a NUL character.
* @return {string}
*/ var UTF8ToString = (ptr, maxBytesToRead, ignoreNul) => ptr ? UTF8ArrayToString(HEAPU8, ptr, maxBytesToRead, ignoreNul) : "";
*/ var UTF8ToString = (ptr, maxBytesToRead, ignoreNul) => {
var useCache = !maxBytesToRead && ptr >= ___rodata_start && ptr < ___rodata_end;
if (useCache) {
var rtn = UTF8ToString_cache[ptr];
if (rtn) {
return rtn;
}
}
var rtn = ptr ? UTF8ArrayToString(HEAPU8, ptr, maxBytesToRead, ignoreNul) : "";
if (useCache) {
UTF8ToString_cache[ptr] = rtn;
}
return rtn;
};

var SYSCALLS = {
calculateAt(dirfd, path, allowEmpty) {
Expand Down Expand Up @@ -3155,12 +3170,14 @@ Module["FS_createLazyFile"] = FS_createLazyFile;
// End JS library exports
// end include: postlibrary.js
// Imports from the Wasm binary.
var _main, memory, __indirect_function_table, wasmMemory;
var _main, memory, __indirect_function_table, ___rodata_start, ___rodata_end, wasmMemory;

function assignWasmExports(wasmExports) {
_main = Module["_main"] = wasmExports["d"];
memory = wasmMemory = wasmExports["b"];
__indirect_function_table = wasmExports["__indirect_function_table"];
___rodata_start = wasmExports["e"].value;
___rodata_end = wasmExports["f"].value;
}

var wasmImports = {
Expand Down
12 changes: 7 additions & 5 deletions test/codesize/test_codesize_file_preload.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 22509,
"a.out.js.gz": 9332,
"a.out.nodebug.wasm": 1681,
"a.out.nodebug.wasm.gz": 960,
"total": 24190,
"total_gz": 10292,
"a.out.nodebug.wasm": 1701,
"a.out.nodebug.wasm.gz": 973,
"total": 24210,
"total_gz": 10305,
"sent": [
"a (fd_write)"
],
Expand All @@ -14,7 +14,9 @@
"exports": [
"b (memory)",
"c (__wasm_call_ctors)",
"d (main)"
"d (main)",
"e (__rodata_start)",
"f (__rodata_end)"
],
"funcs": [
"$__emscripten_stdout_close",
Expand Down
16 changes: 9 additions & 7 deletions test/codesize/test_codesize_files_js_fs.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.out.js": 18174,
"a.out.js.gz": 7430,
"a.out.nodebug.wasm": 381,
"a.out.nodebug.wasm.gz": 260,
"total": 18555,
"total_gz": 7690,
"a.out.js": 18289,
"a.out.js.gz": 7483,
"a.out.nodebug.wasm": 401,
"a.out.nodebug.wasm.gz": 271,
"total": 18690,
"total_gz": 7754,
"sent": [
"a (fd_write)",
"b (fd_read)",
Expand All @@ -20,7 +20,9 @@
"exports": [
"e (memory)",
"f (__wasm_call_ctors)",
"g (main)"
"g (main)",
"h (__rodata_start)",
"i (__rodata_end)"
],
"funcs": [
"$__wasm_call_ctors",
Expand Down
Loading