The problem statement

The sequential version:

julia> function unstable_kernel(x)
           if x <= 1
               return 1
           else
               return π
           end
       end
unstable_kernel (generic function with 1 method)


julia> function naive_loop()
           res = 0
           for _ = 1:10^5
               res += unstable_kernel(rand())
           end
           res
       end

julia> @benchmark naive_loop()
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
 Range (min … max):  181.165 μs … 375.824 μs  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     185.362 μs               ┊ GC (median):    0.00%
 Time  (mean ± σ):   186.691 μs ±   7.935 μs  ┊ GC (mean ± σ):  0.00% ± 0.00%

     ▅▇▂█▂▆▄▃▂▂▁▂▁▂▃▂▁                                          ▂
  ▅▄▁█████████████████▆▆▅▅▄▅▁▄▄▄▅▄▄▅▄▅▃▃▄▁▁▄▁▃▁▁▃▃▃▁▁▁▄▃▁▁▄▄▄▃▃ █
  181 μs        Histogram: log(frequency) by time        221 μs <

Now imagine for reason due to values possibly allowed to this kernel, you know that in some function scope you either know the possible range of x or know you can cast it to some type, say Float64.

You can introduced a function barrier:

kernel_barrier(x)::Int64 = unstable_kernel(x)


julia> function gated_loop()
           res = 0
           for _ = 1:10^5
               res += kernel_barrier(rand())
           end
           res
       end

julia> @benchmark gated_loop()
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
 Range (min … max):  113.265 μs … 209.007 μs  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     116.562 μs               ┊ GC (median):    0.00%
 Time  (mean ± σ):   117.124 μs ±   3.576 μs  ┊ GC (mean ± σ):  0.00% ± 0.00%

               ▆▄  █  ▅▄   ▁▁▁ ▁▁                               ▁
  ▅▁▁▁▁▁▁▁▁▁▆▁▁██▇▃██▇███▇███████████▇▇▇▆▅▇▇▇▇▇█▆▆▆▅▅▅▅▅▆▅▇▇▅▇█ █
  113 μs        Histogram: log(frequency) by time        125 μs <