关于php内核中 __builtin_expect 的解释

8 篇文章 0 订阅

Zend/zend_portability.h
#if PHP_HAVE_BUILTIN_EXPECT
# define EXPECTED(condition)   __builtin_expect(!!(condition), 1)
# define UNEXPECTED(condition) __builtin_expect(!!(condition), 0)
#else
# define EXPECTED(condition)   (condition)
# define UNEXPECTED(condition) (condition)
#endif


main/php-config.h

/* Whether the compiler supports __builtin_expect */
#define PHP_HAVE_BUILTIN_EXPECT 1

解释:
# define EXPECTED(condition)   __builtin_expect(!!(condition), 1)
# define UNEXPECTED(condition) __builtin_expect(!!(condition), 0)

1 !!(condition)真假变量 非非得真的逻辑运算,以0或1来达到预判
2 if(EXPECTED(condition)) 等价于 if(condition)
  if(UNEXPECTED(condition)) 也等价于 if(condition)
3 __builtin_expect((condition),1) 表示 condition 的值为真的可能性更大。
  __builtin_expect((condition),0) 表示 condition 的值为假的可能性更大。
  编译器在编译过程中,会将可能性更大的代码紧跟着前面的代码,从而减少指令跳转。

例子:
Zend/zend_API.h
static zend_always_inline int zend_parse_arg_str(zval *arg, zend_string **dest, int check_null)
{
	if (EXPECTED(Z_TYPE_P(arg) == IS_STRING)) {
		*dest = Z_STR_P(arg);
	} else if (check_null && Z_TYPE_P(arg) == IS_NULL) {
		*dest = NULL;
	} else {
		return zend_parse_arg_str_slow(arg, dest);
	}
	return 1;
}

static zend_always_inline int zend_parse_arg_string(zval *arg, char **dest, size_t *dest_len, int check_null)
{
	zend_string *str;

	if (!zend_parse_arg_str(arg, &str, check_null)) {
		return 0;
	}
	if (check_null && UNEXPECTED(!str)) {
		*dest = NULL;
		*dest_len = 0;
	} else {
		*dest = str->val;
		*dest_len = str->len;
	}
	return 1;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值