What does 'inline' mean?

Manu turkeyman at gmail.com
Tue Jun 9 00:36:18 UTC 2020


On Tue, Jun 9, 2020 at 1:00 AM Steven Schveighoffer via Digitalmars-d <
digitalmars-d at puremagic.com> wrote:

> On 6/8/20 4:42 AM, evilrat wrote:
> > On Monday, 8 June 2020 at 06:14:44 UTC, Manu wrote:
> >>
> >> I think a first part of the conversation to understand, is that since
> >> D doesn't really have first-class `inline` (just a pragma, assumed to
> >> be low-level compiler control), I think most people bring their
> >> conceptual definition over from C/C++, and that definition is a little
> >> odd (although it is immensely useful), but it's not like what D does.
> >>
> >> In C/C++, inline says that a function will be emit to the binary only
> >> when
> >> it is called, and the function is marked with internal linkage (it is
> not
> >> visible to the linker from the symbol table)
> >> By this definition; what inline REALLY means is that the function is not
> >> placed in the binary where it is defined, it is placed in the binary
> >> where
> >> it is CALLED, and each CU that calls an inline function receives their
> >> own
> >> copy of the function. From here; optimisers will typically inline the
> >> call
> >> if they determine it's an advantage to do so.
> >>
> >
> > Sorry for sticking in my nose, but in result inline in C++ simply tells
> > the linker "this symbol could be present multiple times, just pick
> > whichever one of them and done with it" so it won't complain about
> > multiple symbols anymore.
> > And that's why you have to slap inline to functions that have body in
> > header in order to build.
> >
> > But I'm not very certain if that's all about it, not using C++ that much
> > since then, and never to that truly deep extent where any loose bit can
> > destroy performance, not beyond fixing the client's code and some crappy
> > pet projects.
>
> This was not true the last time I found "inline abuse".
>
> At a previous company, there was a type where a function was inline or
> not depending on whether you defined a macro. And when you defined that
> macro, the implementation was DIFFERENT (and different in a way that
> calling the non-inline version would be a bug).
>
> Which meant that if the linker was free to choose whichever
> implementation it wanted, it would defeat the purpose of the inline
> mechanism (BTW, this was the first time I ever experienced an inline
> function, and had to research what it meant).
>
> I think Manu's description is accurate, but I also thought it actually
> inlined the function call as well, even without an optimizer. Hard to
> tell when the end result isn't obvious.
>

What's funny is, in most cases, whether the function is ACTUALLY inlined is
not really interesting in 2020.
What inline allows is control over the binary environment as I describe. I
read it these days as "inline to the calling CU" rather than "inline to the
calling function".

There are cases where inline is really important, and I do want an error if
it fails; for instance, if you have a leaf function (does not allocate any
stack memory), it's only possible to make calls from that function where
the callee is inlined... and if inlining fails, your caller will lose its
no-stack-frame requirement. I've had this come up numerous times, and in
those cases, a really-strong-does-make-compile-error inline would be
useful, but C++ doesn't have anything like that.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20200609/b524e331/attachment-0001.htm>


More information about the Digitalmars-d mailing list