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

Mini-optimization to getElementAtOrBefore #1618

Merged
merged 1 commit into from
Jun 26, 2023

Conversation

TimFelixBeyer
Copy link
Contributor

Replaces an unnecessary sort with max and simplifies the remaining logic of the function.

Replaces an unnecessary sort with max and simplifies the remaining logic of the function.
@coveralls
Copy link

Coverage Status

coverage: 92.989% (-0.002%) from 92.991% when pulling ef2fb55 on TimFelixBeyer:patch-10 into f771fa7 on cuthbertLab:master.

@mscuthbert
Copy link
Member

Hi: the max is approved but some of the other logic changed--before if span was zero it was also added to candidates regardless of nearestTailSpan. Can you justify the change? Thanks!

(I think that the TODO caution is likely obsolete after the inclusion of sortTuple but will need to go back in the code to be sure)

@TimFelixBeyer
Copy link
Contributor Author

TimFelixBeyer commented Jun 26, 2023

Sure:
First, since nearestTrailSpan is always >= 0, we will still always add candidates that have a span of 0 (either creating a new candidates list or appending to the existing one).
Secondly, only candidates with the lowest span can end up as the final candidate, making it ok to discard all previous candidates if we find one with a lower span.
Therefore the logic actually remains unchanged as far as I can tell.
I agree regarding the todo, but I don’t have the time to investigate that right now, so I just left it in to be safe.
Let me know if that clears it up!

@mscuthbert
Copy link
Member

mscuthbert commented Jun 26, 2023

I did the initial review on my phone where I couldn't reread all the logic. Now that I do, I understand why these are equivalent, we could actually cut out one more if to simply the logic just to one <= because setting nearestTrailSpan = span in the case of equal is faster than doing two equality comparisons, but none of this really matters, since we really should be switching this to either trees or using a bisect search on _elements (if sorted) -- a lot of this code is still from the time that autosort was opt-in rather than opt-out (and turning it off was supposed to not have hugely negative consequences -- now it is allowed to).

The other possible improvement is to use .iter(restoreActiveSites=False) and only set the activeSite on the element that is returned, and if the stream is sorted to break once it's found the best candidate. I'll merge this and make a second PR for that.

Thank you!

@mscuthbert mscuthbert merged commit 21b6698 into cuthbertLab:master Jun 26, 2023
@TimFelixBeyer TimFelixBeyer deleted the patch-10 branch June 28, 2023 00:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants