Arlie Davis wrote: > 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?
Yes it does (at least in the 1.1 version). > Or does it ever simply pass a pointer to the structure? In theory it could do this optimization in some cases, but I don't think it is likely that they'll do it in practice. If you want to pass large structures, you can always pass them by reference (unless, of course, you're calling someone else's API, like in your case). > I like value types. However, structures of any significant > size are just impractical, if they must always be copied. It's a lot worse than you (and I) expected. Initially I created a value type with 16 floats, to do something equivalent to the DirextX Matrix (I don't have the managed DirectX sdk installed), but after having seen that I decided to create a really large value type, 100000 floats. My test program died with an InvalidProgramException. This is, of course, bogus. The spec says that a value type can be up to 1 MByte (and there is even a note that says that the "current" Microsoft implementation allows 4 Mbyte). After that I changed the value type to 1000 floats and that worked, but when I looked at the JIT generated code I almost couldn't believe my eyes. It had generated 1000 push instructions to copy the value onto the stack! That's just lame. > 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. It does seem like a strange API design choice, especially considering that the underlying native method does take a pointer. Regards, Jeroen =================================== 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