Skip to content

Broken optimization of Normed multiplication and inefficient multiplication #166

@kimikage

Description

@kimikage

As you know, Normed multiplication in FixedPointNumbers v0.8 is slow. The performance will be improved in FixedPointNumbers v0.9 (cf. JuliaMath/FixedPointNumbers.jl#213), but there are still some issues to be solved before FixedPointNumbers v0.9 is released.

As mentioned below, the real scaling for RGBs is made faster by specialization for Normed. On the other hand, the multiplication for grays and the new multiplication operator use the slow multiplication.

julia> using BenchmarkTools, ColorVectorSpace, ColorTypes, FixedPointNumbers, ColorBlendModes;

julia> a = rand(RGB{N0f8}, 1000, 1000); b = rand(RGB{N0f8}, 1000, 1000);

julia> a .⊙ b == BlendMultiply.(a, b) # the same operation
true

julia> @btime $a .⊙ $b;
  10.084 ms (2 allocations: 2.86 MiB)

julia> @btime BlendMultiply.($a, $b);
  881.000 μs (2 allocations: 2.86 MiB)

One possible option is just to wait for FixedPointNumbers v0.9, and it's okay for me.
However, there is a problem with the current RGB optimization. The following is a bug which has existed since the initial working commit of ColorVectorSpace.

function (*)(f::Normed, c::AbstractRGB{T}) where T<:Normed
fs = reinterpret(f)*(1/widen(reinterpret(oneunit(T)))^2)
rettype(*, f, c)(fs*reinterpret(red(c)), fs*reinterpret(green(c)), fs*reinterpret(blue(c)))
end

julia> 0.5N0f8 * RGB{N0f16}(1, 0.5, 0)
RGB{N0f16}(0.00195,0.00098,0.0)

For that reason, I would like to rewrite the implementation of the multiplication, not so much for the performance, but for the code quality.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions