Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Comment | File | Size | Author |
---|---|---|---|
#14 | 3087661-14.patch | 1.38 KB | andypost |
#14 | interdiff.txt | 1.12 KB | andypost |
#6 | speed.php_.txt | 702 bytes | jhodgdon |
Comments
Comment #2
andypostKinda it
Comment #3
jhodgdonLooks fine. Bot likes it. No downside.
Comment #4
jhodgdonParent issue was committed to 8.8. 8.9, and 9. This one probably should be too.
Comment #5
jhodgdon@andypost brought up in Slack:
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:
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.
2.
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.
Comment #6
jhodgdonI 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.
Comment #7
pwolanin CreditAttribution: pwolanin as a volunteer and at SciShield commentedI 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.
Comment #8
jhodgdonWell 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?
Comment #9
andypostUsing 7.3.8
Using php 7.3.10
Comment #10
jhodgdonInteresting. 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
Comment #11
pwolanin CreditAttribution: pwolanin as a volunteer and at SciShield commentedI think option a) is best if it's a public api. b) is fine too, especially for non-public. c) doesn't make sense
Comment #12
jhodgdonIt is not really a public API, is it? This issue is about modifying a protected method on NodeSearch.
Comment #13
kim.pepperMy preference is for option a)
Comment #14
andypostKinda it
Comment #15
kim.pepper+1 rtbc if green
Comment #16
jhodgdonLooks 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.
Comment #17
larowlanApologies 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?
Comment #18
jhodgdonUmmmm... Well, you already (4 days ago) committed #3075695: Move search_index() and related functions to a service, which changed the function signature:
This patch changes it further to
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.
Comment #19
larowlanoh! so this changes it back - and removes the minor BC break - perfect!
Comment #20
larowlanComment #23
larowlanCommitted 89e11e4 and pushed to 9.0.x. Thanks!
c/p to 8.9 and 8.8