Function inlining questions

this is not a question about the inline keyword.

does the compiler inline functions if it deems that they are simple enough, regardless of build releases, or it it exclusively tied to build release optimizations?

consider the following snippet from my project, would any of new(), air(), blockData() get inlined in debug mode? if so, why?

pub const Block = bitpack struct(u32) {
    id: u14,

    reserved: u18 = 0,
    pub fn new(id: u14) Block {
        return Block{
            .id = id,
        };
    }
    pub fn air() Block {
        return comptime Block{ .id = 0 };
    }
    pub fn blockData(s: Block) BlockData {
        return BlockRegistry.blockData(s.id);
    }
};

It happens regardless of build mode. Inlining functions in Zig has specific semantics, that differ from what you may know from other languages. You can read more here.

As far as I know they wouldn’t.


I would advise you to don’t use inline, if you aren’t very sure that you need it. Measure before you do it for performance reasons. And if you need it for comptime stuff you’ll find that out when you need it.

Sorry, i didn’t make myself clear, i didn’t mean the inline keyword, i meant simply meant that function would get inlined by itself if the compiler recognizes that its a small enough function like the ones i showed above.

ill update my post.

Ah, no problem.

In that case, no, automatic inlining only happens in release modes. In debug none of them will get inlined automatically.

AFAIK, for the self-hosted backends there aren’t any optimizations like that in debug. And when building with the LLVM backend, Zig uses LLVM’s O0, which doesn’t run the regular cost-based inliner at all. The only inliner that runs at O0 is AlwaysInlinerPass, and normal Zig functions don’t get LLVM’s alwaysinline attribute.


A small side-note on air. The return comptime Block{ .id = 0 }; only forces the struct literal inside the function to be evaluated at comptime, it doesn’t inline or comptime-evaluate calls to Block.air(). If you want to avoid a function call for that even in Debug, you could just make it a constant instead: pub const air: Block = .{ .id = 0 };.

I think it’s mainly based on how often the function is called
If it’s only called once it’s a no brainer to inline it
If it’s called a thousand times, inlining it would be better performance but could make the binary size balloon, making it a poor choice
If there are a lot of complex branches leading to scenarios where it could be called, inlining it would be a very bad idea cuz it could lead to combinatorial explosion levels of copying the function’s code, once again making the binary size blow up

Debug mode is very reluctant to inline functions, if you go to https://godbolt.org you’ll find that in debug mode the functions in std/start.zig get preserved as functions (even though they’re called only once), whereas in any release mode they’ll get inlined

I’m not sure if you’re wanting to make sure something isn’t inlined, so I think it’s worth mentioning the noinline keyword. It’s not mentioned in the language docs, apart from the keyword table. It stops a function from being able to be automatically inlined.

It’s rare that you need it, but I have found it useful on occasions.