🎓 A1Learner
🔍
0 of 47 lessons completed

Julia Specialization and Inlining

Specialization and inlining are compiler choices that shape how a function runs. Specialization makes a version for each argument type, and inlining places a small body at its call site.

One function accepts more than one type

One function can accept more than one type. It returns each value unchanged.

function identity_value(x)
    return x
end
println(identity_value(5))
println(identity_value(2.5))

A marked argument still accepts both types

The same function with a marked argument still accepts both types. Its counts after warm-up stay at 0.

function nospec_value(@nospecialize(x))
    return x
end
nospec_value(5)
nospec_value(2.5)
println(nospec_value(5))
println(nospec_value(2.5))
println(@allocated nospec_value(5))
println(@allocated nospec_value(2.5))

A tiny helper can be marked for inlining

A tiny helper can be marked for inlining. Its caller still gives the right answer with no new storage.

@inline function double_value(x)
    return 2 * x
end
function use_double_value(x)
    return double_value(x) + 1
end
use_double_value(21)
println(use_double_value(21))
println(@allocated use_double_value(21))

Example

@inline function add_one_value(x)
    return x + 1
end
function apply_plain(@nospecialize(x))
    return add_one_value(x)
end
apply_plain(10)
apply_plain(2.5)
println(apply_plain(10))
println(apply_plain(2.5))
println(@allocated apply_plain(10))
println(@allocated apply_plain(2.5))

Key points

  • Specialization: the compiler can make a separate version of a function for each argument type.
  • Generic code: one written function can accept an integer and a float and give the right result for each.
  • No specialization: @nospecialize on an argument tells the compiler not to make a separate version for that argument type.
  • Inlining: @inline suggests the compiler place a small function body at its call site.
  • Small bodies: @inline fits tiny helpers whose body is short and simple.

In this example, a tiny helper adds one to its input. A function with a marked argument calls that helper. Both an integer input and a float input give the right result with no new storage.

Note

specialization and inlining change how code runs, not what it returns, so outputs stay the same.

Try it Yourself

Edit the code, then press Run.

Running is simulated · JuliaHub

Exercise

What does @nospecialize on an argument tell the compiler?