JuliaMath / JuliaMath/MeasureTheory.jl

FillArrays much slower than MappedArrays

Open
#52 17 comments 0 reactions 0 assignees View on GitHub
Dominant language
Julia
Stars
401
Forks
31
PR merge metrics
No merged PRs in 30d

Description

This is pretty strange:
```julia
julia> d_fill = Normal(2.0,5.0) ^ 20
Normal(μ = 2.0, σ = 5.0) ^ (20,)

julia> d_mapped = For(1:20) do i Normal(2.0, 5.0) end
For(#15, 1:20)

julia> x = rand(d_fill);

julia> @btime logdensity($d_fill, $x)
132.057 ns (0 allocations: 0 bytes)
-38.3707078501479

julia> @btime logdensity($d_mapped, $x)
14.346 ns (0 allocations: 0 bytes)
-38.370707850147895
```

These both call the same code:

```julia
@inline function MeasureTheory.logdensity(d::ProductMeasure, x)
@boundscheck size(d.data) == size(x) || throw(BoundsError)

s = 0.0
Δs(j) = @inbounds logdensity(d.data[j], x[j])

@inbounds @simd for j in eachindex(x)
s += Δs(j)
end
s
end
```

The only difference is is the payload:

```julia
julia> d_fill.data
20-element Fill{Normal{(:μ, :σ), Tuple{Float64, Float64}}}: entries equal to Normal(μ = 2.0, σ = 5.0)

julia> d_mapped.data
20-element mappedarray(i->Main.Normal(2.0, 5.0), ::UnitRange{Int64}) with eltype Normal{(:μ, :σ), Tuple{Float64, Float64}}:
Normal(μ = 2.0, σ = 5.0)
Normal(μ = 2.0, σ = 5.0)
Normal(μ = 2.0, σ = 5.0)
Normal(μ = 2.0, σ = 5.0)
Normal(μ = 2.0, σ = 5.0)
Normal(μ = 2.0, σ = 5.0)
Normal(μ = 2.0, σ = 5.0)
Normal(μ = 2.0, σ = 5.0)
Normal(μ = 2.0, σ = 5.0)
Normal(μ = 2.0, σ = 5.0)
Normal(μ = 2.0, σ = 5.0)
Normal(μ = 2.0, σ = 5.0)
Normal(μ = 2.0, σ = 5.0)
Normal(μ = 2.0, σ = 5.0)
Normal(μ = 2.0, σ = 5.0)
Normal(μ = 2.0, σ = 5.0)
Normal(μ = 2.0, σ = 5.0)
Normal(μ = 2.0, σ = 5.0)
Normal(μ = 2.0, σ = 5.0)
Normal(μ = 2.0, σ = 5.0)
```

This is really surprising to me. If anything, I'd expect FillArrays to be a little quicker, since the value doesn't change. I tried specializing the `Δs(j)` line for FillArrays, but it doesn't seem to help.

Any idea what's going on here?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.