From c1b57126dd01f103e418a5446886e739363c8e39 Mon Sep 17 00:00:00 2001 From: Peter Bex Date: Tue, 27 Dec 2016 18:06:12 +0100 Subject: [PATCH] Add __builtin_expect to help branch prediction. Currently applied to all C_demand() checks, which are unlikely to fail. So we make sure the C compiler knows the default branch is more likely. --- c-backend.scm | 10 +++++----- chicken.h | 7 +++++++ 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/c-backend.scm b/c-backend.scm index a6205db..a5e0f5d 100644 --- a/c-backend.scm +++ b/c-backend.scm @@ -827,10 +827,10 @@ (when target-stack-size (gen #t "C_resize_stack(" target-stack-size ");") ) ) (gen #t "C_check_nursery_minimum(C_calculate_demand(" demand ",c," max-av "));" - #t "if(!C_demand(C_calculate_demand(" demand ",c," max-av "))){" + #t "if(C_unlikely(!C_demand(C_calculate_demand(" demand ",c," max-av ")))){" #t "C_save_and_reclaim((void*)C_" topname ",c,av);}" #t "toplevel_initialized=1;" - #t "if(!C_demand_2(" ldemand ")){" + #t "if(C_unlikely(!C_demand_2(" ldemand "))){" #t "C_save(t1);" #t "C_rereclaim2(" ldemand "*sizeof(C_word),1);" #t "t1=C_restore;}" @@ -845,7 +845,7 @@ (when (and (not unsafe) (not no-argc-checks) (> n 2) (not empty-closure)) (gen #t "if(c<" n ") C_bad_min_argc_2(c," n ",t0);") ) (when insert-timer-checks (gen #t "C_check_for_interrupt;")) - (gen #t "if(!C_demand(C_calculate_demand((c-" n ")*C_SIZEOF_PAIR +" demand ",c," max-av "))){")) + (gen #t "if(C_unlikely(!C_demand(C_calculate_demand((c-" n ")*C_SIZEOF_PAIR +" demand ",c," max-av ")))){")) (else (unless direct (gen #t "C_word *a;")) (when (and direct (not unsafe) (not disable-stack-overflow-checking)) @@ -860,10 +860,10 @@ ;; The interrupt handler may fill the stack, so we only ;; check for an interrupt when the procedure is restartable (when insert-timer-checks (gen #t "C_check_for_interrupt;")) - (gen #t "if(!C_demand(C_calculate_demand(" + (gen #t "if(C_unlikely(!C_demand(C_calculate_demand(" demand (if customizable ",0," ",c,") - max-av "))){")) + max-av ")))){")) (else (gen #\{))))) (cond ((and (not (eq? 'toplevel id)) (not direct)) diff --git a/chicken.h b/chicken.h index 733958d..4ae93e0 100644 --- a/chicken.h +++ b/chicken.h @@ -245,6 +245,8 @@ void *alloca (); #endif #if defined(__GNUC__) || defined(__INTEL_COMPILER) +# define C_unlikely(x) __builtin_expect((x), 0) +# define C_likely(x) __builtin_expect((x), 1) # ifndef __cplusplus # define C_cblock ({ # define C_cblockend }) @@ -261,6 +263,11 @@ void *alloca (); # endif #elif defined(__WATCOMC__) # define C_ccall __cdecl +# define C_unlikely(x) (x) +# define C_likely(x) (x) +#else +# define C_unlikely(x) (x) +# define C_likely(x) (x) #endif #ifndef C_cblock -- 2.1.4