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
random: Do not use ZVAL_DUP in Randomizer::shuffleArray()
PHP Internals Book says:

> The ZVAL_DUP macro is similar to ZVAL_COPY, but will duplicate arrays, rather
> than just incrementing their refcount. If you are using this macro, you are
> almost certainly doing something very wrong.

Replace this by an explicit call to `zend_array_dup()`, as done in
`php_array_diff()`. Besides being more explicit in what is happening, this
likely also results in better assembly.
  • Loading branch information
TimWolla committed Sep 26, 2024
commit 1b9c6324ccd2fb3ee303f2add7fba1036a92dc46
2 changes: 1 addition & 1 deletion ext/random/randomizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ PHP_METHOD(Random_Randomizer, shuffleArray)
Z_PARAM_ARRAY(array)
ZEND_PARSE_PARAMETERS_END();

ZVAL_DUP(return_value, array);
RETVAL_ARR(zend_array_dup(Z_ARRVAL_P(array)));
Copy link
Member

Choose a reason for hiding this comment

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

Is there a particular reason for not using SEPARATE_ARRAY()? This would avoid a duplication when it's not stricly necessary, e.g.:

$r->shuffleArray(range(0,10));

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you have a specific suggestion as how to adjust the code to leverage SEPARATE_ARRAY? My attempts all lead to either memory unsafety or SEPARATE_ARRAY not providing a value-add.

Copy link
Member

Choose a reason for hiding this comment

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

You are right indeed, there SEPARATE_ARRAY would not make sense here.

if (!php_array_data_shuffle(randomizer->engine, return_value)) {
RETURN_THROWS();
}
Expand Down
Loading