g_new0 performance
Submitted by Morten Welinder
Link to original bug (#106925)
Description
g_new0 is very often called with a constant object count, notably one. Therefore, the total size if often known at compile time and the compiler ought to be able to zero the structure in a smarter way that just memset.
Unfortunately, g_new0 is implemented in terms of g_malloc0 so the size knowledge is tossed away, :-(
For gcc, something like this ought to work:
#define g_new0(struct_type, n_structs)
({ gsize _gnew0_size = sizeof (struct_type) * (n_structs);
struct_type *_gnew0_res = g_malloc (_gnew0_size);
memset (_gnew0_res, 0, _gnew0_size);
_gnew0_res; })