diff options
author | Dmitry Stogov <dmitry@zend.com> | 2014-08-21 12:02:48 +0400 |
---|---|---|
committer | Dmitry Stogov <dmitry@zend.com> | 2014-08-21 12:02:48 +0400 |
commit | e9439f62e5e3696728accf21bf90b0c07fbed4df (patch) | |
tree | c1554dee44c8491e6defc220f1a6979e0c73b7e2 /ext/spl | |
parent | 92ac625b94e3dc79a0c9def934c61d83397afe81 (diff) | |
download | php-git-e9439f62e5e3696728accf21bf90b0c07fbed4df.tar.gz |
Avoid reallocation and copying
Diffstat (limited to 'ext/spl')
-rw-r--r-- | ext/spl/php_spl.c | 13 | ||||
-rw-r--r-- | ext/spl/php_spl.h | 2 | ||||
-rw-r--r-- | ext/spl/spl_observer.c | 13 |
3 files changed, 9 insertions, 19 deletions
diff --git a/ext/spl/php_spl.c b/ext/spl/php_spl.c index f9a642410f..5636a5f989 100644 --- a/ext/spl/php_spl.c +++ b/ext/spl/php_spl.c @@ -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); } /* }}} */ diff --git a/ext/spl/php_spl.h b/ext/spl/php_spl.h index b186255f57..c2b1837e8d 100644 --- a/ext/spl/php_spl.h +++ b/ext/spl/php_spl.h @@ -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 */ diff --git a/ext/spl/spl_observer.c b/ext/spl/spl_observer.c index bdfff35506..1eeaf95724 100644 --- a/ext/spl/spl_observer.c +++ b/ext/spl/spl_observer.c @@ -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)); } /* }}} */ |