Skip to content

ext/standard: Optimize result-row construction in array_map() with a null callback - #23809

Open
LamentXU123 wants to merge 3 commits into
php:masterfrom
LamentXU123:opt---------
Open

LamentXU123 wants to merge 3 commits into
php:masterfrom
LamentXU123:opt---------

Conversation

@LamentXU123

Copy link
Copy Markdown
Member

When array_map() receives a null callback and multiple arrays, each result row has a known size and consecutive integer keys.

In this case we just init each value once. No regressions intended.

Input reduction
2 arrays × 256 elements 11.1%
4 arrays × 256 elements 21.1%
16 arrays × 256 elements 41.2%

Comment thread ext/standard/array.c Outdated
if (!ZEND_FCI_INITIALIZED(fci)) {
uint32_t *array_pos = ecalloc(n_arrays, sizeof(HashPosition));
zval zv;
zval zv = {0};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks a bit suspicious, why did you need to do this?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from what I observe it is assigned in every path indeed.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks a bit suspicious, why did you need to do this?

It's because later in ZEND_HASH_FILL_ADD copy the value of zv and it's not initialized. So GCC warns on it.

But anyways, we don't need to consider this now.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I was curious because it didn't seem necessary and felt like something uninitialized ended up somewhere. But apparently it's just the compiler not being smart enough.

Comment thread ext/standard/array.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can optimise that part too (i.e. return_value initialisation is optimised).

@devnexen devnexen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks ok from my pov

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants