Re: Using __builtin_expect() in the body of unlikely branch

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



* Konstantin Kharlamov:

> On 07.01.2019 13:35, Florian Weimer wrote:
>> * Marc Glisse:
>>
>>> On Mon, 7 Jan 2019, Konstantin Kharlamov wrote:
>>>
>>>> In most projects a definite pattern that's unlikely to be executed
>>>> is a PRINT_ERR macro which is basically a wrapper around fprintf()
>>>> call. E.g.
>>>>
>>>> 	if (some_error) {
>>>> 		PRINT_ERR("ERR");
>>>> 		// do cleanup
>>>> 		return;
>>>> 	}
>>>>
>>>> I wonder, is there a way to hint GCC that, whenever that code
>>>> appears, whatever branch was prior to that is unlikely to be
>>>> executed?
>>>
>>> Make PRINT_ERR a function with __attribute__((cold)).
>>
>> But this does something completely differently.  On some targets, it
>> produces unbearably slow code because GCC expects the code to never run
>> in practice.  __builtin_expect does not have this effect.
>
> So, you would not recommend using that attribute for
> functions-loggers?

No, at least not on i386 and x86-64.  It may be okay if the logging
action ends up in an actual system call (due to its overhead), but if
the logging-active path ends up suppressing the message (after calling a
string function), it could lead to artifacts showing up in profiling.

> Ultimately I was asking because I was thinking of
> contributing such optimization to arbitrary projects I happen to use,
> such as libinput, wine, etc (list is completely offhand, I didn't look
> at wine code in particular, and they're on feature-freeze ATM anyway).

Currently, you need to use __builtin_expect.

I'll try to improve the documentation of the attribute.

Thanks,
Florian



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux