JeffreySarnoff / SaferIntegers.jl

These integer types use checked arithmetic, otherwise they are as system types.
MIT License
59 stars 10 forks source link

High overhead for SafeInt #14

Closed rgankema closed 3 years ago

rgankema commented 4 years ago

The benchmark in the readme suggest a fairly low overhead for using SafeInts, but unfortunately I'm seeing a much larger performance gap (almost an order of magnitude) for summing a simple vector of integers. Am I doing something wrong?

julia> function sum_iterate_unsafe(xs)
           s = zero(Int)
           for x in xs
               s += x
           end
           s
        end
sum_iterate_unsafe (generic function with 1 method)

julia> function sum_iterate_safe(xs)
           s = zero(SafeInt)
           for x in xs
               s += x
           end
           s
        end

julia> xs = Int[x for x in 1:1000];

julia> @btime sum_iterate_unsafe($xs)
  57.702 ns (0 allocations: 0 bytes)
500500

julia> @btime sum_iterate_safe($xs)
  450.239 ns (0 allocations: 0 bytes)
500500
JeffreySarnoff commented 4 years ago

You make a good point. Benchmarking results were on Julia v1.1, on v1.5 they still hold but there were regressions with v1.3, 1.4 independent of SaferIntegers that slowed down the performance. These are micro-benchmarks rather than vector oriented. I will investigate.

JeffreySarnoff commented 3 years ago

Working with sum and summing Ints vs summing SafeInts, I find these results to be reasonable.
I am not convinced that mucking with the currently correct logic is worth saving a factor of 2 here. There is a possibility that I can apply SIMD.jl to these sums -- I will let you know.

n rel time
10 1.2
100 5
1_000 6.5
10_000 5
100_000 3.25
1_000_000 2.75
JeffreySarnoff commented 3 years ago

SIMD is not a good solution here.