From 4c5ccd3e8fd1126722e6b3dc3d87a77d90079902 Mon Sep 17 00:00:00 2001 From: Hans Petter Jansson Date: Mon, 27 Jul 2020 17:03:58 +0200 Subject: [PATCH] gslice: Allow inlining of memset() in g_slice_new0() macro Inlining the memset() is beneficial since the size here is constant and usually small. Closes #5 --- glib/gslice.h | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/glib/gslice.h b/glib/gslice.h index ff8b02a65..002410635 100644 --- a/glib/gslice.h +++ b/glib/gslice.h @@ -23,6 +23,7 @@ #endif #include +#include G_BEGIN_DECLS @@ -43,7 +44,22 @@ void g_slice_free_chain_with_offset (gsize block_size, gpointer mem_chain, gsize next_offset); #define g_slice_new(type) ((type*) g_slice_alloc (sizeof (type))) -#define g_slice_new0(type) ((type*) g_slice_alloc0 (sizeof (type))) + +/* Allow the compiler to inline memset(). Since the size is a constant, this + * can significantly improve performance. */ +#if defined (__GNUC__) && (__GNUC__ >= 2) && defined (__OPTIMIZE__) +# define g_slice_new0(type) \ + (type *) (G_GNUC_EXTENSION ({ \ + gsize __s = sizeof (type); \ + gpointer __p; \ + __p = g_slice_alloc (__s); \ + memset (__p, 0, __s); \ + __p; \ + })) +#else +# define g_slice_new0(type) ((type*) g_slice_alloc0 (sizeof (type))) +#endif + /* MemoryBlockType * * g_slice_dup (MemoryBlockType, * MemoryBlockType *mem_block);