Microsoft.DirectX.Matrix is a 4x4 value type / struct, containing 16
elements, M11 through M44.

System.Drawing.Drawing2D.Matrix is a very different class.  It is a 3x3
matrix, and it is a reference class.  This is because it is just a wrapper
around a GDI+ pointer.  I don't know what is behind that -- either simply an
unmanaged C++ class/structure, or a kernel / GDI+ handle.

It appears that Microsoft.DirectX.Matrix, the managed type, has a memory
layout that is compatible with the unmanaged DirectX structure, D3DMATRIX.
This is to be expected, of course -- it allows the Managed DirectX libraries
to simply cast pointers.

However, all of the Managed DirectX methods that take a Matrix as an input
value do so as a pass-by-value value-type.  I can't find any at all that use
"ref".

I'm concerned with both the waste of CPU time, and the excessive and
completely unnecessary stack growth.  I understand that it "looks" nice, but
in this situation it seems like the wrong decision.

Now, if the JIT can inline these short wrapper calls, I'm totally fine with
that.  I really like the idea that the MSIL code represents the *semantics*
and not the *mechanics* of what is happening -- the methods take as input a
structure, and they do not (at least in the semantic model of the language)
modify that structure.  All I care about is that the combination of "best
practices" (such as passing structures like Matrix by value, instead of by
reference) and the JIT implementation should do something sensible in the
real world.

-- arlie


-----Original Message-----
From: Unmoderated discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of Shawn A. Van Ness
Sent: Monday, May 17, 2004 1:29 AM
To: [EMAIL PROTECTED]
Subject: Re: [ADVANCED-DOTNET] Large structures

> This seems like a big waste of time

Arlie, are you concerned more about the time (ie, the memcpy operation) or
excess growth of the stack (eg, in recursive scenarios)?  Or both?

I don't have the managed dx sdk handy -- is their Matrix really a valuetype?
In System.Drawing.Drawing2D, it's a class.  Somewhat ironic, considering
that one's only 2x3 floats. :/

I'd be shocked to learn that the JIT we have today gives special treatment
to large valuetypes -- it'd be a pretty obscure opt, if not for the DirectX
calls you cite.  But I agree it sounds like it would be feasible.
(Especially in scenarios where the JIT also decides to inline the function
in question -- wouldn't even need to pass a pointer, on x86 -- just emit
opcodes which read the fields from somewhere above the ebp, instead of below
it.)

Cheers,
-Shawn
http://www.windojitsu.com/


-----Original Message-----
From: Arlie Davis [mailto:[EMAIL PROTECTED]
Sent: Sunday, May 16, 2004 11:51
Subject: Large structures

I have a few questions for the advanced CLR people here.  I haven't been
able to find any satisfactory answers elsewhere.

Consider value types / structures in CLR, and passing them as arguments to
methods.  Does the MS CLR *always* copy the entire structure onto the stack?
Or does it ever simply pass a pointer to the structure?

I like value types.  However, structures of any significant size are just
impractical, if they must always be copied.  For example, in managed
DirectX, nearly all of the matrix manipulation methods take "Matrix foo" as
an argument, never "ref Matrix foo".  These matrices are 4x4 floats, which
means that 64 bytes must be copied for every single matrix that is passed
into a method.

This seems like a big waste of time, compared to the same thing in C++,
where you would simply always pass a pointer to the matrix.  Does the CLR
actually pass an entire Matrix on the stack?  Or just a pointer?

It seems like it would be within the specification / requirements of the CLR
to allow a CLR implementation to do this, if it realizes that the method
never alters the structure, only examines its fields.

Any insight is appreciated.

-- arlie

===================================
This list is hosted by DevelopMentor.  http://www.develop.com Some .NET
courses you may be interested in:

NEW! Guerrilla ASP.NET, 17 May 2004, in Los Angeles
http://www.develop.com/courses/gaspdotnetls

View archives and manage your subscription(s) at http://discuss.develop.com

===================================
This list is hosted by DevelopMentor.  http://www.develop.com Some .NET
courses you may be interested in:

NEW! Guerrilla ASP.NET, 17 May 2004, in Los Angeles
http://www.develop.com/courses/gaspdotnetls

View archives and manage your subscription(s) at http://discuss.develop.com

===================================
This list is hosted by DevelopMentor®  http://www.develop.com
Some .NET courses you may be interested in:

NEW! Guerrilla ASP.NET, 17 May 2004, in Los Angeles
http://www.develop.com/courses/gaspdotnetls

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to