<p dir="auto">It should be possible for <code class="notranslate">searchsorted</code> to search a view of an array without allocating.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="julia&gt; a = [0]
1-element Array{Int64,1}:
 0

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

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

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

julia&gt; 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)"><pre class="notranslate"><code class="notranslate">julia&gt; a = [0]
1-element Array{Int64,1}:
 0

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

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

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

julia&gt; 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)
</code></pre></div>
<p dir="auto">I couldn't find an open issue for this frequently-noted problem, so I'm filing this. Feel free to close if there's an earlier one.</p>
<p dir="auto">It's worth linking to <a href="https://groups.google.com/forum/#!topic/julia-users/6Z1kJ5LrSMU" rel="nofollow">https://groups.google.com/forum/#!topic/julia-users/6Z1kJ5LrSMU</a>, where it was pointed out that "faking" a view as <code class="notranslate">(A, R)</code>, where <code class="notranslate">R</code> is a <code class="notranslate">CartesianRange</code>, is currently the cleanest way to circumvent this problem (but can only handle the equivalent of <code class="notranslate">Int/UnitRange/Colon</code> indexes). Various solutions using <code class="notranslate">Ptr</code> have also been employed in the past, but of course those suffer from the standpoint of garbage-collection safety.</p>