# Fastest way to get CodeInfo from a function call

**URL:** <https://discourse.julialang.org/t/fastest-way-to-get-codeinfo-from-a-function-call/74813>\
**Category:** Internals & Design\
**Created:** [January 18, 2022, 4:15pm UTC](https://discourse.julialang.org/t/fastest-way-to-get-codeinfo-from-a-function-call/74813 "2022-01-18T16:15:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [January 18, 2022, 4:15pm UTC](https://discourse.julialang.org/t/fastest-way-to-get-codeinfo-from-a-function-call/74813/1 "2022-01-18T16:15:40Z")

</div>

Given a function `f` and its arguments `args`, Is there a faster way to get corresponding `CodeInfo` than `code_lowered(f, map(typeof, args))`? I don’t care about type inference and compiler optimizations - in fact, the closer to the original method definition, the better

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [January 18, 2022, 4:58pm UTC](https://discourse.julialang.org/t/fastest-way-to-get-codeinfo-from-a-function-call/74813/2 "2022-01-18T16:58:58Z")

</div>

I think `code_lowered` is really bare minimal already, it comes down to (as you probably have looked at):

```julia
julia> m = first(Base.method_instances(+, (Int, Int))).def
julia> Base.uncompressed_ir(m)
CodeInfo(
    @ int.jl:87 within `+`
1 ─ %1 = Base.add_int(x, y)
└── return %1
)

```

and looks like if the function is not generated, the `uncompressed_ir` is just calling:

```julia
_uncompressed_ir(m::Method, s::Array{UInt8,1}) = ccall(:jl_uncompress_ir, Any, (Any, Ptr{Cvoid}, Any), m, C_NULL, s)::CodeInfo

```

on the byte vector of `m.def.source`

---

<div class="post-metadata">

**Author:** ![dfdx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dfdx/32/120_2.png) [@dfdx](https://discourse.julialang.org/u/dfdx)\
**Post date:** [January 19, 2022, 10:30pm UTC](https://discourse.julialang.org/t/fastest-way-to-get-codeinfo-from-a-function-call/74813/3 "2022-01-19T22:30:33Z")

</div>

Huh, actually, if we use only the methods you showed and skip a few checks you avoided, we get a good performance improvement for the _first_ call on the _first_ function:

```julia
# each line ran in a fresh Julia session
@time for fn in fns get_code_info(fn, 1.0) end # 0.29s
@time for fn in fns get_code_info2(fn, 1.0) end # 0.09
@time get_code_info(sin, 1.0) # 0.23
@time get_code_info2(sin, 1.0) # 0.08

```

where:

```julia
function get_code_info(f, args...)
    types = map(typeof, args)
    cis = code_lowered(f, types)
    if isempty(cis)
        arg_type_str = join(types, ", ")
        error("Cannot get CodeInfo for $f($arg_type_str)")
    end
    return cis[1]
end

function get_code_info2(f, args...)
    types = map(typeof, args)
    mis = Base.method_instances(f, types)
    if isempty(mis)
        arg_type_str = join(types, ", ")
        error("Cannot get CodeInfo for $f($arg_type_str)")
    end
    m = first(mis).def
    ci = Base.uncompressed_ir(m)
    return ci
end

```

Anyway, the added latency of `code_info()` only shows up during the very first invocation, so probably it’s not worth the complexity. Thanks for confirming my expectations!
