Skip to content

Commit 4b24f6a

Browse files
Merge pull request #60 from ChrisRackauckas-Claude/static-improvements-20251229-095758
Add JET.jl static analysis tests and Real type annotations
2 parents 1d753f9 + f9e2ae4 commit 4b24f6a

3 files changed

Lines changed: 27 additions & 9 deletions

File tree

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
99
[compat]
1010
Aqua = "0.8"
1111
Distributions = "0.25"
12+
JET = "0.9, 0.10, 0.11"
1213
LogExpFunctions = "0.3"
1314
Random = "1.10"
1415
Statistics = "1"
@@ -18,8 +19,9 @@ julia = "1.10"
1819
[extras]
1920
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
2021
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
22+
JET = "c3a54625-cd67-489e-a8e7-0a5a0ff4e31b"
2123
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
2224
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
2325

2426
[targets]
25-
test = ["Aqua", "Statistics", "Test", "Distributions"]
27+
test = ["Aqua", "Statistics", "Test", "Distributions", "JET"]

src/PoissonRandom.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ Random.rand(rng::PassthroughRNG) = rand()
1212
Random.randexp(rng::PassthroughRNG) = randexp()
1313
Random.randn(rng::PassthroughRNG) = randn()
1414

15-
count_rand(λ) = count_rand(Random.GLOBAL_RNG, λ)
16-
function count_rand(rng::AbstractRNG, λ)
15+
count_rand::Real) = count_rand(Random.GLOBAL_RNG, λ)
16+
function count_rand(rng::AbstractRNG, λ::Real)
1717
n = 0
1818
c = randexp(rng)
1919
while c < λ
@@ -31,8 +31,8 @@ end
3131
#
3232
# For μ sufficiently large, (i.e. >= 10.0)
3333
#
34-
ad_rand(λ) = ad_rand(Random.GLOBAL_RNG, λ)
35-
function ad_rand(rng::AbstractRNG, λ)
34+
ad_rand::Real) = ad_rand(Random.GLOBAL_RNG, λ)
35+
function ad_rand(rng::AbstractRNG, λ::Real)
3636
s = sqrt(λ)
3737
d = 6 * λ^2
3838
L = floor(Int, λ - 1.1484)
@@ -82,7 +82,7 @@ function ad_rand(rng::AbstractRNG, λ)
8282
end
8383

8484
# Procedure F
85-
function procf(λ, K::Int, s::Float64)
85+
function procf::Real, K::Int, s::Float64)
8686
# can be pre-computed, but does not seem to affect performance
8787
INV_SQRT_2PI = inv(sqrt(2pi))
8888
ω = INV_SQRT_2PI / s
@@ -133,7 +133,7 @@ pois_rand(rng, λ)
133133
pois_rand(PoissonRandom.PassthroughRNG(), λ)
134134
```
135135
"""
136-
pois_rand(λ) = pois_rand(Random.GLOBAL_RNG, λ)
137-
pois_rand(rng::AbstractRNG, λ) = λ < 6 ? count_rand(rng, λ) : ad_rand(rng, λ)
136+
pois_rand::Real) = pois_rand(Random.GLOBAL_RNG, λ)
137+
pois_rand(rng::AbstractRNG, λ::Real) = λ < 6 ? count_rand(rng, λ) : ad_rand(rng, λ)
138138

139139
end # module

test/qa.jl

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using PoissonRandom, Aqua
1+
using PoissonRandom, Aqua, JET
2+
using Random
3+
24
@testset "Aqua" begin
35
Aqua.find_persistent_tasks_deps(PoissonRandom)
46
Aqua.test_ambiguities(PoissonRandom, recursive = false)
@@ -10,3 +12,17 @@ using PoissonRandom, Aqua
1012
Aqua.test_unbound_args(PoissonRandom)
1113
Aqua.test_undefined_exports(PoissonRandom)
1214
end
15+
16+
@testset "JET static analysis" begin
17+
@testset "Type stability" begin
18+
JET.@test_opt target_modules = (PoissonRandom,) pois_rand(10.0)
19+
JET.@test_opt target_modules = (PoissonRandom,) pois_rand(Random.default_rng(), 10.0)
20+
JET.@test_opt target_modules = (PoissonRandom,) pois_rand(PassthroughRNG(), 10.0)
21+
end
22+
23+
@testset "Error analysis" begin
24+
JET.@test_call target_modules = (PoissonRandom,) pois_rand(10.0)
25+
JET.@test_call target_modules = (PoissonRandom,) pois_rand(Random.default_rng(), 10.0)
26+
JET.@test_call target_modules = (PoissonRandom,) pois_rand(PassthroughRNG(), 10.0)
27+
end
28+
end

0 commit comments

Comments
 (0)