From 5fe5848f5e4cbd8cff39866ce90d9e77a8c95c2c Mon Sep 17 00:00:00 2001 From: Peter Bex Date: Tue, 27 Dec 2016 17:53:40 +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 | 5 +++++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/c-backend.scm b/c-backend.scm index 5431268..ca94014 100644 --- a/c-backend.scm +++ b/c-backend.scm @@ -855,10 +855,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;}" @@ -873,7 +873,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)) @@ -888,10 +888,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 0f619aa..76661a7 100644 --- a/chicken.h +++ b/chicken.h @@ -222,6 +222,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 }) @@ -236,6 +238,9 @@ void *alloca (); # if defined(__i386__) && !defined(__clang__) # define C_regparm __attribute__ ((regparm(3))) # endif +#else +# define C_unlikely(x) (x) +# define C_likely(x) (x) #endif #ifndef C_cblock -- 2.1.4