Skip to content
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

Stop $this->validColumnNames array from growing and growing #8928

Conversation

jalogut
Copy link
Contributor

@jalogut jalogut commented Mar 17, 2017

Optimise customer import getValidColumnNames() method

  • Stop $this->validColumnNames array from growing and growing as its value is recursively updated with its own data several times.
  • Importing hundred of customers was affecting performance and even breaking due to memory limits.

…ue is recursively updated with its own data several times. Importing hundred of customers affect performance and even break due to memory limits
@magento-cicd2
Copy link
Contributor

magento-cicd2 commented Mar 17, 2017

CLA assistant check
All committers have signed the CLA.

@okorshenko
Copy link
Contributor

@jalogut thank you for your contribution. Could you please sign CLA?

return array_unique(
array_merge(
$this->validColumnNames,
$this->customerFields
Copy link
Contributor

Choose a reason for hiding this comment

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

Such implementation is better than before but still has linear computational complexity.

If you flip the arrays fields will be unique by design and you will be able to perform main operations with O(1) complexity.

So, use array_keys($array) to get values and $array1 + $array2 to combine two arrays.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry but I do not really understand. Where is the complexity here? in the array_unique? If so, we could remove it as the main problem was simply that the resulting array was saved over and over again into $this->validColumnNames. That way, just returning the array_merge will fix that.

Regarding your solution, it will be ok here but in the CustomerComposite.php we have to flip 4 arrays. If I understand it correctly that would be:

return array_keys(
             array_flip($this->validColumnNames) +
             array_flip($this->_customerAttributes) +
             array_flip($this->_addressAttributes) +
             array_flip($this->_customerEntity->getValidColumnNames())
 );

I find this a bit more complicated to understand than my current solution. Does it really make a difference in terms of performance? If so, I would not hesitate to edit my pull request.

Copy link
Contributor

@orlangur orlangur Mar 21, 2017

Choose a reason for hiding this comment

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

I'm not saying to call array_flip, I'm saying to flip arrays by themselves.

So, instead of ['column-name'] there will be ['column-name' => 1].

Where is the complexity here?

I'm saying about https://en.wikipedia.org/wiki/Time_complexity#Table_of_common_time_complexities

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, this wikipedia link is too complex for me. I understand what you mean about flipping the arrays themselves but that will make the code difficult to understand. It will also imply to make changes in several places where all this arrays are created/updated.
My goal was just to point out that saving recursively $this->validColumnNames including itself, creates an array that is twice bigger in every iteration.
How to fix that? there are several valid solutions including the 2 we are discussing. I just went for the simplest and most understandable but of course other are also valid.

@davidalger davidalger self-assigned this Mar 23, 2017
@davidalger davidalger added this to the March 2017 milestone Mar 23, 2017
@davidalger
Copy link
Member

@jalogut Thanks for signing the CLA and very good catch!

@davidalger davidalger self-requested a review March 23, 2017 15:43
@vrann
Copy link
Contributor

vrann commented Mar 25, 2017

@jalogut Thank you, merged!

@magento-team magento-team merged commit c6624cb into magento:develop Mar 25, 2017
magento-team pushed a commit that referenced this pull request Mar 25, 2017
@jalogut jalogut deleted the optimise-customer-import-getValidColumnNames branch July 25, 2017 11:44
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.

7 participants