From 4eb32173dbfc7dcf13fc126883c259605e58e7a3 Mon Sep 17 00:00:00 2001 From: Marko Kreen Date: Fri, 23 Nov 2007 10:57:42 +0000 Subject: [PATCH] system.h improvements - typedef bool to uchar instead of enum - clearer signedness behaviour - clean Assert() def - general memcpy() optimization - general strcmp() optimization --- include/system.h | 57 ++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 4 deletions(-) diff --git a/include/system.h b/include/system.h index 5088936..411b92d 100644 --- a/include/system.h +++ b/include/system.h @@ -65,8 +65,13 @@ #endif #ifdef CASSERT -#define Assert(e) do { if (unlikely(!(e))) { \ - fatal_noexit("Assert(%s) failed", #e); abort(); } } while (0) +#define Assert(e) \ +do { \ + if (unlikely(!(e))) { \ + fatal_noexit("Assert(%s) failed", #e); \ + abort(); \ + } \ +} while (0) #else #define Assert(e) #endif @@ -81,10 +86,12 @@ /* - * PostgreSQL types. + * bool type. */ -typedef enum { false=0, true=1 } bool; +typedef unsigned char bool; +#define false 0 +#define true 1 /* * PostgreSQL type OIDs for resultsets. @@ -108,3 +115,45 @@ size_t strlcat(char *dst, const char *src, size_t n); int getpeereid(int fd, uid_t *uid_p, gid_t *gid_p); #endif +/* + * memcpy() optimization - improves hash.c. + * + * GCC can optimize fixed-length memcpys but not variable-length ones. + * For short variable-length memcpys its faster to do dumb inlined copy + * than call out to libc. + */ + +#if defined(__GNUC__) && (__GNUC__ >= 3) + +static inline void *_inline_memcpy(void *dst_, const void *src_, size_t len) +{ + const uint8_t *src = src_; + uint8_t *dst = dst_; + while (len--) + *dst++ = *src++; + return dst_; +} + +#define memcpy(dst, src, len) \ + ( __builtin_constant_p(len) \ + ? memcpy(dst, src, len) \ + : ((__builtin_constant_p((len) < 16) && ((len) < 16)) \ + ? _inline_memcpy(dst, src, len) \ + : memcpy(dst, src, len))) + +#endif + +/* + * strcmp() optimization - compare first char inline. + */ + +static inline int _inline_strcmp(const char *a, const char *b) +{ + if ((*a - *b) != 0) + return (*a - *b); + return strcmp(a, b); +} + +#define strcmp(a, b) _inline_strcmp(a, b) + + -- 2.40.0