-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Don't use array merge here. #17391
Don't use array merge here. #17391
Conversation
Instead of: create array merge new array into old array to create another array replace old array with newly merged array Just: push item onto old array
It is great, if you know that you do not have duplicates [EDIT] |
@ggppdk None of what you have said seems to apply to this situation at all. |
i only said 1 thing , i spoke of duplicates |
on what i agree with you, is that |
@ggppdk array_merge has nothing to do with duplicates unless you mean duplicate keys when using associative arrays. This function has nothing to do with associative arrays. |
For me function Off topic: |
yes, you were right and i was wrong, i remembered wrongly its behaviour ... i was thinking of its behaviour when keys are strings, in which case it replaces the values of the first array if same index exists in the second |
@csthomas for you, maybe the same. For PHP, not the same. Let's break down This all happens very fast but try doing it 10,000 times with ever increasing array sizes. When you do |
Indeed in this case we have numerical indexes and not string indexes (?) |
@okonomiyaki3000 Yes I understand it and whole PR very well. |
99% of the time i avoid using array_merge because of its unneed bad performance, especially when code will be called repeatedly, and that made me post wrong comment above This PR looks like a very good performance improvement |
The only question for me is, should we also change the rest of this function to be like: foreach ($elements as $element)
{
$this->elements[] = $element;
} I think we probably should, I just hope I'm not missing anything. In fact, it might be better to just rewrite the whole function as: public function append($elements)
{
foreach ((array) $elements as $element)
{
$this->elements[] = $element;
}
} Any opinions about it? |
Looking at it more carefully The change you have done so far seems 100% safe If you also want to change the case that an array of ($elements) will be appended Currently the class is written with ability to preserve non-numerical keys, |
I have tested this item ✅ successfully on eb7264a This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/17391. |
I have tested this item ✅ successfully on eb7264a This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/17391. |
RTC after two successful tests. |
* staging: (148 commits) Correcting non-escaped double quotes in en-GB.plg_sampledata_testing.ini (joomla#17455) Correct namespace reference (Fix joomla#17448) Correcting Jalali/Persian calendar popup (joomla#17432) Adding russian calendar language file (joomla#17443) Reset for dev Prepare 3.8 Beta release Fix covers tags Fix file paths Move library files to just libraries/src as it should be (joomla#17441) Add a default empty array for the session queue (joomla#16943) [3.8] Restructure version constants (joomla#16169) Adjusting copyright and versions and two remaining "sampledata" (joomla#17435) PHP 7.2 has branched, update Travis config to reflect PHP 7.2 count warning (joomla#16840) Enforce array for subform values (joomla#16733) System URL menu link (joomla#17419) Don't use array merge here. (joomla#17391) add the checked attribute (joomla#17336) [RFC] Mod sample data (joomla#7680) Rename Page to Menu Item (joomla#17409) ...
* staging: (148 commits) Correcting non-escaped double quotes in en-GB.plg_sampledata_testing.ini (joomla#17455) Correct namespace reference (Fix joomla#17448) Correcting Jalali/Persian calendar popup (joomla#17432) Adding russian calendar language file (joomla#17443) Reset for dev Prepare 3.8 Beta release Fix covers tags Fix file paths Move library files to just libraries/src as it should be (joomla#17441) Add a default empty array for the session queue (joomla#16943) [3.8] Restructure version constants (joomla#16169) Adjusting copyright and versions and two remaining "sampledata" (joomla#17435) PHP 7.2 has branched, update Travis config to reflect PHP 7.2 count warning (joomla#16840) Enforce array for subform values (joomla#16733) System URL menu link (joomla#17419) Don't use array merge here. (joomla#17391) add the checked attribute (joomla#17336) [RFC] Mod sample data (joomla#7680) Rename Page to Menu Item (joomla#17409) ...
Pull Request for Issue # .
Summary of Changes
Instead of:
create array
merge new array into old array to create another array
replace old array with newly merged array
Just:
push item onto old array
Testing Instructions
max_execution_time
is something sensible like 30 seconds.Expected result
Well, I expect the article to save properly and the page to reload without crashing.
Actual result
Most likely, the article will save but in the post-save indexing process there will be a timeout. This happens because we are looping over a huge array of tokens and adding each one to the
values
array of the query object. The query object handles this rather inefficiently by creating an array, merging it with an existing array to create a third array and then replacing the existing array with the new one when all it needed to do was a push. This gets very bad when the existingvalues
array is already very large.There is another half of this function which is when an array is passed to it instead of a string. This would probably be a lot better like:
Anybody agree?
Documentation Changes Required
nope