-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Update nextind, prevind #31321
base: master
Are you sure you want to change the base?
Update nextind, prevind #31321
Conversation
The pull request is much appreciated but I think this one is going to be quite performance-sensitive so it's going to take some careful work to make sure that this doesn't cause anything to regress. |
I noticed that you had assigned it to yourself, so I close this PR? Or can you guide me? |
I'm a bit confused by why your change modifies the bounds checking logic. Why did you change that? This patch seems like a good and efficient way to implement this change: diff --git a/base/strings/basic.jl b/base/strings/basic.jl
index d7c9cb279f..56e6623f2b 100644
--- a/base/strings/basic.jl
+++ b/base/strings/basic.jl
@@ -458,7 +458,7 @@ prevind(s::AbstractString, i::Int) = prevind(s, i, 1)
function prevind(s::AbstractString, i::Int, n::Int)
n < 0 && throw(ArgumentError("n cannot be negative: $n"))
z = ncodeunits(s) + 1
- @boundscheck 0 < i ≤ z || throw(BoundsError(s, i))
+ @boundscheck (n != 0) ≤ i ≤ z || throw(BoundsError(s, i))
n == 0 && return thisind(s, i) == i ? i : string_index_err(s, i)
while n > 0 && 1 < i
@inbounds n -= isvalid(s, i -= 1)
@@ -518,7 +518,7 @@ nextind(s::AbstractString, i::Int) = nextind(s, i, 1)
function nextind(s::AbstractString, i::Int, n::Int)
n < 0 && throw(ArgumentError("n cannot be negative: $n"))
z = ncodeunits(s)
- @boundscheck 0 ≤ i ≤ z || throw(BoundsError(s, i))
+ @boundscheck 0 ≤ i ≤ z + (n == 0) || throw(BoundsError(s, i))
n == 0 && return thisind(s, i) == i ? i : string_index_err(s, i)
while n > 0 && i < z
@inbounds n -= isvalid(s, i += 1) Features that make it appealing:
Additional work needed to make the change ready for merging:
If you want to do all of that, that would be wonderful. |
Reason for tweaking the logic, I wanted to make sure, Basically, what you did, is what I planned to do, but I couldn't execute it. 😿
I would be glad to do that |
Great! Thanks for taking it on. |
Next step here: there are a bunch of test failures, they should be changed to match the new behavior (checking that each one is actually intended; from a quick perusal, they seem to be). While you're trying to get the tests to pass you probably want to run tests locally. You can do that by recompiling Julia and running |
Umm, how to resolve that |
No, that shouldn't happen. The new issue is the |
It's very tempting to just relax the bounds checks to allow any index between |
imo, intuitively |
Why are the tests for |
It's not a matter of a bottleneck—the compiler can clearly eliminate overhead from a bit of ±1 arithmetic. The point is that this is a case where |
Is the changes good to go for this PR? What should be the |
No, I need to think about this some more. |
Fixes #31319