From: Xinchen Hui <laruence@php.net>
Date: Mon, 22 Dec 2014 03:35:25 +0000 (-0500)
Subject: Micro optimizations
X-Git-Tag: PRE_PHP7_REMOVALS~35^2~48
X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=201e1b8a8d22b244b4e22d239db55ea85ccc6983;p=php

Micro optimizations
---

diff --git a/Zend/zend_operators.c b/Zend/zend_operators.c
index 23f3ddcf7f..9688e95414 100644
--- a/Zend/zend_operators.c
+++ b/Zend/zend_operators.c
@@ -467,7 +467,7 @@ ZEND_API void _convert_to_string(zval *op ZEND_FILE_LINE_DC) /* {{{ */
 			break;
 		case IS_RESOURCE: {
 			char buf[sizeof("Resource id #") + MAX_LENGTH_OF_LONG];
-			int len = snprintf(buf, sizeof(buf), "Resource id #" ZEND_LONG_FMT, Z_RES_HANDLE_P(op));
+			int len = snprintf(buf, sizeof(buf), "Resource id #" ZEND_LONG_FMT, (zend_long)Z_RES_HANDLE_P(op));
 			ZVAL_NEW_STR(op, zend_string_init(buf, len, 0));
 			break;
 		}
@@ -742,7 +742,7 @@ try_again:
 			char buf[sizeof("Resource id #") + MAX_LENGTH_OF_LONG];
 			int len;
 
-			len = snprintf(buf, sizeof(buf), "Resource id #" ZEND_LONG_FMT, Z_RES_HANDLE_P(op));
+			len = snprintf(buf, sizeof(buf), "Resource id #" ZEND_LONG_FMT, (zend_long)Z_RES_HANDLE_P(op));
 			return zend_string_init(buf, len, 0);
 		}
 		case IS_LONG: {
@@ -1815,7 +1815,7 @@ ZEND_API int compare_function(zval *result, zval *op1, zval *op2) /* {{{ */
 					ZVAL_LONG(result, 0);
 					return SUCCESS;
 				}
-				zendi_smart_strcmp(result, op1, op2);
+				ZVAL_LONG(result, zendi_smart_strcmp(op1, op2));
 				return SUCCESS;
 
 			case TYPE_PAIR(IS_NULL, IS_STRING):
@@ -2539,15 +2539,15 @@ ZEND_API int zend_binary_zval_strncasecmp(zval *s1, zval *s2, zval *s3) /* {{{ *
 }
 /* }}} */
 
