Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion ext/standard/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -1026,7 +1026,11 @@ PHPAPI void php_implode(const zend_string *glue, HashTable *pieces, zval *return
}

cptr -= ZSTR_LEN(glue);
memcpy(cptr, ZSTR_VAL(glue), ZSTR_LEN(glue));
if (ZSTR_LEN(glue) == 1) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is in loop, is ZSTR_LEN(glue) guaranteed to be evaluated only once?

What about moving the condition outside the loop, ie. "duplicating" the loop body?

Also what about the case when the glue is empty string? (we use that strongly in https://github.com/atk4/ui/blob/6.0.0/src/HtmlTemplate.php#L570 - with 10k calls per webpage request easily)

Copy link
Member

Choose a reason for hiding this comment

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

This is in loop, is ZSTR_LEN(glue) guaranteed to be evaluated only once?

No. But chances are that it doesn't matter if this is in a variable or not because you'll need a load from a spill slot in the variable case most likely.

*cptr = ZSTR_VAL(glue)[0];
} else {
memcpy(cptr, ZSTR_VAL(glue), ZSTR_LEN(glue));
}
}

free_alloca(strings, use_heap);
Expand Down