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

Simplify iteration over ranges in general #27161

Merged
merged 5 commits into from
May 25, 2018

Conversation

haampie
Copy link
Contributor

@haampie haampie commented May 18, 2018

This complements #27147 with a bit of refactoring of StepRange.

  • The step type of AbstractUnitRange is now identical to the element type
  • For StepRange{<:AbstractFloat}: assume that we cannot land on the boundary of the range exactly. Assume we can land on the boundary of the range exactly, so that we can always iterate over the full range inclusive without overflows.
  • LinRange and StepRangeLen iteration was identical, so they're now combined.

Also fixes a bug where one cannot iterate as follows:

s = 0
for i = typemin(Int) : 1 : typemax(Int)
  i == typemin(Int) + 10 && break
  s += 1
endx
@assert s == 10

@haampie haampie force-pushed the fix-steprange-iteration branch 2 times, most recently from 3739c5e to afb66b7 Compare May 19, 2018 07:37
@haampie
Copy link
Contributor Author

haampie commented May 20, 2018

Probably the specialization for AbstractFloat can be removed altogether.

> StepRange(1.0, 2.0, 3.0)
ERROR: ArgumentError: StepRange should not be used with floating point

I think it is safe to assume that we will always land exactly on the boundary of the range then? Will update this PR and simplify the iteration even more :).

Edit: done. PR is good to go if the tests are green now.

@haampie haampie force-pushed the fix-steprange-iteration branch 3 times, most recently from e551165 to b3e48e1 Compare May 20, 2018 20:30
@haampie haampie changed the title Simplify iteration over OrdinalRange Simplify iteration over ranges in general May 20, 2018
@haampie
Copy link
Contributor Author

haampie commented May 21, 2018

Just to be sure:
@nanosoldier runbenchmarks(ALL, vs = ":master")

@KristofferC
Copy link
Sponsor Member

@nanosoldier runbenchmarks(ALL, vs = ":master")

Need commit bit.

@KristofferC
Copy link
Sponsor Member

Hmm...

@@ -374,7 +374,7 @@ julia> step(range(2.5, stop=10.9, length=85))
```
"""
step(r::StepRange) = r.step
step(r::AbstractUnitRange) = 1
step(r::AbstractUnitRange{T}) where{T} = oneunit(T)
Copy link
Sponsor Member

@mbauman mbauman May 21, 2018

Choose a reason for hiding this comment

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

I think this is a good change, but could we add a ::T assertion here to ensure that we're in line with the step type we report to OrdinalRange?

Edit: whoops, sorry, that is oneunit! It is indeed documented to return T, which is good enough IMO. I had it backwards from one.

@haampie
Copy link
Contributor Author

haampie commented May 21, 2018

Ping @ararslan

@ararslan
Copy link
Member

There's nothing in the server log indicative of failure so I'm not sure what the problem was, but I restarted the server anyway.

@nanosoldier runbenchmarks(ALL, vs=":master")

@nanosoldier
Copy link
Collaborator

Your benchmark job has completed - possible performance regressions were detected. A full report can be found here. cc @ararslan

@haampie
Copy link
Contributor Author

haampie commented May 22, 2018

Seems like this is good to go then.

@mbauman
Copy link
Sponsor Member

mbauman commented May 22, 2018

I'm not too worried about a 20% regression in a deprecated method that's already dog-slow, but I am slightly concerned by the 315% regression in ["find", "findprev", "(\"Array{Bool,1}\", \"50-50\")"].

@haampie
Copy link
Contributor Author

haampie commented May 22, 2018

I verified locally that its equal (it generates the same @code_native)

> @benchmark perf_findprev(r) setup = (r = rand(Bool, 10000)) #0961ee3599
  median time:      55.806 μs (0.00% GC)
> @benchmark perf_findprev(r) setup = (r = rand(Bool, 10000)) #eefa333d01
  median time:      55.798 μs (0.00% GC)

Also I think @inbounds or at least @propagate_inbounds is missing in findprev, but that's another issue.

@mbauman
Copy link
Sponsor Member

mbauman commented May 25, 2018

Great! I confirmed that locally, too. This is good to go.

@mbauman mbauman merged commit 6a0ac1b into JuliaLang:master May 25, 2018
@haampie haampie deleted the fix-steprange-iteration branch May 25, 2018 17:02
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.

None yet

5 participants