-ZEND_API void zendi_smart_strcmp(zval *result, zval *s1, zval *s2) /* {{{ */
+ZEND_API zend_long zendi_smart_strcmp(zval *s1, zval *s2) /* {{{ */
 {
 	int ret1, ret2;
 	int oflow1, oflow2;
 	zend_long lval1 = 0, lval2 = 0;
 	double dval1 = 0.0, dval2 = 0.0;
 
-	if ((ret1=is_numeric_string_ex(Z_STRVAL_P(s1), Z_STRLEN_P(s1), &lval1, &dval1, 0, &oflow1)) &&
-		(ret2=is_numeric_string_ex(Z_STRVAL_P(s2), Z_STRLEN_P(s2), &lval2, &dval2, 0, &oflow2))) {
+	if ((ret1 = is_numeric_string_ex(Z_STRVAL_P(s1), Z_STRLEN_P(s1), &lval1, &dval1, 0, &oflow1)) &&
+		(ret2 = is_numeric_string_ex(Z_STRVAL_P(s2), Z_STRLEN_P(s2), &lval2, &dval2, 0, &oflow2))) {
 #if ZEND_ULONG_MAX == 0xFFFFFFFF
 		if (oflow1 != 0 && oflow1 == oflow2 && dval1 - dval2 == 0. &&
 			((oflow1 == 1 && dval1 > 9007199254740991. /*0x1FFFFFFFFFFFFF*/)
@@ -2559,18 +2559,16 @@ ZEND_API void zendi_smart_strcmp(zval *result, zval *s1, zval *s2) /* {{{ */
 			 * double comparison may have resulted in crucial accuracy lost */
 			goto string_cmp;
 		}
-		if ((ret1==IS_DOUBLE) || (ret2==IS_DOUBLE)) {
-			if (ret1!=IS_DOUBLE) {
+		if ((ret1 == IS_DOUBLE) || (ret2 == IS_DOUBLE)) {
+			if (ret1 != IS_DOUBLE) {
 				if (oflow2) {
 					/* 2nd operand is integer > LONG_MAX (oflow2==1) or < LONG_MIN (-1) */
-					ZVAL_LONG(result, -1 * oflow2);
-					return;
+					return -1 * oflow2;
 				}
 				dval1 = (double) lval1;
-			} else if (ret2!=IS_DOUBLE) {
+			} else if (ret2 != IS_DOUBLE) {
 				if (oflow1) {
-					ZVAL_LONG(result, oflow1);
-					return;
+					return oflow1;
 				}
 				dval2 = (double) lval2;
 			} else if (dval1 == dval2 && !zend_finite(dval1)) {
@@ -2578,15 +2576,16 @@ ZEND_API void zendi_smart_strcmp(zval *result, zval *s1, zval *s2) /* {{{ */
 				 * so a numeric comparison would be inaccurate */
 				goto string_cmp;
 			}
-			Z_DVAL_P(result) = dval1 - dval2;
-			ZVAL_LONG(result, ZEND_NORMALIZE_BOOL(Z_DVAL_P(result)));
+			dval1 = dval1 - dval2;
+			return ZEND_NORMALIZE_BOOL(dval1);
 		} else { /* they both have to be long's */
-			ZVAL_LONG(result, lval1 > lval2 ? 1 : (lval1 < lval2 ? -1 : 0));
+			return lval1 > lval2 ? 1 : (lval1 < lval2 ? -1 : 0);
 		}
 	} else {
+		int strcmp_ret;
 string_cmp:
-		Z_LVAL_P(result) = zend_binary_strcmp(Z_STRVAL_P(s1), Z_STRLEN_P(s1), Z_STRVAL_P(s2), Z_STRLEN_P(s2));
-		ZVAL_LONG(result, ZEND_NORMALIZE_BOOL(Z_LVAL_P(result)));
+		strcmp_ret = zend_binary_strcmp(Z_STRVAL_P(s1), Z_STRLEN_P(s1), Z_STRVAL_P(s2), Z_STRLEN_P(s2));
+		return ZEND_NORMALIZE_BOOL(strcmp_ret);
 	}
 }
 /* }}} */
diff --git a/Zend/zend_operators.h b/Zend/zend_operators.h
index b9baa5bd9a..57120228fb 100644
--- a/Zend/zend_operators.h
+++ b/Zend/zend_operators.h
@@ -337,7 +337,7 @@ ZEND_API int zend_binary_strncasecmp(const char *s1, size_t len1, const char *s2
 ZEND_API int zend_binary_strcasecmp_l(const char *s1, size_t len1, const char *s2, size_t len2);
 ZEND_API int zend_binary_strncasecmp_l(const char *s1, size_t len1, const char *s2, size_t len2, size_t length);
 
-ZEND_API void zendi_smart_strcmp(zval *result, zval *s1, zval *s2);
+ZEND_API zend_long zendi_smart_strcmp(zval *s1, zval *s2);
 ZEND_API void zend_compare_symbol_tables(zval *result, HashTable *ht1, HashTable *ht2);
 ZEND_API void zend_compare_arrays(zval *result, zval *a1, zval *a2);
 ZEND_API void zend_compare_objects(zval *result, zval *o1, zval *o2);
@@ -878,8 +878,9 @@ static zend_always_inline int fast_mod_function(zval *result, zval *op1, zval *o
 	return mod_function(result, op1, op2);
 }
 
-static zend_always_inline int fast_equal_check_function(zval *result, zval *op1, zval *op2)
+static zend_always_inline int fast_equal_check_function(zval *op1, zval *op2)
 {
+	zval result;
 	if (EXPECTED(Z_TYPE_P(op1) == IS_LONG)) {
 		if (EXPECTED(Z_TYPE_P(op2) == IS_LONG)) {
 			return Z_LVAL_P(op1) == Z_LVAL_P(op2);
@@ -903,13 +904,12 @@ static zend_always_inline int fast_equal_check_function(zval *result, zval *op1,
 					return memcmp(Z_STRVAL_P(op1), Z_STRVAL_P(op2), Z_STRLEN_P(op1)) == 0;
 				}
 			} else {
-				zendi_smart_strcmp(result, op1, op2);
-				return Z_LVAL_P(result) == 0;
+				return zendi_smart_strcmp(op1, op2) == 0;
 			}
 		}
 	}
-	compare_function(result, op1, op2);
-	return Z_LVAL_P(result) == 0;
+	compare_function(&result, op1, op2);
+	return Z_LVAL(result) == 0;
 }
 
 static zend_always_inline void fast_equal_function(zval *result, zval *op1, zval *op2)
@@ -944,8 +944,7 @@ static zend_always_inline void fast_equal_function(zval *result, zval *op1, zval
 					return;
 				}
 			} else {
-				zendi_smart_strcmp(result, op1, op2);
-				ZVAL_BOOL(result, Z_LVAL_P(result) == 0);
+				ZVAL_BOOL(result, zendi_smart_strcmp(op1, op2) == 0);
 				return;
 			}
 		}
@@ -986,8 +985,7 @@ static zend_always_inline void fast_not_equal_function(zval *result, zval *op1,
 					return;
 				}
 			} else {
-				zendi_smart_strcmp(result, op1, op2);
-				ZVAL_BOOL(result, Z_LVAL_P(result) != 0);
+				ZVAL_BOOL(result, zendi_smart_strcmp(op1, op2) != 0);
 				return;
 			}
 		}
