https://github.com/linkdata/csharp-variadic-args
Performant ways of emulating C's VARARGS using P/Invoke or unsafe C#
https://github.com/linkdata/csharp-variadic-args
Last synced: 9 months ago
JSON representation
Performant ways of emulating C's VARARGS using P/Invoke or unsafe C#
- Host: GitHub
- URL: https://github.com/linkdata/csharp-variadic-args
- Owner: linkdata
- Created: 2016-11-03T14:41:43.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-11-07T13:09:23.000Z (over 9 years ago)
- Last Synced: 2025-03-26T03:18:50.278Z (12 months ago)
- Language: C#
- Size: 12.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# CSharp-variadic-args
Performant ways of emulating C's VARARGS using P/Invoke or unsafe C#
Provides two sample implementations for passing varidic arguments from C# to native (C).
Both implementations will pin pointer types (`byte[]` and `string`) for the duration of the native call. The `unsafe` implementation runs faster only because it can use `stackalloc` and avoiding to copy the `VarArg` structure that the safe version uses.
```
3205 calls/millisecond for SAFE "OnlyValueTypes"
4918 calls/millisecond for UNSAFE "OnlyValueTypes"
2209 calls/millisecond for SAFE "OnlyValueTypesAndString"
2952 calls/millisecond for UNSAFE "OnlyValueTypesAndString"
1617 calls/millisecond for SAFE "AllTypes"
1944 calls/millisecond for UNSAFE "AllTypes"
```
An interesting note here is that in some cases, pinning strings and buffers isn't the most performant way of passing these values. In the case of a slow native call, pinning objects will degrade GC performance and may cause erratic performance in multithreaded applications. In these situations, it's better to rely on P/Invoke or to use unmanaged memory allocations and make copies.