-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemo.cs
More file actions
269 lines (237 loc) · 9.02 KB
/
Demo.cs
File metadata and controls
269 lines (237 loc) · 9.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#:project ./QuickJSharp/QuickJSharp.csproj
#:property AllowUnsafeBlocks=true
// Run in release (dotnet run -c Release Demo.cs)
// Or publish as AOT and run the resulting executable (dotnet publish -c Release Demo.cs) for more realisticstable runs.
// This is a quickly thrown together demo of various QuickJSharp features and performance characteristics,
// not meant to be a formal benchmark or test suite. The code is intentionally straightforward and unoptimized
// to show typical usage patterns and relative overheads of different interop approaches.
// Also we're using primitives here for simplicity. In real sinarios you'd be using strings and objects, which
// add overhead, especially for string marshalling, though we try to minimize that as much as possible but it's still
// something to be aware of in your usage and avoid where possible.
using System.Runtime.InteropServices;
using System.Text;
using QuickJSharp;
using NativeJS = QuickJSharp.Native.QuickJS;
using JSRuntime rt = new();
using JSContext ctx = rt.CreateContext();
rt.ModuleLoader = (c, name) =>
{
if (name == "calc")
{
return c.CreateModule(name)
.Export(
"add",
ctx => ctx.NewFunction((c, _, args) => c.NewDouble(args[0].ToDouble(c) + args[1].ToDouble(c)), "add")
)
.Export(
"sub",
ctx => ctx.NewFunction((c, _, args) => c.NewDouble(args[0].ToDouble(c) - args[1].ToDouble(c)), "sub")
)
.Export("PI", ctx => ctx.NewBigInt64(3))
.Build();
}
return null;
};
var sw = System.Diagnostics.Stopwatch.StartNew();
var timeObj = ctx.NewObject();
timeObj.SetProperty(ctx, "now", ctx.NewFunction((c, _, _) => c.NewDouble(sw.Elapsed.TotalMilliseconds), "now"));
ctx.GlobalObject.SetProperty(ctx, "Time", timeObj);
unsafe
{
ctx.GlobalObject.SetProperty(ctx, "rawAdd", ctx.NewFunctionRaw(&Program.AddProxy, "rawAdd", 2));
}
int capturedIncrement = 1;
ctx.GlobalObject.SetProperty(
ctx,
"managedAdd",
ctx.NewFunction(
(JSContext c, JSValue thisVal, ReadOnlySpan<JSValue> args) =>
{
return c.NewInt32(args[0].ToInt32(c) + capturedIncrement + args[1].ToInt32(c) - 1);
},
"managedAdd"
)
);
static JSValue AddManaged(JSContext c, JSValue thisVal, ReadOnlySpan<JSValue> args)
{
return c.NewInt32(args[0].ToInt32(c) + args[1].ToInt32(c));
}
ctx.GlobalObject.SetProperty(ctx, "managedAddStatic", ctx.NewFunction(AddManaged, "managedAddStatic"));
var jsObj = ctx.Eval("({ get val() { return 42; } })");
ctx.GlobalObject.SetProperty(ctx, "jsObj", jsObj);
var nativeObj = ctx.NewObject();
var nativeKey = ctx.NewAtom("val");
var nativeGetter = ctx.NewFunction((c, _, _) => c.NewInt32(42), "getVal", 0);
nativeObj.DefineProperty(
ctx,
nativeKey,
nativeGetter,
JSValue.Undefined,
JSPropertyFlags.Enumerable | JSPropertyFlags.Configurable
);
ctx.GlobalObject.SetProperty(ctx, "nativeObj", nativeObj);
nativeKey.Free(ctx);
const int Iterations = 1_000_000;
string benchScript =
$@"
(function() {{
const iterations = {Iterations};
const now = () => Time.now();
let start = now();
for(let i=0; i < iterations; i++) rawAdd(i, 1);
let end = now();
const rawTime = end - start;
start = now();
for(let i=0; i < iterations; i++) managedAdd(i, 1);
end = now();
const managedTime = end - start;
start = now();
for(let i=0; i < iterations; i++) managedAddStatic(i, 1);
end = now();
const managedStaticTime = end - start;
start = now();
for(let i=0; i < iterations; i++) {{ let x = jsObj.val; }}
end = now();
const jsGetterTime = end - start;
start = now();
for(let i=0; i < iterations; i++) {{ let x = nativeObj.val; }}
end = now();
const nativeGetterTime = end - start;
return 'Iterations: ' + iterations.toLocaleString() + '\n' +
'-----------------------------------------\n' +
'FUNCTION CALLS:\n' +
'RAW (Native Function*): ' + rawTime.toFixed(2) + 'ms (' + (iterations / (rawTime || 1) * 1000).toFixed(0) + ' calls / sec)\n' +
'MANAGED (Lambda): ' + managedTime.toFixed(2) + 'ms (' + (iterations / (managedTime || 1) * 1000).toFixed(0) + ' calls / sec)\n' +
'MANAGED (Static Method): ' + managedStaticTime.toFixed(2) + 'ms (' + (iterations / (managedStaticTime || 1) * 1000).toFixed(0) + ' calls / sec)\n' +
'Overhead (Lambda): ' + (managedTime - rawTime).toFixed(2) + 'ms total extra cost\n' +
'-----------------------------------------\n' +
'PROPERTY GETTERS:\n' +
'JS GETTER (get val()): ' + jsGetterTime.toFixed(2) + 'ms (' + (iterations / (jsGetterTime || 1) * 1000).toFixed(0) + ' gets / sec)\n' +
'NATIVE GETTER (C# callback): ' + nativeGetterTime.toFixed(2) + 'ms (' + (iterations / (nativeGetterTime || 1) * 1000).toFixed(0) + ' gets / sec)\n' +
'-----------------------------------------\n' +
'Native vs JS overhead: ' + (nativeGetterTime / (jsGetterTime || 1)).toFixed(2) + 'x';
}})();";
JSValue resultWrap = ctx.Eval(benchScript);
Console.WriteLine(resultWrap.ToString(ctx));
resultWrap.Free(ctx);
string moduleDemoScript =
@"
import { add, sub, PI } from 'calc';
console.log(`Add: ${add(10, 5)}`);
console.log(`Sub: ${sub(10, 5)}`);
console.log(`PI (integer): ${PI}`);
";
ctx.GlobalObject.SetProperty(ctx, "console", ctx.NewObject());
ctx.GlobalObject.GetProperty(ctx, "console")
.SetProperty(
ctx,
"log",
ctx.NewFunction(
(c, _, args) =>
{
Console.WriteLine(args[0].ToString(c));
return c.Undefined;
},
"log"
)
);
var modRes = ctx.Eval(moduleDemoScript, "main.js", JSEvalFlags.Module);
if (modRes.IsException)
{
var err = ctx.GetException();
Console.WriteLine($"Error: {err.ToString(ctx)}");
modRes.Free(ctx);
}
Console.WriteLine("\n--- Property Access Benchmark (Atom vs String) ---");
var benchObj = ctx.Eval("({ propInt: 0, propStr: '' })");
try
{
var propIntAtom = ctx.NewAtom("propInt");
var propStrAtom = ctx.NewAtom("propStr");
const int BenchIterations = 1_000_000;
sw.Restart();
for (int i = 0; i < BenchIterations; i++)
{
benchObj.SetProperty(ctx, "propInt", ctx.NewInt32(i));
}
var setIntStringTime = sw.Elapsed.TotalMilliseconds;
sw.Restart();
for (int i = 0; i < BenchIterations; i++)
{
benchObj.SetProperty(ctx, propIntAtom, ctx.NewInt32(i));
}
var setIntAtomTime = sw.Elapsed.TotalMilliseconds;
sw.Restart();
long sum = 0;
for (int i = 0; i < BenchIterations; i++)
{
var val = benchObj.GetProperty(ctx, "propInt");
sum += val.ToInt32(ctx);
val.Free(ctx);
}
var getIntStringTime = sw.Elapsed.TotalMilliseconds;
sw.Restart();
sum = 0;
for (int i = 0; i < BenchIterations; i++)
{
var val = benchObj.GetProperty(ctx, propIntAtom);
sum += val.ToInt32(ctx);
val.Free(ctx);
}
var getIntAtomTime = sw.Elapsed.TotalMilliseconds;
var testStr = "Hello World";
sw.Restart();
for (int i = 0; i < BenchIterations; i++)
{
benchObj.SetProperty(ctx, "propStr", ctx.NewString(testStr));
}
var setStrStringTime = sw.Elapsed.TotalMilliseconds;
sw.Restart();
for (int i = 0; i < BenchIterations; i++)
{
benchObj.SetProperty(ctx, propStrAtom, ctx.NewString(testStr));
}
var setStrAtomTime = sw.Elapsed.TotalMilliseconds;
Console.WriteLine($"Iterations: {BenchIterations:N0}");
Console.WriteLine(
$"SET Int (Str Key): {setIntStringTime:F2}ms ({(BenchIterations / setIntStringTime * 1000):N0} ops/s)"
);
Console.WriteLine(
$"SET Int (Atom Key): {setIntAtomTime:F2}ms ({(BenchIterations / setIntAtomTime * 1000):N0} ops/s)"
);
Console.WriteLine(
$"GET Int (Str Key): {getIntStringTime:F2}ms ({(BenchIterations / getIntStringTime * 1000):N0} ops/s)"
);
Console.WriteLine(
$"GET Int (Atom Key): {getIntAtomTime:F2}ms ({(BenchIterations / getIntAtomTime * 1000):N0} ops/s)"
);
Console.WriteLine(
$"SET Str (Str Key): {setStrStringTime:F2}ms ({(BenchIterations / setStrStringTime * 1000):N0} ops/s)"
);
Console.WriteLine(
$"SET Str (Atom Key): {setStrAtomTime:F2}ms ({(BenchIterations / setStrAtomTime * 1000):N0} ops/s)"
);
propIntAtom.Free(ctx);
propStrAtom.Free(ctx);
}
finally
{
benchObj.Free(ctx);
}
public partial class Program
{
[UnmanagedCallersOnly(CallConvs = [typeof(System.Runtime.CompilerServices.CallConvCdecl)])]
public static unsafe NativeJS.JSValue AddProxy(
NativeJS.JSContext* ctx,
NativeJS.JSValue this_val,
int argc,
NativeJS.JSValue* argv
)
{
int a,
b;
NativeJS.JS_ToInt32(ctx, &a, argv[0]);
NativeJS.JS_ToInt32(ctx, &b, argv[1]);
return NativeJS.JS_NewInt32(ctx, a + b);
}
}