[Mono-dev] RE: Com Interop Patch - mcs

Paolo Molaro lupus at ximian.com
Wed Jan 11 11:51:40 EST 2006


On 01/11/06 Zoltan Varga wrote:
> On 1/9/06, Paolo Molaro <lupus at ximian.com> wrote:
> > > +        public static int AddRef(IntPtr pUnk)
> > > +        {
> > > +            IntPtr vtable = Marshal.ReadIntPtr(pUnk);
> > > +            IntPtr qi = Marshal.ReadIntPtr(vtable);
> > > +            // QueryInterface is 2st method
> > > +            IntPtr pAddRef = (IntPtr)(qi.ToInt64() + IntPtr.Size);
> > > +            AddRefDelegate add_ref = (AddRefDelegate)Marshal.GetDelegateForFunctionPointer(pAddRef, typeof(AddRefDelegate));
> > > +            return add_ref(pUnk);
> > > +        }
> >
> > Moving code to the managed side is sometimes the best way to implement
> > some things: this method is the proof that sometimes it's an orrible
> > idea:-) This stuff shuld be done in the C runtime.
> >
> 
>    I would argue otherwise. Writing correct runtime code is very hard,
> so the less
> we write, the less problems we will have later fixing it, adapting it
> to a new GC
> etc. We have our string copy implementation in managed code, so why not these ?

Since I moved quite a few of the string methods to managed code I
appreciate the benefits of doing that, but moving code to the managed
side is not a religion, there are reasons and they need to be evaluated.
For example:
*) faster execution
*) reduced memory usage
*) code readability and maintainability
*) reduced number of managed <-> unmanaged transitions

The managed method above is worse in any single item on the list than
having the simple icall that does:

static int
com_addref (com_ptr_t * pUnk) {
	return pUnk->vtable->qi->addref (pUnk);
}

With the 3-4 nice structure definitions that describe the com object,
vtable and qi format in memory.
Note that here we're talking about improvements of an order of magnitude
in the above measurable parameters.

In general, when possible, code should be written on the managed side.
This is not the case for the above method.

lupus

-- 
-----------------------------------------------------------------
lupus at debian.org                                     debian/rules
lupus at ximian.com                             Monkeys do it better



More information about the Mono-devel-list mailing list