2022-02-28 乐帮网
c#
我有一个 blazor wasm 应用程序。在调用一个接收双精度数组的 javascript 函数。这非常慢,尤其是当数组很大时。
请看下面的代码
javascript “test.js”
function testSumArray(array) {
var t0 = performance.now();
sumArray(array);
var t1 = performance.now();
console.log('From JS, time to sum: ' + (t1 - t0) / 1000 + ' s');
}
function sumArray(array) {
var i;
var s = 0;
for (i = 0; i < array.length; i++) {
s += array[i];
}
return s;
}
c# 代码(index.razor)
@page "/"
@inject IJSRuntime JSRuntime;
@using System.Text
@using BlazorWasmOnlyTest.Shared
<h1>Hello, world!</h1>
Welcome to your new app.
<div class="container">
<div class="row mb-2">
<div class="col">
<button class="btn btn-primary" @onclick="@TestInvokeJS">Test invoke js</button>
</div>
</div>
</div>
@code {
private int _id;
private string _status = "";
private DataInputFileForm _dataInputFileForm;
private async void TestInvokeJS()
{
var n = 100000;
var array = new double[n];
for (int i = 0; i < n; i++)
{
array[i] = i;
}
var w = new System.Diagnostics.Stopwatch();
w.Start();
await JSRuntime.InvokeVoidAsync("testSumArray",array);
w.Stop();
Console.WriteLine($"C# time to invoke js and sum: {w.ElapsedMilliseconds/1000:F3} s");
}
}
首页页面 index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>BlazorWasmOnlyTest</title>
<base href="/" />
<link href="css/bootstrap/bootstrap.min.css" rel="stylesheet" />
<link href="css/app.css" rel="stylesheet" />
<script src="js/test.js"></script>
</head>
<body>
<app>Loading...</app>
<div id="blazor-error-ui">
An unhandled error has occurred.
<a href="" class="reload">Reload</a>
<a class="dismiss">x</a>
</div>
<script src="_framework/blazor.webassembly.js"></script>
</body>
</html>
在我的机器上运行提示以下日志输出:
From JS, time to sum: 0.0037800000282004476 s
C# time to invoke js and sum: 7.000 s
这是一个相当高的开销时间......有谁知道是否有办法加快速度(主要功能在 Blazor/C# 中无法实现,所以我必须调用Javascript方法 - 主要是更新前端一些视图)
编辑:我已经尝试过这里描述的同步方法,执行时间没有任何差异。
var jsInProcess2 = (IJSInProcessRuntime)JSRuntime;
jsInProcess2.InvokeVoid("testSumArray", array);
我尝试使用同步互操作传递 JSON 字符串:
var jsInProcess3 = (IJSInProcessRuntime)JSRuntime;
var array_json3 = System.Text.Json.JsonSerializer.Serialize(array);
jsInProcess3.InvokeVoid("testSumArray3", array_json);
function testSumArray3(array_json_string) {
var t0 = performance.now();
var array = JSON.parse(array_json_string);
var s = sumArray(array);
var t1 = performance.now();
console.log('From JS, time to sum: ' + (t1 - t0) / 1000 + ' s');
console.log('Array sum = ' + s);
}
并使用 JSON 字符串和 InvokeUnmarshalled js interopcall:
var jsInProcess4 = (Microsoft.JSInterop.WebAssembly.WebAssemblyJSRuntime)JSRuntime;
var array_json4 = System.Text.Json.JsonSerializer.Serialize(array);
jsInProcess4.InvokeUnmarshalled<string,string>("testSumArray4", array_json4);
function testSumArray4(array_mono_json_string) {
var t0 = performance.now();
const array_json_string = BINDING.conv_string(array_mono_json_string);
var array = JSON.parse(array_json_string);
var s = sumArray(array);
var t1 = performance.now();
console.log('From JS, time to sum: ' + (t1 - t0) / 1000 + ' s');
console.log('Array sum = ' + s);
}
所有方法都需要大约相同的时间,6-7 秒才能完成(在 javascript 函数中大约需要 0.0015-0.006 秒)。
我试图弄清楚如何调用 unmarshalled 传递一个数组,使用BINDING.mono_array_to_js_array在这个文件中找到,但这会引发一个很长的错误。C#:
var sum = jsInProcess4.InvokeUnmarshalled<double[],double>("testSumArray5",array)
js:
function testSumArray5(array_in) {
var t0 = performance.now();
var array = BINDING.mono_array_to_js_array(array_in);
console.log(array[0]);
var s = sumArray(array);
var t1 = performance.now();
console.log('From JS, time to sum: ' + (t1 - t0) / 1000 + ' s');
console.log('Array sum = ' + s);
return s;
}
刚刚找到在 js 中使用 .net 字节或浮点数组的方法。
c#
[Inject] //Injected JSRuntime from Blazor DI
private IJSRuntime JSRuntime { get; set; }
byte[] bytes1;
float[] floats2;
...
if (JSRuntime is IJSUnmarshalledRuntime webAssemblyJSRuntime)
{
webAssemblyJSRuntime.InvokeUnmarshalled<byte[], float[], object>
("downloadArray", bytes1, floats2);
}
JavaScript:
function downloadArray(bytes1, floats2) {
// Easy way to convert Uint8 arrays
var byteArray = Blazor.platform.toUint8Array(bytes1);
// Adapted method above for float32
var m = floats2 + 12;
var r = Module.HEAP32[m >> 2]
var j = new Float32Array(Module.HEAPF32.buffer, m + 4, r);
}
这里的结果是在合理的时间内分别来自 byte[] 和 float[] 的 Uint8Array 和 Float32Array 对象。
可能有任何方法来获取 js 数组,因为您可以从 ArrayBuffers 访问整个 .net 堆,例如 Module.HEAPU8(Uint8Array 内的堆)或 Module.HEAPF32(Float32Array 内的堆),并且可以通过来自 InvokeUnmarshalled 参数的指针轻松访问对象.
关注我的微信公众号
在公众号里留言交流
投稿邮箱:1052839972@qq.com
庭院深深深几许?杨柳堆烟,帘幕无重数。
玉勒雕鞍游冶处,楼高不见章台路。
雨横风狂三月暮。门掩黄昏,无计留春住。
泪眼问花花不语,乱红飞过秋千去。
如果感觉对您有帮助
欢迎向作者提供捐赠
这将是创作的最大动力