-
Notifications
You must be signed in to change notification settings - Fork 13
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
Compose SimpleRatio and SaferIntegers #23
Conversation
as an aside JuliaMath/Interpolations.jl#457 (comment) |
In terms of benchmarks, using julia> f(ipti) = for i=1:10000; itpi(i); end
f (generic function with 2 methods)
julia> itpi = LinearInterpolation(Int[1,10000],Int[1,10000]; extrapolation_bc=Line())
2-element extrapolate(interpolate((::Vector{Int64},), ::Vector{Int64}, Gridded(Linear())), Line()) with element type Float64:
SimpleRatio{Int64}(99980001, 99980001)
SimpleRatio{Int64}(999800010000, 99980001)
julia> itpi_safe = LinearInterpolation(SafeInt[1,10000],SafeInt[1,10000]; extrapolation_bc=Line())
2-element extrapolate(interpolate((::Vector{SafeInt64},), ::Vector{SafeInt64}, Gridded(Linear())), Line()) with element type Float64:
SimpleRatio{SafeInt64}(99980001, 99980001)
SimpleRatio{SafeInt64}(999800010000, 99980001)
julia> @benchmark $f($itpi)
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
Range (min … max): 256.500 μs … 2.060 ms ┊ GC (min … max): 0.00% … 85.61%
Time (median): 304.500 μs ┊ GC (median): 0.00%
Time (mean ± σ): 323.403 μs ± 151.025 μs ┊ GC (mean ± σ): 4.67% ± 8.32%
▁██▄ ▂
█████▇█▆▅▆▅▅▅▇▆▆▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▃▃ █
256 μs Histogram: log(frequency) by time 1.68 ms <
Memory estimate: 460.77 KiB, allocs estimate: 19489.
julia> @benchmark $f($itpi_safe)
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
Range (min … max): 265.200 μs … 2.593 ms ┊ GC (min … max): 0.00% … 77.90%
Time (median): 309.500 μs ┊ GC (median): 0.00%
Time (mean ± σ): 331.228 μs ± 153.054 μs ┊ GC (mean ± σ): 4.58% ± 8.29%
▃█▇▄ ▁ ▂
█████▅▆█▆▄▄▅▆▇▆▆▆▅▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▄ █
265 μs Histogram: log(frequency) by time 1.66 ms <
Memory estimate: 460.77 KiB, allocs estimate: 19489. |
Running the same benchmarks on 1.8.0-DEV, I see similar results with @benchmark using n=10_000. I see little timing difference with @Btime using n=100_000. and the @benchmark means are about the same, though the medians are like the results for n=10_000.
|
fwiw, that using SafeInt64s only requires 1.025 the time for Int64s is quite good |
Color me impressed: I did not realize that checked arithmetic now had such low overhead. Nice work on SaferIntegers, @JeffreySarnoff. Will this also apply for |
(Note that my comment applies only to interpolation, not extrapolation, as extrapolation already comes with a branch. So this should be benchmarked using the lower-level API of interpolations.) |
I'm leaning towards closing this in favor of JeffreySarnoff/SaferIntegers.jl#20. |
I agree, but let's let that be merged first. It doesn't seem that CI is set up on that repo, so there could be unintended consequences (unless you tested locally). |
I tested locally. The most difficult aspect was getting type promotion to work again in the same manner since dispatch was modified. |
looking .. |
I have requested two changes JeffreySarnoff/SaferIntegers.jl#20 (review) |
The modification to SaferIntegers.jl is merged. This change requires a new major version imo, so now we have v3 -- I expect that will require extra days in the package management merging. |
Thank you @mkitti for the perspicacious shepherding. |
This pull request composes Ratios with SaferIntegers in order to detect overflow errors.
SafeSigned
andSafeUnsigned
inSaferIntegers
are not subtypes ofSigned
andUnsigned
, respectively. Therefore, the additive inverse method is not defined in Ratios.jl for these types resulting in aMethodError
.SafeSigned
andSafeUnsigned
are subtypes ofSafeInteger
andInteger
, so the other methods are defined.This pull request adds methods to take the additive inverse of
SimpleRatio
s based onSafeInt
andSafeUInt
:After the addition of these methods integer overflow can be detected in Interpolations.jl as in JuliaMath/Interpolations.jl#457
Additionally a test suite is added to test the integration of the two packages.