Giter Site home page Giter Site logo

csharp-variadic-args's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.