From 35136f2131051c36824663cc8c9af2925cbd3ed6 Mon Sep 17 00:00:00 2001 From: Thays Date: Wed, 22 Sep 2021 12:23:14 -0300 Subject: [PATCH 1/4] 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 e5c658ed3d1e81..14e51af4902f99 100644 --- a/src/mono/wasm/runtime/library_mono.js +++ b/src/mono/wasm/runtime/library_mono.js @@ -581,10 +581,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`); @@ -593,8 +607,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 48896fb72c7b6cf503afe8769c9c87dc4f460799 Mon Sep 17 00:00:00 2001 From: Thays Date: Wed, 22 Sep 2021 13:50:24 -0300 Subject: [PATCH 2/4] 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 14e51af4902f99..fb106a32f7afdf 100644 --- a/src/mono/wasm/runtime/library_mono.js +++ b/src/mono/wasm/runtime/library_mono.js @@ -587,7 +587,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 0a2a4e36cb7ef3ff9861160b822fd99e4677a995 Mon Sep 17 00:00:00 2001 From: Thays Date: Wed, 22 Sep 2021 14:03:39 -0300 Subject: [PATCH 3/4] 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 fb106a32f7afdf..4fa3e005042319 100644 --- a/src/mono/wasm/runtime/library_mono.js +++ b/src/mono/wasm/runtime/library_mono.js @@ -589,10 +589,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 6b4066a2fce314f8a0b5cc5b2f4e651d7a94063d Mon Sep 17 00:00:00 2001 From: Thays Date: Wed, 22 Sep 2021 14:24:20 -0300 Subject: [PATCH 4/4] 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 4fa3e005042319..cc3b9654a5a518 100644 --- a/src/mono/wasm/runtime/library_mono.js +++ b/src/mono/wasm/runtime/library_mono.js @@ -583,24 +583,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`); @@ -610,7 +607,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`); @@ -845,7 +842,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;