unsafe

C# performance - Using unsafe pointers instead of IntPtr and Marshal

走远了吗. 提交于 2019-11-27 04:13:44
问题 Question I'm porting a C application into C#. The C app calls lots of functions from a 3rd-party DLL, so I wrote P/Invoke wrappers for these functions in C#. Some of these C functions allocate data which I have to use in the C# app, so I used IntPtr 's, Marshal.PtrToStructure and Marshal.Copy to copy the native data (arrays and structures) into managed variables. Unfortunately, the C# app proved to be much slower than the C version. A quick performance analysis showed that the above mentioned

True Unsafe Code Performance

大憨熊 提交于 2019-11-27 04:01:27
问题 I understand unsafe code is more appropriate to access things like the Windows API and do unsafe type castings than to write more performant code, but I would like to ask you if you have ever noticed any significant performance improvement in real-world applications by using it when compared to safe c# code. 回答1: Some Performance Measurements The performance benefits are not as great as you might think. I did some performance measurements of normal managed array access versus unsafe pointers

Maximum length of byte[]?

家住魔仙堡 提交于 2019-11-27 02:41:26
问题 I'm trying to create an array of byte s whose length is UInt32.MaxValue . This array is essentially a small(ish) in-memory database: byte[] countryCodes = new byte[UInt32.MaxValue]; On my machine, however, at run-time, I get a System.OverflowException with "Arithmetic operation resulted in an overflow". What's the deal? Do I need to use an unsafe block and malloc ? How would I do that in C#? 回答1: The current implementation of System.Array uses Int32 for all its internal counters etc, so the

What is the overhead of C# fixed statement on a managed unsafe struct containing fixed arrays?

我与影子孤独终老i 提交于 2019-11-27 02:14:28
问题 I've been trying to determine what the true cost of using the fixed statement within C# for managed unsafe structs that contain fixed arrays. Please note I am not referring to unmanaged structs. Specifically, is there any reason to avoid the pattern shown by 'MultipleFixed' class below? Is the cost of simply fixing the data non zero, near zero (== cost similar to setting & clearing a single flag when entering/exiting the fixed scope), or is it significant enough to avoid when possible?

Cannot take the address of, get the size of, or declare a pointer to a managed type

筅森魡賤 提交于 2019-11-27 01:55:36
问题 I've done a fair bit of research, but am stuck now as to why I'm still getting this error. I have a struct with the following attributes: struct Account { //private attributes private double mBalance; private int mAccountNumber; private string mName; private string mDateCreated; } and am trying to do the following: class BankManager { //private attributes private unsafe Account *mAccounts; private unsafe bool *mAccountsAvailable; private int mNumberAccounts; } Even after turning my class

C# unsafe value type array to byte array conversions

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 01:46:36
I use an extension method to convert float arrays into byte arrays: public static unsafe byte[] ToByteArray(this float[] floatArray, int count) { int arrayLength = floatArray.Length > count ? count : floatArray.Length; byte[] byteArray = new byte[4 * arrayLength]; fixed (float* floatPointer = floatArray) { fixed (byte* bytePointer = byteArray) { float* read = floatPointer; float* write = (float*)bytePointer; for (int i = 0; i < arrayLength; i++) { *write++ = *read++; } } } return byteArray; } I understand that an array is a pointer to memory associated with information on the type and number

Why does this code work without the unsafe keyword?

徘徊边缘 提交于 2019-11-27 00:37:52
问题 In an answer to his own controversial question, Mash has illustrated that you don't need the "unsafe" keyword to read and write directly to the bytes of any .NET object instance. You can declare the following types: [StructLayout(LayoutKind.Explicit)] struct MemoryAccess { [FieldOffset(0)] public object Object; [FieldOffset(0)] public TopBytes Bytes; } class TopBytes { public byte b0; public byte b1; public byte b2; public byte b3; public byte b4; public byte b5; public byte b6; public byte

Where is sun.misc.Unsafe documented? [closed]

二次信任 提交于 2019-11-27 00:13:03
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . Does anyone know of any comprehensive documentation for sun.misc.Unsafe ? I'm looking for documentation on Unsafe.putOrderedInt() . This was all I was able to find. public native void putOrderedInt(Object o, long offset, int x) Ordered/Lazy version of #putIntVolatile(Object, long, int) Does anyone know of a

Java 8 Unsafe: xxxFence() instructions

笑着哭i 提交于 2019-11-27 00:06:15
问题 In Java 8 three memory barrier instructions were added to Unsafe class (source): /** * Ensures lack of reordering of loads before the fence * with loads or stores after the fence. */ void loadFence(); /** * Ensures lack of reordering of stores before the fence * with loads or stores after the fence. */ void storeFence(); /** * Ensures lack of reordering of loads or stores before the fence * with loads or stores after the fence. */ void fullFence(); If we define memory barrier with the

Converting System.Decimal to System.Guid

半世苍凉 提交于 2019-11-26 23:21:42
问题 I have a big dictionary where the key is decimal, but the GetHashCode() of System.Decimal is disasterously bad. To prove my guess, I ran a for loop with 100.000 neigboring decimals and checked the distribution. 100.000 different decimal numbers used only 2 (two!!!) different hashcodes. Decimal is represented as 16 bytes. Just like Guid! But the GetHashCode() distribution of Guid is pretty good. How can I convert a decimal to Guid in C# as cheap as possible? Unsafe code is OK! EDIT: The test