diff --git a/ext/standard/array.c b/ext/standard/array.c
index d808c419b7..f845adafd1 100644
--- a/ext/standard/array.c
+++ b/ext/standard/array.c
@@ -1224,12 +1224,11 @@ PHP_FUNCTION(array_walk_recursive)
  * 0 = return boolean
  * 1 = return key
  */
-static zend_always_inline void php_search_array(INTERNAL_FUNCTION_PARAMETERS, int behavior) /* {{{ */
+static inline void php_search_array(INTERNAL_FUNCTION_PARAMETERS, int behavior) /* {{{ */
 {
 	zval *value,				/* value to check for */
 		 *array,				/* array to check in */
-		 *entry,				/* pointer to array entry */
-		  res;					/* comparison result */
+		 *entry;				/* pointer to array entry */
 	zend_ulong num_idx;
 	zend_string *str_idx;
 	zend_bool strict = 0;		/* strict comparison or not */
@@ -1248,6 +1247,7 @@ static zend_always_inline void php_search_array(INTERNAL_FUNCTION_PARAMETERS, in
 #endif
 
 	if (strict) {
+		zval res;					/* comparison result */
 		ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(array), num_idx, str_idx, entry) {
 			ZVAL_DEREF(entry);
 			is_identical_function(&res, value, entry);
@@ -1266,7 +1266,7 @@ static zend_always_inline void php_search_array(INTERNAL_FUNCTION_PARAMETERS, in
 		} ZEND_HASH_FOREACH_END();
 	} else {
 		ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(array), num_idx, str_idx, entry) {
-			if (fast_equal_check_function(&res, value, entry)) {
+			if (fast_equal_check_function(value, entry)) {
 				if (behavior == 0) {
 					RETURN_TRUE;
 				} else {