JuliaArrays / JuliaArrays/AxisArrays.jl

Faster version of permutation() and axisnames()

Open
#187 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
207
Forks
41
PR merge metrics
No merged PRs in 30d

Description

Hi!
I came up with a faster version of permute(). Maybe it lacks some functionality or generality, but I couldn't come up with such a case yet - can you?

using AxisArrays
using BenchmarkTools

# the wooden hammer
@inline @inbounds function check_duplicates(arr)
    N = length(arr)
    for i in 1:N-1
        for j in i+1:N
            arr[i] != arr[j] || throw(ArgumentError("duplicate"))
        end
    end
    return nothing
end

function foo_perm(to, from)
    length(to) == length(from) || throw(ArgumentError("not same length"))
    res = Vector{Int}(undef, length(from))
    @inbounds for (i,t) in enumerate(to)
        idx = findfirst(from .== t)
        idx != nothing || throw(ArgumentError("a not in b"))
        res[i] = idx
    end
    check_duplicates(res)
    return res
end

to=(:c,:w,:h,:d)
from=(:c,:h,:w,:d)
foo_perm(to, from) == AxisArrays.permutation(to, from)
@btime foo_perm($to, $from)  # 42ns
@btime AxisArrays.permutation($to, $from)  # 307ns

I think axisnames() is typically called with an AxisArray as argument, but I found it a bit slow. Again I came up with another solution which might lack generality, but is faster:

axname(a::AxisArrays.Axis{name}) where name = name
axnames(a::AxisArray) = axname.(a.axes)

a = AxisArray(rand(3,4,5), :c,:h,:w)
axnames(a) == axisnames(a)
@btime axnames($a)  # 340ns
@btime axisnames($a)  # 4us

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the existing permutation() and axisnames() implementations and compare them with the benchmarked Julia snippets in the issue. Check behavior and generality for supported inputs, then benchmark representative cases and confirm that any accepted implementation preserves existing results.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
performance
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.