Eliminate managed -> native transition cost of errno conversion
Currently, we P/Invoke whenever we need to go from raw errno to PAL value or vice versa. We could instead retrieve a shared table pointer once from native code and do the lookup from managed code.
EDIT: I think we should probably just return converted errors from shims: https://github.com/dotnet/corefx/pull/3877#issuecomment-148484741
In dotnet/corefx#2606, @stephentoub and I had the following conversation about it (can't manage to link to it):
@stephentoub
What would you think of exposing a function to retrieve the whole conversion table once, and then doing all of these conversions purely in managed without having to make a managed->native call each time? I realize these are on an error path, so performance isn't as critical, but there are cases where things can fail that aren't actually failures, e.g. EINTR for I/O operations.
@nguerrera
I thought about this and was concerned about duplicating the table in each library (since we just compile the Interop.* in to each), but there aren't really that many errors so maybe it's OK after all.
@stephentoub
That's a fair point. Up to you. Either way, we'll want to re-evaluate as we do more performance testing/tuning.
(Note that it could probably be done in a way that has the table shared across libraries, e.g. if the native code created the table and returned a pointer to it to managed in some format the managed code could easily use as-is.)
@nguerrera
I'll file an issue to investigate, but stick to the current approach for the initial commit. You're right that it could be shared that way -- I hadn't thought of it. It is slightly tricky because we'd need to share something that is efficient to lookup in directly (whereas I was imagining building the lookup from an unordered list in managed code). The Pal -> Platform case can just be an index into an array so that's easy. However, the more interesting and common Platform -> Pal case can be sparse. I'd be inclined to use a sorted array of (int, Error) + binary search to keep the shared data structure trivial. It'll take me some time to measure, etc., and it's just complex enough to warrant its own review IMHO.
Would it be against architecture/design of corefx to use a pre-processing tool for solving this problem? Say, we have a program that would take , as input, a file containing c# code interspersed with c constructs and generates the corresponding C# file with the values populated for the target architecture:
[C# + C ] -> tool (On target architecture) -> C#
Such a tool would need to run at the corefx build time on the target architecture.
This is analogous to the use of hsc2hs in Haskell ecosystem.
This approach has been working fine and hasn't shown to be a problem for performance, so I'm closing this. We can address issues if/when they arise.