From 0206acc83834d4b4d274dcc77a360d5bfaf1b50a Mon Sep 17 00:00:00 2001 From: Thays Date: Wed, 22 Sep 2021 12:23:14 -0300 Subject: [PATCH 1/8] Reusing buffer to avoid allocate every debugger message. --- src/mono/wasm/runtime/library_mono.js | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/mono/wasm/runtime/library_mono.js b/src/mono/wasm/runtime/library_mono.js index 2f9fe8a5e19c52..ab4ea7e7c15b97 100644 --- a/src/mono/wasm/runtime/library_mono.js +++ b/src/mono/wasm/runtime/library_mono.js @@ -599,10 +599,24 @@ var MonoSupportLib = { MONO.commands_received = buffer_obj; }, + mono_wasm_malloc_and_set_debug_buffer: function (command_parameters) + { + if (!this.debugger_buffer || command_parameters.length > this.debugger_buffer_len) + { + if (this.debugger_buffer) + Module._free (this.debugger_buffer); + var length = command_parameters.length > this.debugger_buffer_len ? command_parameters.length : 256; + this.debugger_buffer = Module._malloc (length); + this.heap_bytes = new Uint8Array (Module.HEAPU8.buffer, this.debugger_buffer, length); + this.debugger_buffer_len = length; + } + this.heap_bytes.set(this._base64_to_uint8 (command_parameters)); + }, + mono_wasm_send_dbg_command_with_parms: function (id, command_set, command, command_parameters, length, valtype, newvalue) { - var dataHeap = this.mono_wasm_load_bytes_into_heap (this._base64_to_uint8 (command_parameters)); - this._c_fn_table.mono_wasm_send_dbg_command_with_parms_wrapper (id, command_set, command, dataHeap, length, valtype, newvalue.toString()); + this.mono_wasm_malloc_and_set_debug_buffer(command_parameters); + this._c_fn_table.mono_wasm_send_dbg_command_with_parms_wrapper (id, command_set, command, this.debugger_buffer, length, valtype, newvalue.toString()); let { res_ok, res } = MONO.commands_received; if (!res_ok) throw new Error (`Failed on mono_wasm_invoke_method_debugger_agent_with_parms`); @@ -611,8 +625,8 @@ var MonoSupportLib = { mono_wasm_send_dbg_command: function (id, command_set, command, command_parameters) { - var dataHeap = this.mono_wasm_load_bytes_into_heap (this._base64_to_uint8 (command_parameters)); - this._c_fn_table.mono_wasm_send_dbg_command_wrapper (id, command_set, command, dataHeap, command_parameters.length); + this.mono_wasm_malloc_and_set_debug_buffer(command_parameters); + this._c_fn_table.mono_wasm_send_dbg_command_wrapper (id, command_set, command, this.debugger_buffer, command_parameters.length); let { res_ok, res } = MONO.commands_received; if (!res_ok) throw new Error (`Failed on mono_wasm_send_dbg_command`); From 3bbdba6f4227f91039dabecebc2f839f1cf4108b Mon Sep 17 00:00:00 2001 From: Thays Date: Wed, 22 Sep 2021 13:50:24 -0300 Subject: [PATCH 2/8] Fixing what was suggested by @radical. --- src/mono/wasm/runtime/library_mono.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/mono/wasm/runtime/library_mono.js b/src/mono/wasm/runtime/library_mono.js index ab4ea7e7c15b97..7c68fbd7d61474 100644 --- a/src/mono/wasm/runtime/library_mono.js +++ b/src/mono/wasm/runtime/library_mono.js @@ -605,7 +605,9 @@ var MonoSupportLib = { { if (this.debugger_buffer) Module._free (this.debugger_buffer); - var length = command_parameters.length > this.debugger_buffer_len ? command_parameters.length : 256; + else + this.debugger_buffer_len = 256; + var length = command_parameters.length > this.debugger_buffer_len ? command_parameters.length : this.debugger_buffer_len; this.debugger_buffer = Module._malloc (length); this.heap_bytes = new Uint8Array (Module.HEAPU8.buffer, this.debugger_buffer, length); this.debugger_buffer_len = length; From b262298d6f6fd1eac2c29c2dd47bdc70fe40cc0c Mon Sep 17 00:00:00 2001 From: Thays Date: Wed, 22 Sep 2021 14:03:39 -0300 Subject: [PATCH 3/8] cleaning code. --- src/mono/wasm/runtime/library_mono.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/mono/wasm/runtime/library_mono.js b/src/mono/wasm/runtime/library_mono.js index 7c68fbd7d61474..df6b4c76ab95df 100644 --- a/src/mono/wasm/runtime/library_mono.js +++ b/src/mono/wasm/runtime/library_mono.js @@ -607,10 +607,10 @@ var MonoSupportLib = { Module._free (this.debugger_buffer); else this.debugger_buffer_len = 256; - var length = command_parameters.length > this.debugger_buffer_len ? command_parameters.length : this.debugger_buffer_len; - this.debugger_buffer = Module._malloc (length); - this.heap_bytes = new Uint8Array (Module.HEAPU8.buffer, this.debugger_buffer, length); - this.debugger_buffer_len = length; + if (this.debugger_buffer_len < command_parameters.length) + this.debugger_buffer_len = command_parameters.length; + this.debugger_buffer = Module._malloc (this.debugger_buffer_len); + this.heap_bytes = new Uint8Array (Module.HEAPU8.buffer, this.debugger_buffer, this.debugger_buffer_len); } this.heap_bytes.set(this._base64_to_uint8 (command_parameters)); }, From 8fd17bb47f84246a674f8cc2721ac1ac7ca3f6fb Mon Sep 17 00:00:00 2001 From: Thays Date: Wed, 22 Sep 2021 14:24:20 -0300 Subject: [PATCH 4/8] Changing what was suggested by @radical --- src/mono/wasm/runtime/library_mono.js | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/mono/wasm/runtime/library_mono.js b/src/mono/wasm/runtime/library_mono.js index df6b4c76ab95df..acab365e27a860 100644 --- a/src/mono/wasm/runtime/library_mono.js +++ b/src/mono/wasm/runtime/library_mono.js @@ -601,24 +601,21 @@ var MonoSupportLib = { mono_wasm_malloc_and_set_debug_buffer: function (command_parameters) { - if (!this.debugger_buffer || command_parameters.length > this.debugger_buffer_len) + if (command_parameters.length > this._debugger_buffer_len) { - if (this.debugger_buffer) - Module._free (this.debugger_buffer); - else - this.debugger_buffer_len = 256; - if (this.debugger_buffer_len < command_parameters.length) - this.debugger_buffer_len = command_parameters.length; - this.debugger_buffer = Module._malloc (this.debugger_buffer_len); - this.heap_bytes = new Uint8Array (Module.HEAPU8.buffer, this.debugger_buffer, this.debugger_buffer_len); + if (this._debugger_buffer) + Module._free (this._debugger_buffer); + this._debugger_buffer_len = Math.max(command_parameters.length, this._debugger_buffer_len, 256); + this._debugger_buffer = Module._malloc (this._debugger_buffer_len); + this._debugger_heap_bytes = new Uint8Array (Module.HEAPU8.buffer, this._debugger_buffer, this._debugger_buffer_len); } - this.heap_bytes.set(this._base64_to_uint8 (command_parameters)); + this._debugger_heap_bytes.set(this._base64_to_uint8 (command_parameters)); }, mono_wasm_send_dbg_command_with_parms: function (id, command_set, command, command_parameters, length, valtype, newvalue) { this.mono_wasm_malloc_and_set_debug_buffer(command_parameters); - this._c_fn_table.mono_wasm_send_dbg_command_with_parms_wrapper (id, command_set, command, this.debugger_buffer, length, valtype, newvalue.toString()); + this._c_fn_table.mono_wasm_send_dbg_command_with_parms_wrapper (id, command_set, command, this._debugger_buffer, length, valtype, newvalue.toString()); let { res_ok, res } = MONO.commands_received; if (!res_ok) throw new Error (`Failed on mono_wasm_invoke_method_debugger_agent_with_parms`); @@ -628,7 +625,7 @@ var MonoSupportLib = { mono_wasm_send_dbg_command: function (id, command_set, command, command_parameters) { this.mono_wasm_malloc_and_set_debug_buffer(command_parameters); - this._c_fn_table.mono_wasm_send_dbg_command_wrapper (id, command_set, command, this.debugger_buffer, command_parameters.length); + this._c_fn_table.mono_wasm_send_dbg_command_wrapper (id, command_set, command, this._debugger_buffer, command_parameters.length); let { res_ok, res } = MONO.commands_received; if (!res_ok) throw new Error (`Failed on mono_wasm_send_dbg_command`); @@ -863,7 +860,7 @@ var MonoSupportLib = { this._c_fn_table = {}; this._register_c_fn ('mono_wasm_send_dbg_command', 'bool', [ 'number', 'number', 'number', 'number', 'number' ]); this._register_c_fn ('mono_wasm_send_dbg_command_with_parms', 'bool', [ 'number', 'number', 'number', 'number', 'number', 'number', 'string' ]); - + this._debugger_buffer_len = -1; // DO NOT REMOVE - magic debugger init function if (globalThis.dotnetDebugger) debugger; From c356045c083738a3acf0e1a86d8f5afb743fbb63 Mon Sep 17 00:00:00 2001 From: Thays Date: Tue, 28 Sep 2021 11:36:57 -0300 Subject: [PATCH 5/8] Implementing again after merge --- src/mono/wasm/runtime/src/mono/debug.ts | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/mono/wasm/runtime/src/mono/debug.ts b/src/mono/wasm/runtime/src/mono/debug.ts index 9f097b8933cfa8..0efae3c86ee2fe 100644 --- a/src/mono/wasm/runtime/src/mono/debug.ts +++ b/src/mono/wasm/runtime/src/mono/debug.ts @@ -9,6 +9,9 @@ import { mono_wasm_load_bytes_into_heap } from './init' var commands_received: any; var _call_function_res_cache: any = {} var _next_call_function_res_id = 0; +var _debugger_buffer_len = -1; +var _debugger_buffer: any; +var _debugger_heap_bytes : any; export function mono_wasm_runtime_ready() { MONO.mono_wasm_runtime_is_ready = true; @@ -16,6 +19,7 @@ export function mono_wasm_runtime_ready() { // FIXME: where should this go? _next_call_function_res_id = 0; _call_function_res_cache = {}; + _debugger_buffer_len = -1; // DO NOT REMOVE - magic debugger init function if ((globalThis).dotnetDebugger) @@ -41,10 +45,20 @@ export function mono_wasm_add_dbg_command_received(res_ok: boolean, id: number, } commands_received = buffer_obj; } +export function mono_wasm_malloc_and_set_debug_buffer(command_parameters: string) { + if (command_parameters.length > _debugger_buffer_len) { + if (_debugger_buffer) + Module._free (_debugger_buffer); + _debugger_buffer_len = Math.max(command_parameters.length, _debugger_buffer_len, 256); + _debugger_buffer = Module._malloc (_debugger_buffer_len); + _debugger_heap_bytes = new Uint8Array (Module.HEAPU8.buffer, _debugger_buffer, _debugger_buffer_len); + } + _debugger_heap_bytes.set(_base64_to_uint8 (command_parameters)); +} export function mono_wasm_send_dbg_command_with_parms(id: number, command_set: number, command: number, command_parameters: any, length: number, valtype: number, newvalue: number) { - const dataHeap = mono_wasm_load_bytes_into_heap(_base64_to_uint8(command_parameters)); - cwraps.mono_wasm_send_dbg_command_with_parms(id, command_set, command, dataHeap, length, valtype, newvalue.toString()); + mono_wasm_malloc_and_set_debug_buffer(command_parameters); + cwraps.mono_wasm_send_dbg_command_with_parms(id, command_set, command, _debugger_buffer, length, valtype, newvalue.toString()); const { res_ok, res } = commands_received; if (!res_ok) @@ -53,8 +67,8 @@ export function mono_wasm_send_dbg_command_with_parms(id: number, command_set: n } export function mono_wasm_send_dbg_command(id: number, command_set: number, command: number, command_parameters: any) { - var dataHeap = mono_wasm_load_bytes_into_heap(_base64_to_uint8(command_parameters)); - cwraps.mono_wasm_send_dbg_command(id, command_set, command, dataHeap, command_parameters.length); + mono_wasm_malloc_and_set_debug_buffer(command_parameters); + cwraps.mono_wasm_send_dbg_command(id, command_set, command, _debugger_buffer, command_parameters.length); const { res_ok, res } = commands_received; if (!res_ok) From 425cebb221b68b0fc9af5b43a16c91abdb23ca50 Mon Sep 17 00:00:00 2001 From: Thays Date: Tue, 28 Sep 2021 12:58:24 -0300 Subject: [PATCH 6/8] Fix spacing before parentheses. --- src/mono/wasm/runtime/src/mono/debug.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/mono/wasm/runtime/src/mono/debug.ts b/src/mono/wasm/runtime/src/mono/debug.ts index 0efae3c86ee2fe..ac6e8e4e051e56 100644 --- a/src/mono/wasm/runtime/src/mono/debug.ts +++ b/src/mono/wasm/runtime/src/mono/debug.ts @@ -48,12 +48,12 @@ export function mono_wasm_add_dbg_command_received(res_ok: boolean, id: number, export function mono_wasm_malloc_and_set_debug_buffer(command_parameters: string) { if (command_parameters.length > _debugger_buffer_len) { if (_debugger_buffer) - Module._free (_debugger_buffer); + Module._free(_debugger_buffer); _debugger_buffer_len = Math.max(command_parameters.length, _debugger_buffer_len, 256); - _debugger_buffer = Module._malloc (_debugger_buffer_len); - _debugger_heap_bytes = new Uint8Array (Module.HEAPU8.buffer, _debugger_buffer, _debugger_buffer_len); + _debugger_buffer = Module._malloc(_debugger_buffer_len); + _debugger_heap_bytes = new Uint8Array(Module.HEAPU8.buffer, _debugger_buffer, _debugger_buffer_len); } - _debugger_heap_bytes.set(_base64_to_uint8 (command_parameters)); + _debugger_heap_bytes.set(_base64_to_uint8(command_parameters)); } export function mono_wasm_send_dbg_command_with_parms(id: number, command_set: number, command: number, command_parameters: any, length: number, valtype: number, newvalue: number) { @@ -173,8 +173,7 @@ export function mono_wasm_call_function_on(request: CallRequest) { const fn_args = request.arguments != undefined ? request.arguments.map(a => JSON.stringify(a.value)) : []; const fn_eval_str = `var fn = ${request.functionDeclaration}; fn.call (proxy, ...[${fn_args}]);`; - const local_eval = eval; // https://rollupjs.org/guide/en/#avoiding-eval - const fn_res = local_eval(fn_eval_str); + const fn_res = eval(fn_eval_str); if (fn_res === undefined) return { type: "undefined" }; From 5f652fc2ba43286fa9e9f337d0b884e615d1bd9b Mon Sep 17 00:00:00 2001 From: Thays Date: Tue, 28 Sep 2021 14:40:57 -0300 Subject: [PATCH 7/8] Addressing @pavelsavara comments. --- src/mono/wasm/runtime/src/mono/debug.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mono/wasm/runtime/src/mono/debug.ts b/src/mono/wasm/runtime/src/mono/debug.ts index ac6e8e4e051e56..f14b304b398a9d 100644 --- a/src/mono/wasm/runtime/src/mono/debug.ts +++ b/src/mono/wasm/runtime/src/mono/debug.ts @@ -11,7 +11,7 @@ var _call_function_res_cache: any = {} var _next_call_function_res_id = 0; var _debugger_buffer_len = -1; var _debugger_buffer: any; -var _debugger_heap_bytes : any; +var _debugger_heap_bytes : Uint8Array; export function mono_wasm_runtime_ready() { MONO.mono_wasm_runtime_is_ready = true; @@ -45,7 +45,7 @@ export function mono_wasm_add_dbg_command_received(res_ok: boolean, id: number, } commands_received = buffer_obj; } -export function mono_wasm_malloc_and_set_debug_buffer(command_parameters: string) { +function mono_wasm_malloc_and_set_debug_buffer(command_parameters: string) { if (command_parameters.length > _debugger_buffer_len) { if (_debugger_buffer) Module._free(_debugger_buffer); From 85d891f700f2c09369a8fcbcdd59e96bcd9eb7aa Mon Sep 17 00:00:00 2001 From: Thays Date: Tue, 28 Sep 2021 14:44:22 -0300 Subject: [PATCH 8/8] Fix _debugger_buffer type. --- src/mono/wasm/runtime/src/mono/debug.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mono/wasm/runtime/src/mono/debug.ts b/src/mono/wasm/runtime/src/mono/debug.ts index f14b304b398a9d..bcd1146924d947 100644 --- a/src/mono/wasm/runtime/src/mono/debug.ts +++ b/src/mono/wasm/runtime/src/mono/debug.ts @@ -10,7 +10,7 @@ var commands_received: any; var _call_function_res_cache: any = {} var _next_call_function_res_id = 0; var _debugger_buffer_len = -1; -var _debugger_buffer: any; +var _debugger_buffer: number; var _debugger_heap_bytes : Uint8Array; export function mono_wasm_runtime_ready() {