Re: Garbage collectable pinned arrays!



"Jon Skeet [C# MVP]" <skeet@xxxxxxxxx> wrote in message news:MPG.221c2857a51749bf8d2@xxxxxxxxxxxxxxxxxxxxxxx
Willy Denoyette [MVP] <willy.denoyette@xxxxxxxxxx> wrote:

<snip>

Jon, what you are doing here is not directly comparable with what the OP is
doing, but it illustrates the overhead taken by "protecting" an object in
memory.
The OP is measuring the overhead of a GCHandle.Alloc, which is not what
happens when passing an object during a call into unmanaged and not what
happens when interop 'pins' the object during a GC run.

By using a "fixed" and "unsafe" code block, you are effectively creating a
pointer, the OP is creating "pinned object handles".

Do we know that the OP can't actually use pointers? I probably missed a
post somewhere.


I don't know either, this thread is simply derailed as it is. As far as I understand, the OP thinks that pinning is
exactly what is done by GCHandle.Alloc with GCHandleType.Pinned, that's why he is measuring this methods overhead. But interop doesn't pin implicitely, it pins the arguments when needed. It's so easy to watch this when running some code in an unmanaged debugger, provide you know how to use your tools ;-).
It's also possible to inspect what happens when a GC comes along, the problem is that it takes some time to find out where to set the break-points.


Basically what the JIT does is nothing more than taking the address of the
first element of an array, put the address of the object (the array) in the
"Pointer Table" ( which is part of the GCInfo table) together with some
tracking info. The array is not really "pinned", but the GC knows how to
deal with this, that is, he inspects the pointer table before he starts a
scan, so he will not move or collect the object as long as the current
instruction falls in the range of the tracking info of this pointer.

Making sure that an object isn't moved or collected sounds like pinning
it to me. It's pinned as far as the IL goes, too - the variable is
explicitly marked with the "pinned" flag.


Yes, but what's been produced by the JIT is not the same as you "really" pin an object at run-time, say by using GCHandle.Alloc with GCHandleType.Pinned, and it's also not exactly the same as done by the Interop layer.
Fixed code blocks and pointers are the cheapest way to handle "pinned" buffers, this is quite normal as it can be done a compile time, which is not the case when using interop. Also, note that fixed was introduced after the initial release of the framework, the Pointer table did not exists in the first versions of the CLR.



Now, that may not be doing everything the OP is talking
about - but I don't see how it doesn't count as being pinned.


What I mean is, that it's not "pinned", like it's done when calling GCHAndle.Alloc, that doesn't mean the objects address isn't fixed for the duration of the scope of the declaring block.
Actually, there is no "pinned reference" in the GCInfo table (the pinned object table) when using "fixed", nor is there a bit set in the object header.
Not sure however what kind of "pinning" is used by the interop layer , but I'm sure it isn't the same as done with unsafe/fixed. I know that in V1.1 it was done by setting a bit in the objects header (for both PInvoke and COM interop), but possibly this has changed since more bits are used for other purposes.

Willy.

.



Relevant Pages

  • Re: A garbage collector for C++
    ... Support C++ raw pointer, unions, bit-fields and hidden pointers. ... Even conducting an accurate tracing, ... synchronization overhead. ... doing that incur the overheads of both tracing and reference counting. ...
    (comp.programming.threads)
  • Re: Interop Marshalling Overhead - Newbie qn
    ... Interop Overhead ... In my application I'm quite comfortable with the interop ... Marshalling Overhead ... int GetDataFromXYZ; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Ancient history
    ... If a pointer holds origin, ... into one allocation into a pointer into another. ... where languages like Fortran 77 can have reliable ... the situation in C is such that the overhead is generally log ...
    (sci.crypt)
  • Re: Interop Marshalling Overhead - Newbie qn
    ... SuppressUnmanagedCodeSecurityAttribute to my interop calls I could reduce the ... interop access overhead by a factor of 4-5! ... >> expensive marshalling penalties. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Ada Popularity: Comparison of Ada/Charles with C++ STL (and Perl)
    ... the overhead of conversion is virtually none (at least in ... so std::string::c_strboils down to returning a pointer. ... the GCC implementation writes terminating '\0' every time ... little overhead contrary to what many people suspect - that converting ...
    (comp.lang.ada)

Loading