Re: JIT-friendly version of unpack()

  • From: Craig Barnes <craigbarnes85@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Tue, 16 Sep 2014 14:01:43 +0100

On 16 September 2014 13:00, Roman Tsisyk <roman@xxxxxxxxxxxxx> wrote:
> 2. Specialized versions for particular sizes - do compile, but looks ugly
>
>     local function unpack3(t)
>         if #t == 1 then
>             return t[1]
>         else if #t == 2 then
>             return t[1], t[2]
>         else if #t == 3 then
>             return t[1], t[2], t[3]
>         else
>             return unpack(t) -- goodbye, JIT
>         end
>     end

It doesn't have to be that ugly. The following is more or less equivalent:

local function unpack3(t)
    if #t <= 3 then
        return t[1], t[2], t[3]
    else
        return unpack(t)
    end
end

You could also make an unpack5 without any extra redundant branches or
length operators and only a little extra code.

Other related posts: