Problem/Motivation

In #3087407-9: Search indexing calls are inefficient pointed that pass by reference could be removed

Proposed resolution

Update words withut passing by reference

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

andypost created an issue. See original summary.

andypost’s picture

Status: Active » Needs review
FileSize
1.32 KB

Kinda it

jhodgdon’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Quick fix

Looks fine. Bot likes it. No downside.

jhodgdon’s picture

Version: 9.0.x-dev » 8.8.x-dev

Parent issue was committed to 8.8. 8.9, and 9. This one probably should be too.

jhodgdon’s picture

Status: Reviewed & tested by the community » Needs review

@andypost brought up in Slack:

Not sure that cloning array is more efficient comparing to passing by reference

I think the reason this was suggested by @larowlan is related to his comment when suggesting pass-by-reference be removed from the "grandparent" issue #3075695: Move search_index() and related functions to a service:

Is there a reason why we can't make this a boolean, instead of a pass-by-reference, which is a bit of a code smell.

So, there are two considerations:
a) Do we for some reason not like pass-by-reference ("code smell")?
b) Do we think that pass-by-reference is more/less efficient than returning an array and doing array addition?

So the two patterns are:
1.

$foo += return_some_array_values($bar);

2.

add_in_the_array_values($bar, &$foo);

Which is actually better, less smelly, and more efficient? If 2 is better, we should go back to the grandparent issue and put back in the pass by reference. If 1 is better, we should mark this one as RTBC.

jhodgdon’s picture

FileSize
702 bytes

I ran a quick test with the attached PHP script. The pass-by-reference pattern is slightly faster than the plus-equals pattern -- about 15%. I'm not sure we care about efficiency, but that is what my test says. Not sure how it scales.

pwolanin’s picture

I would expect the by-ref version is slightly faster since you are only updating one array, vs doing the updates 2x.

I don't have a strong feeling about which is "cleaner". I think the current code is really fine since this is all happening within the one object instance.

jhodgdon’s picture

Well the other question is should we go back to #3075695: Move search_index() and related functions to a service and put the pass-by-reference into the API, as was suggested by earlier patches. But maybe we don't want a pass-by-reference in an API function like that?

andypost’s picture

Using 7.3.8

By reference time: 0.060431957244873
Plus equals time: 0.04203200340271

Using php 7.3.10

/var/www/html/web $ php speed.php_.txt
By reference time: 0.038882970809937
Plus equals time: 0.043861150741577
/var/www/html/web $ php speed.php_.txt
By reference time: 0.040800094604492
Plus equals time: 0.042826890945435
jhodgdon’s picture

Status: Needs review » Needs work

Interesting. So the relative speed may depend on which version you run on. It's never very much difference anyway -- we should probably just go by coding style preference?

In which case, I think the patch is probably a good idea, since it mirrors what the search API is doing.

However, it doesn't seem quite right, as it is. The $words array is passed into the new method, modified there, and returned. But then the return value of $this->indexNode() is added to the previous $words array.

So we should either:
a) Not pass $words in to indexNode() -- similar to what the search API is doing -- and use $words += return value
b) Pass $words in to indexNode() by reference (no patch, the current code)
c) Pass $words in by value, modify it there, and use $words = return value

pwolanin’s picture

I think option a) is best if it's a public api. b) is fine too, especially for non-public. c) doesn't make sense

jhodgdon’s picture

It is not really a public API, is it? This issue is about modifying a protected method on NodeSearch.

kim.pepper’s picture

My preference is for option a)

andypost’s picture

Status: Needs work » Needs review
FileSize
1.12 KB
1.38 KB

Kinda it

kim.pepper’s picture

+1 rtbc if green

jhodgdon’s picture

Status: Needs review » Reviewed & tested by the community

Looks good. Let's get it in before 8.8.0, to go with #3087407: Search indexing calls are inefficient, which already changed this method anyway.

larowlan’s picture

Apologies for the nuisance, but I think we can't change the signature here, in case of sub-classes (yes protected methods are internal etc but we should still care about the minor upgrade path) - see https://3v4l.org/tVjks

In light of that, I'm inclined to mark this closed won't fix - I don't think its worth the effort/disruption - sorry 😬

What do people think?

jhodgdon’s picture

Ummmm... Well, you already (4 days ago) committed #3075695: Move search_index() and related functions to a service, which changed the function signature:

-  protected function indexNode(NodeInterface $node) {
+  protected function indexNode(NodeInterface $node, array &$words) {

This patch changes it further to

+   * @return array
+   *   An array of words to update after indexing.
    */
-  protected function indexNode(NodeInterface $node, array &$words) {
+  protected function indexNode(NodeInterface $node) {

Which is actually closer to what it was, although there is a new contract that the caller needs to deal with the returned words.

So... this should be considered (I think) a quick update to what was already a BC break in that other issue.

larowlan’s picture

oh! so this changes it back - and removes the minor BC break - perfect!

larowlan’s picture

  • larowlan committed 89e11e4 on 9.0.x
    Issue #3087661 by andypost, jhodgdon, pwolanin: Remove pass by reference...

  • larowlan committed 3d1ed15 on 8.8.x
    Issue #3087661 by andypost, jhodgdon, pwolanin: Remove pass by reference...
  • larowlan committed ee6943c on 8.9.x
    Issue #3087661 by andypost, jhodgdon, pwolanin: Remove pass by reference...
larowlan’s picture

Status: Reviewed & tested by the community » Fixed

Committed 89e11e4 and pushed to 9.0.x. Thanks!

c/p to 8.9 and 8.8

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.