Skip to content

Commit 09560a0

Browse files
committed
Use unchecked functions with raw values for better performance.
1 parent e357e2c commit 09560a0

File tree

8 files changed

+5012
-4702
lines changed

8 files changed

+5012
-4702
lines changed

src/Function.FromCallback.cs

Lines changed: 2340 additions & 2340 deletions
Large diffs are not rendered by default.

src/Function.FromCallback.tt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ for (int hasCaller = 0; hasCaller < 2; hasCaller++)
8686
methodGenerics.Append(genericType);
8787
delegateType.Append(genericType);
8888

89-
parameterConverters.AppendLine($" var conv{genericType} = ValueBox.Converter<{genericType}>();");
89+
parameterConverters.AppendLine($" var conv{genericType} = ValueRaw.Converter<{genericType}>();");
9090
}
9191

9292
if (parameterCount > 0 && resultCount > 0)
@@ -117,7 +117,7 @@ for (int hasCaller = 0; hasCaller < 2; hasCaller++)
117117
methodGenerics.Append(genericType);
118118
delegateType.Append(genericType);
119119

120-
parameterConverters.AppendLine($" var conv{genericType} = ValueBox.Converter<{genericType}>();");
120+
parameterConverters.AppendLine($" var conv{genericType} = ValueRaw.Converter<{genericType}>();");
121121
}
122122

123123
if (resultCount > 1)
@@ -161,7 +161,7 @@ for (int hasCaller = 0; hasCaller < 2; hasCaller++)
161161
#>
162162
unsafe
163163
{
164-
Function.Native.WasmtimeFuncCallback func = (env, callerPtr, args, nargs, results, nresults) =>
164+
Function.Native.WasmtimeFuncUncheckedCallback func = (env, callerPtr, args_and_results, num_args_and_results) =>
165165
{
166166
using var caller = new Caller(callerPtr);
167167

@@ -188,7 +188,7 @@ for (int hasCaller = 0; hasCaller < 2; hasCaller++)
188188
genericType += (i + 1).ToString(CultureInfo.InvariantCulture);
189189
}
190190

191-
#>conv<#= genericType #>.Unbox(caller, args[<#= i.ToString(CultureInfo.InvariantCulture) #>].ToValueBox())<#
191+
#>conv<#= genericType #>.Unbox(caller, args_and_results[<#= i.ToString(CultureInfo.InvariantCulture) #>])<#
192192

193193
if (i + 1 < parameterCount)
194194
{
@@ -213,7 +213,7 @@ for (int hasCaller = 0; hasCaller < 2; hasCaller++)
213213
tupleAccessor = ".Item" + (i + 1).ToString(CultureInfo.InvariantCulture);
214214
}
215215

216-
#>results[<#= i.ToString(CultureInfo.InvariantCulture) #>] = Value.FromValueBox(conv<#= genericType #>.Box(result<#= tupleAccessor #>));
216+
#>args_and_results[<#= i.ToString(CultureInfo.InvariantCulture) #>] = conv<#= genericType #>.Box(caller, result<#= tupleAccessor #>);
217217
<#
218218
}
219219
#>
@@ -231,7 +231,7 @@ for (int hasCaller = 0; hasCaller < 2; hasCaller++)
231231
}
232232
};
233233

234-
Native.wasmtime_func_new(
234+
Native.wasmtime_func_new_unchecked(
235235
store.Context.handle,
236236
funcType,
237237
func,

src/Function.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1973,17 +1973,28 @@ internal static class Native
19731973
{
19741974
public delegate void Finalizer(IntPtr data);
19751975

1976-
public unsafe delegate IntPtr WasmtimeFuncCallback(IntPtr env, IntPtr caller, Value* args, UIntPtr nargs, Value* results, UIntPtr nresults);
1976+
public unsafe delegate IntPtr WasmtimeFuncCallback(IntPtr env, IntPtr caller, Value* args, nuint nargs, Value* results, nuint nresults);
1977+
1978+
public unsafe delegate IntPtr WasmtimeFuncUncheckedCallback(IntPtr env, IntPtr caller, ValueRaw* args_and_results, nuint num_args_and_results);
19771979

19781980
[DllImport(Engine.LibraryName)]
19791981
public static extern void wasmtime_func_new(IntPtr context, TypeHandle type, WasmtimeFuncCallback callback, IntPtr env, Finalizer? finalizer, out ExternFunc func);
19801982

19811983
[DllImport(Engine.LibraryName)]
1982-
public static unsafe extern IntPtr wasmtime_func_call(IntPtr context, in ExternFunc func, Value* args, UIntPtr nargs, Value* results, UIntPtr nresults, out IntPtr trap);
1984+
public static extern void wasmtime_func_new_unchecked(IntPtr context, TypeHandle type, WasmtimeFuncUncheckedCallback callback, IntPtr env, Finalizer? finalizer, out ExternFunc func);
1985+
1986+
[DllImport(Engine.LibraryName)]
1987+
public static unsafe extern IntPtr wasmtime_func_call(IntPtr context, in ExternFunc func, Value* args, nuint nargs, Value* results, nuint nresults, out IntPtr trap);
19831988

19841989
[DllImport(Engine.LibraryName)]
19851990
public static unsafe extern IntPtr wasmtime_func_type(IntPtr context, in ExternFunc func);
19861991

1992+
[DllImport(Engine.LibraryName)]
1993+
public static unsafe extern void wasmtime_func_from_raw(IntPtr context, nuint raw, out ExternFunc func);
1994+
1995+
[DllImport(Engine.LibraryName)]
1996+
public static unsafe extern nuint wasmtime_func_to_raw(IntPtr context, in ExternFunc func);
1997+
19871998
[DllImport(Engine.LibraryName)]
19881999
public static extern IntPtr wasm_functype_new(in ValueTypeArray parameters, in ValueTypeArray results);
19892000

0 commit comments

Comments
 (0)