In my C # application, I have a large straight (176 bytes) that probably goes to one hundred thousand times per second. The ceremony This function then simply takes an indicator for the structure and the pointer passes through unmanaged code. Neither the function nor any unmanaged code structure will make any modifications.
My question is, should I pass the structure with the value or reference of the function? In this particular case, my guess is that passing through the reference gets faster than pushing the 176 bytes on the call stack, unless JIT gets the recognition that the structure is never modified (my guess is That it can not recognize the structure because it is given to unmanaged code) and optimizes the code.
Since we are on it, let's also answer the more common case where the function does not the untranslated code to the pointers of the strait, but instead of some reading- Does the correct operation. Will it be faster to pass the structure from the context? In this case, GIT assumes that the structure is never modified and is favorable in this way? It is possibly more efficient to pass the 1-byte structure by reference not , but what structure structure is better for passing a structure based on context, if ever?
Thank you.
Edit:
It is also possible to create an "equivalent" square for regular use, as described below, and then use a structure when approaching unmanaged code. Please. I see two options here:
1) Create a "wrapper" class that is just in the straight, and then when you need to uncompress the code, pin and push an indicator in the structure, a potential problem It is that pinning has its own performance result.
2) Create an equivalent class, whose structure structure is copied when structure is required. But copying will take a lot of time and I think that to defeat the point of reference in the first place.
Edit:
As mentioned below, I will definitely be measuring the performance of each of these methods. Do and post the results However, I am still interested in seeing people's answers and arguments from an intellectual perspective.
Before you ask whether or not you should pass the structure from context, ask yourself Why should you have got such a big structure in the first place? Does it actually need a structure? If you need to use a straight at the point of P / Invoke, will it be a composition for just and then the equivalent class will be worth somewhere else?
One structure is big, very unusual ...
For more guidance on this, see the section.