[Gtk-sharp-list] [PATCH] Extra allocations in Gtk# structs

Ben Maurer bmaurer@users.sourceforge.net
Sun, 11 Jan 2004 20:24:56 -0500


--=-EZH/CvcvtqDncWwbY7Dn
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Hello,

Today I found that in every Gtk# struct a `New' method is generated that
includes:

(<mytype>) Marshal.PtrToStructure (raw, self.GetType ());

However, this is slow, because the Marshal method ends up allocating a
boxed value type. So, I changed the generator to emit:

return *(<mytype>*) raw;

This is much faster, as it is just an indirection + copy. I tested
monodoc and MD after the change, they work fine.

May I commit?

-- Ben

--=-EZH/CvcvtqDncWwbY7Dn
Content-Disposition: attachment; filename=generator-struct-no-marshal.patch
Content-Type: text/x-patch; name=generator-struct-no-marshal.patch; charset=UTF-8
Content-Transfer-Encoding: 7bit

Index: StructBase.cs
===================================================================
RCS file: /cvs/public/gtk-sharp/generator/StructBase.cs,v
retrieving revision 1.43
diff -u -r1.43 StructBase.cs
--- StructBase.cs	10 Dec 2003 22:56:49 -0000	1.43
+++ StructBase.cs	12 Jan 2004 01:05:50 -0000
@@ -250,13 +250,11 @@
 
 			sw.WriteLine ("\t\tpublic static {0} Zero = new {0} ();", QualifiedName);
 			sw.WriteLine();
-			sw.WriteLine ("\t\tpublic static " + QualifiedName + " New(IntPtr raw) {");
+			sw.WriteLine ("\t\tpublic unsafe static " + QualifiedName + " New(IntPtr raw) {");
 			sw.WriteLine ("\t\t\tif (raw == IntPtr.Zero) {");
 			sw.WriteLine ("\t\t\t\treturn {0}.Zero;", QualifiedName);
 			sw.WriteLine ("\t\t\t}");
-			sw.WriteLine ("\t\t\t{0} self = new {0}();", QualifiedName);
-			sw.WriteLine ("\t\t\tself = ({0}) Marshal.PtrToStructure (raw, self.GetType ());", QualifiedName);
-			sw.WriteLine ("\t\t\treturn self;");
+			sw.WriteLine ("\t\t\treturn *({0}*) raw;", QualifiedName);
 			sw.WriteLine ("\t\t}");
 			sw.WriteLine ();
 

--=-EZH/CvcvtqDncWwbY7Dn--