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

searchsorted allocates unnecessarily for views #35529

Closed
robsmith11 opened this issue Apr 20, 2020 · 2 comments
Closed

searchsorted allocates unnecessarily for views #35529

robsmith11 opened this issue Apr 20, 2020 · 2 comments

Comments

@robsmith11
Copy link
Contributor

It should be possible for searchsorted to search a view of an array without allocating.

julia> a = [0]
1-element Array{Int64,1}:
 0

julia> @btime searchsorted($a, 0)
  14.404 ns (0 allocations: 0 bytes)
1:1

julia> @btime searchsorted(view($a,1:1), 0)
  37.277 ns (1 allocation: 48 bytes)
1:1

julia> @btime findfirst(x->x==0, view($a,1:1))
  5.833 ns (0 allocations: 0 bytes)
1

julia> versioninfo()
Julia Version 1.5.0-DEV.645
Commit c3fc36707c (2020-04-18 20:13 UTC)
Platform Info:
  OS: Linux (aarch64-unknown-linux-gnu)
  CPU: unknown
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-9.0.1 (ORCJIT, kryo)
@stev47
Copy link
Contributor

stev47 commented Apr 20, 2020

It's the view itself that allocates:

a = [0]
@btime av = view($a, 1:1) # 11.396 ns (1 allocation: 48 bytes)
@btime searchsorted($av, 0) # 10.577 ns (0 allocations: 0 bytes)

Thus a duplicate of #14955. Only in a few select cases the compiler is able to optimize the view away.

@robsmith11
Copy link
Contributor Author

Ah, I see. Thanks.

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

No branches or pull requests

2 participants