Partial inlining

There this function from std.Io that I can’t stop thinking about:

https://codeberg.org/ziglang/zig/src/commit/23e69f7c5766c46c2b699762642f7134d916e515/lib/std/Io/Reader.zig#L1117

the function Reader.fill is split between Reader.fill that contains the early return case where nothing needs to be done, and Reader.fillUnbeffered where the actual prefill happen. This allows the early return branch to be inlined at the call site, and save the cost of a functon call in that case.

So my question is should I program like this?

While searching the topic I learned that LLVM does have a partial inlining pass, that is aimed at automaticallysolving this specific issue, but it’s not enabled by default.

Has anyone here tried that pass?

It seems like it would be great in Zig, eg I often have asserts at the top of my function that arguably would be better located on the caller side.

1 Like

So my question is should I program like this?

If you observe the problem in a profiler and can demonstrate a positive performance delta once changed, then yeah. Doing this prematurely at all possible hot-paths is probably not desirable.

I often have asserts at the top of my function that arguably would be better located on the caller side.

I would (maybe?) expect the default optimiser settings to figure the assert paths out in Release as they trigger undefined behaviour. Early returns/errors though, definitely curious about that pass you mention.