]> granicus.if.org Git - php/commitdiff
Optimize integer in_array with strict=true
authorNikita Popov <nikita.ppv@gmail.com>
Fri, 5 Jul 2019 09:15:13 +0000 (11:15 +0200)
committerNikita Popov <nikita.ppv@gmail.com>
Fri, 5 Jul 2019 09:20:29 +0000 (11:20 +0200)
It doesn't make sense that using in_array with strict=false is
much faster for this case, due to lack of a specialized codepath.

ext/standard/array.c

index 068acf33bc799beabace8beec5c015f1b505faa0..f5582baf33be9d23cb7f9ebd170e261088340fcb 100644 (file)
@@ -1554,21 +1554,39 @@ static inline void php_search_array(INTERNAL_FUNCTION_PARAMETERS, int behavior)
        ZEND_PARSE_PARAMETERS_END();
 
        if (strict) {
-               ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(array), num_idx, str_idx, entry) {
-                       ZVAL_DEREF(entry);
-                       if (fast_is_identical_function(value, entry)) {
-                               if (behavior == 0) {
-                                       RETURN_TRUE;
-                               } else {
-                                       if (str_idx) {
-                                               RETVAL_STR_COPY(str_idx);
+               if (Z_TYPE_P(value) == IS_LONG) {
+                       ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(array), num_idx, str_idx, entry) {
+                               ZVAL_DEREF(entry);
+                               if (Z_TYPE_P(entry) == IS_LONG && Z_LVAL_P(entry) == Z_LVAL_P(value)) {
+                                       if (behavior == 0) {
+                                               RETURN_TRUE;
                                        } else {
-                                               RETVAL_LONG(num_idx);
+                                               if (str_idx) {
+                                                       RETVAL_STR_COPY(str_idx);
+                                               } else {
+                                                       RETVAL_LONG(num_idx);
+                                               }
+                                               return;
                                        }
-                                       return;
                                }
-                       }
-               } ZEND_HASH_FOREACH_END();
+                       } ZEND_HASH_FOREACH_END();
+               } else {
+                       ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(array), num_idx, str_idx, entry) {
+                               ZVAL_DEREF(entry);
+                               if (fast_is_identical_function(value, entry)) {
+                                       if (behavior == 0) {
+                                               RETURN_TRUE;
+                                       } else {
+                                               if (str_idx) {
+                                                       RETVAL_STR_COPY(str_idx);
+                                               } else {
+                                                       RETVAL_LONG(num_idx);
+                                               }
+                                               return;
+                                       }
+                               }
+                       } ZEND_HASH_FOREACH_END();
+               }
        } else {
                if (Z_TYPE_P(value) == IS_LONG) {
                        ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(array), num_idx, str_idx, entry) {