From: Dmitry Stogov Date: Thu, 5 Jun 2014 15:14:47 +0000 (+0400) Subject: Speedup string equality check X-Git-Tag: POST_PHPNG_MERGE~222 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1b8916886b5b3b88ebe016ea311951b21db93ddd;p=php Speedup string equality check --- diff --git a/Zend/zend_operators.c b/Zend/zend_operators.c index 0c9f530b6c..caf5e78a42 100644 --- a/Zend/zend_operators.c +++ b/Zend/zend_operators.c @@ -1724,15 +1724,19 @@ ZEND_API int compare_function(zval *result, zval *op1, zval *op2 TSRMLS_DC) /* { return SUCCESS; case TYPE_PAIR(IS_STRING, IS_STRING): + if (Z_STR_P(op1) == Z_STR_P(op2)) { + ZVAL_LONG(result, 0); + return SUCCESS; + } zendi_smart_strcmp(result, op1, op2); return SUCCESS; case TYPE_PAIR(IS_NULL, IS_STRING): - ZVAL_LONG(result, zend_binary_strcmp("", 0, Z_STRVAL_P(op2), Z_STRLEN_P(op2))); + ZVAL_LONG(result, Z_STRLEN_P(op2) == 0 ? 0 : -1); return SUCCESS; case TYPE_PAIR(IS_STRING, IS_NULL): - ZVAL_LONG(result, zend_binary_strcmp(Z_STRVAL_P(op1), Z_STRLEN_P(op1), "", 0)); + ZVAL_LONG(result, Z_STRLEN_P(op1) == 0 ? 0 : 1); return SUCCESS; case TYPE_PAIR(IS_OBJECT, IS_NULL): diff --git a/Zend/zend_operators.h b/Zend/zend_operators.h index 1192e111c6..b952b9889e 100644 --- a/Zend/zend_operators.h +++ b/Zend/zend_operators.h @@ -830,6 +830,21 @@ static zend_always_inline int fast_equal_check_function(zval *result, zval *op1, } else if (EXPECTED(Z_TYPE_P(op2) == IS_LONG)) { return Z_DVAL_P(op1) == ((double)Z_LVAL_P(op2)); } + } else if (EXPECTED(Z_TYPE_P(op1) == IS_STRING)) { + if (EXPECTED(Z_TYPE_P(op2) == IS_STRING)) { + if (Z_STR_P(op1) == Z_STR_P(op2)) { + return 1; + } else if (Z_STRVAL_P(op1)[0] > '9' || Z_STRVAL_P(op2)[0] > '9') { + if (Z_STRLEN_P(op1) != Z_STRLEN_P(op2)) { + return 0; + } else { + 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; + } + } } compare_function(result, op1, op2 TSRMLS_CC); return Z_LVAL_P(result) == 0; @@ -853,6 +868,25 @@ static zend_always_inline void fast_equal_function(zval *result, zval *op1, zval ZVAL_BOOL(result, Z_DVAL_P(op1) == ((double)Z_LVAL_P(op2))); return; } + } else if (EXPECTED(Z_TYPE_P(op1) == IS_STRING)) { + if (EXPECTED(Z_TYPE_P(op2) == IS_STRING)) { + if (Z_STR_P(op1) == Z_STR_P(op2)) { + ZVAL_TRUE(result); + return; + } else if (Z_STRVAL_P(op1)[0] > '9' || Z_STRVAL_P(op2)[0] > '9') { + if (Z_STRLEN_P(op1) != Z_STRLEN_P(op2)) { + ZVAL_FALSE(result); + return; + } else { + ZVAL_BOOL(result, memcmp(Z_STRVAL_P(op1), Z_STRVAL_P(op2), Z_STRLEN_P(op1)) == 0); + return; + } + } else { + zendi_smart_strcmp(result, op1, op2); + ZVAL_BOOL(result, Z_LVAL_P(result) == 0); + return; + } + } } compare_function(result, op1, op2 TSRMLS_CC); ZVAL_BOOL(result, Z_LVAL_P(result) == 0); @@ -876,6 +910,25 @@ static zend_always_inline void fast_not_equal_function(zval *result, zval *op1, ZVAL_BOOL(result, Z_DVAL_P(op1) != ((double)Z_LVAL_P(op2))); return; } + } else if (EXPECTED(Z_TYPE_P(op1) == IS_STRING)) { + if (EXPECTED(Z_TYPE_P(op2) == IS_STRING)) { + if (Z_STR_P(op1) == Z_STR_P(op2)) { + ZVAL_FALSE(result); + return; + } else if (Z_STRVAL_P(op1)[0] > '9' || Z_STRVAL_P(op2)[0] > '9') { + if (Z_STRLEN_P(op1) != Z_STRLEN_P(op2)) { + ZVAL_TRUE(result); + return; + } else { + ZVAL_BOOL(result, memcmp(Z_STRVAL_P(op1), Z_STRVAL_P(op2), Z_STRLEN_P(op1)) != 0); + return; + } + } else { + zendi_smart_strcmp(result, op1, op2); + ZVAL_BOOL(result, Z_LVAL_P(result) != 0); + return; + } + } } compare_function(result, op1, op2 TSRMLS_CC); ZVAL_BOOL(result, Z_LVAL_P(result) != 0);