# Optimizing array_reverse for packed hash tables (when preserve_keys=false)

- Thread: https://externals.io/message/94727
- Original archive: https://news-web.php.net/php.internals/94727
- Started by Benjamin Coutu on 2016-07-27T14:42:00+00:00
- Messages: 1
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #94727 — Benjamin Coutu — 2016-07-27T14:42:00+00:00

URL: https://externals.io/message/94727#94727

Hello Xinchen,

Analogue to the array_slice implementation, the array_reverse function could also be optimized for packed hash tables when preserve_keys is false (most common).
The following patch should do just that:

if (!preserve_keys && (Z_ARRVAL_P(input)->u.flags & HASH_FLAG_PACKED)) {
	zend_hash_real_init(Z_ARRVAL_P(return_value), 1);
	
	ZEND_HASH_FILL_PACKED(Z_ARRVAL_P(return_value)) {
		ZEND_HASH_REVERSE_FOREACH_VAL(Z_ARRVAL_P(input), entry) {
			ZEND_HASH_FILL_ADD(entry);
			zval_add_ref(entry);
		} ZEND_HASH_FOREACH_END();
	} ZEND_HASH_FILL_END();
}

Please let me know your thoughts.

Thanks,

Ben

-- 

Bejamin Coutu
ben.coutu@zeyos.com

ZeyOS, Inc.
http://www.zeyos.com
