]> granicus.if.org Git - php/commitdiff
Avoid reallocation and copying
authorDmitry Stogov <dmitry@zend.com>
Thu, 21 Aug 2014 08:02:48 +0000 (12:02 +0400)
committerDmitry Stogov <dmitry@zend.com>
Thu, 21 Aug 2014 08:02:48 +0000 (12:02 +0400)
ext/spl/php_spl.c
ext/spl/php_spl.h
ext/spl/spl_observer.c

index f9a642410fc639c40b8eedfc94d1da5ef816f382..5636a5f9891c62f443f9b33d56c18d45c20c6571 100644 (file)
@@ -742,22 +742,18 @@ PHP_FUNCTION(spl_autoload_functions)
 PHP_FUNCTION(spl_object_hash)
 {
        zval *obj;
-       char hash[33];
 
        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "o", &obj) == FAILURE) {
                return;
        }
        
-       php_spl_object_hash(obj, hash TSRMLS_CC);
-       
-       RETURN_STRING(hash);
+       RETURN_NEW_STR(php_spl_object_hash(obj TSRMLS_CC));
 }
 /* }}} */
 
-PHPAPI void php_spl_object_hash(zval *obj, char *result TSRMLS_DC) /* {{{*/
+PHPAPI zend_string *php_spl_object_hash(zval *obj TSRMLS_DC) /* {{{*/
 {
        intptr_t hash_handle, hash_handlers;
-       char *hex;
 
        if (!SPL_G(hash_mask_init)) {
                if (!BG(mt_rand_is_seeded)) {
@@ -772,10 +768,7 @@ PHPAPI void php_spl_object_hash(zval *obj, char *result TSRMLS_DC) /* {{{*/
        hash_handle   = SPL_G(hash_mask_handle)^(intptr_t)Z_OBJ_HANDLE_P(obj);
        hash_handlers = SPL_G(hash_mask_handlers)^(intptr_t)Z_OBJ_HT_P(obj);
 
-       spprintf(&hex, 32, "%016lx%016lx", hash_handle, hash_handlers);
-
-       strlcpy(result, hex, 33);
-       efree(hex);
+       return strpprintf(32, "%016lx%016lx", hash_handle, hash_handlers);
 }
 /* }}} */
 
index b186255f57dfacfa3a78cca156ee90409709429d..c2b1837e8dc1fabb48daf31834fb21d4c1f0838f 100644 (file)
@@ -79,7 +79,7 @@ PHP_FUNCTION(class_parents);
 PHP_FUNCTION(class_implements);
 PHP_FUNCTION(class_uses);
 
-PHPAPI void php_spl_object_hash(zval *obj, char* md5str TSRMLS_DC);
+PHPAPI zend_string *php_spl_object_hash(zval *obj TSRMLS_DC);
 
 #endif /* PHP_SPL_H */
 
index bdfff355067bb3a46c093192063a3fd034900d22..1eeaf95724ed0426b4ce7f9fc57c485b0881fb13 100644 (file)
@@ -311,7 +311,7 @@ static HashTable* spl_object_storage_debug_info(zval *obj, int *is_temp TSRMLS_D
        spl_SplObjectStorageElement *element;
        HashTable *props;
        zval tmp, storage;
-       char md5str[33];
+       zend_string *md5str;
        zend_string *zname;
 
        *is_temp = 0;
@@ -330,14 +330,15 @@ static HashTable* spl_object_storage_debug_info(zval *obj, int *is_temp TSRMLS_D
                array_init(&storage);
 
                ZEND_HASH_FOREACH_PTR(&intern->storage, element) {
-                       php_spl_object_hash(&element->obj, md5str TSRMLS_CC);
+                       md5str = php_spl_object_hash(&element->obj TSRMLS_CC);
                        array_init(&tmp);
                        /* Incrementing the refcount of obj and inf would confuse the garbage collector.
                         * Prefer to null the destructor */
                        Z_ARRVAL_P(&tmp)->pDestructor = NULL;
                        add_assoc_zval_ex(&tmp, "obj", sizeof("obj") - 1, &element->obj);
                        add_assoc_zval_ex(&tmp, "inf", sizeof("inf") - 1, &element->inf);
-                       add_assoc_zval_ex(&storage, md5str, 32, &tmp);
+                       zend_hash_update(Z_ARRVAL(storage), md5str, &tmp);
+                       STR_RELEASE(md5str);
                } ZEND_HASH_FOREACH_END();
 
                zname = spl_gen_private_prop_name(spl_ce_SplObjectStorage, "storage", sizeof("storage")-1 TSRMLS_CC);
@@ -469,16 +470,12 @@ SPL_METHOD(SplObjectStorage, detach)
 SPL_METHOD(SplObjectStorage, getHash)
 {
        zval *obj;
-       char *hash;
 
        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "o", &obj) == FAILURE) {
                return;
        }
-
-       hash = emalloc(33);
-       php_spl_object_hash(obj, hash TSRMLS_CC);
        
-       RETVAL_STRING(hash);
+       RETURN_NEW_STR(php_spl_object_hash(obj TSRMLS_CC));
 
 } /* }}} */