]> granicus.if.org Git - php/commitdiff
Speedup string equality check
authorDmitry Stogov <dmitry@zend.com>
Thu, 5 Jun 2014 15:14:47 +0000 (19:14 +0400)
committerDmitry Stogov <dmitry@zend.com>
Thu, 5 Jun 2014 15:14:47 +0000 (19:14 +0400)
Zend/zend_operators.c
Zend/zend_operators.h

index 0c9f530b6cbdca45447798e371001b7bbd3cfee0..caf5e78a42dd68a02d58466837d9bb36bcf09571 100644 (file)
@@ -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):
index 1192e111c612faee96eb76859de50e0f23731958..b952b9889e7c0efc25963c6759825d6bbe90c982 100644 (file)
@@ -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);