Forcing inlining of delegates and lazy
ABrightLight
example at example.com
Wed Sep 2 01:11:12 UTC 2026
On Tuesday, 1 September 2026 at 22:09:04 UTC, FinalEvilution
wrote:
> On Tuesday, 1 September 2026 at 17:07:59 UTC, ABrightLight
> wrote:
>> On Tuesday, 1 September 2026 at 15:58:12 UTC, FinalEvilution
>> wrote:
>>>
>>> If the option doesn't work out would you be able to provide a
>>> reduced test case? I've become rather curious as to what's
>>> going on.
>>
>> Actually in your sample, if you go over to godbolt.org and
>> compile with LDC and -O5 (or -Oz) you'll see that that
>> `expr();` ends up being:
>> ```
>> mov rdi, r14
>> call rbx
>> ```
>
> -O5 and -O4 are just aliases and are equivalent to -O3.
>
> I just checked myself on goldbolt and ldc inlines and unrolls
> the loop into `_Dmain` without any calls to `times`.
> https://godbolt.org/z/sWYzT9sW6
> The unused `times` function cannot be eliminated as the binary
> hasn't been linked yet.
> Here's another version without the noise of the standard
> library. Uncomment the `//pragma(inline, false)` to see the
> difference.
> https://godbolt.org/z/4Y5q3nv4e
> The `foo` function cannot be inlined as there is no function
> body. It is only used for demonstration.
Regarding the compiler flags, I was aware- but I appreciate the
tip nevertheless.
I expanded the example to:
```d
void main () {
test1;
test2;
}
void test1 ()
{
int x;
3.times(foo(x++));
}
void test2 ()
{
3.timesAgain;
}
pragma(inline, true)
void times(int n, lazy void exp)
{
while (n--)
{
exp();
}
}
pragma(inline, true)
void timesAgain (int n) {
int x;
while (n--)
{
foo(x++);
}
}
void foo(int val);
```
It looks like you're right.
`main` ends up with 3 calls to `foo` in the first example and
then another 3 calls to `foo` as part of the second test, even
thought the `times` and `timesAgain` functions remain in the
binary and have different instructions.
I'll go ahead and rely on this in hotcode. Thanks for your help!
More information about the Digitalmars-d
mailing list