Bug 39631 - add optimisation helpers
Summary: add optimisation helpers
Status: RESOLVED FIXED
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: LibreOffice (show other bugs)
Version:
(earliest affected)
unspecified
Hardware: Other All
: medium normal
Assignee: sheikha
URL:
Whiteboard: target:5.2.0
Keywords: difficultyBeginner, easyHack, skillCpp
Depends on:
Blocks:
 
Reported: 2011-07-28 09:36 UTC by Björn Michaelsen
Modified: 2016-10-25 19:08 UTC (History)
3 users (show)

See Also:
Crash report or crash signature:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Björn Michaelsen 2011-07-28 09:36:24 UTC
=== add optimisation helpers ===

'''Background:''' Having profiled, a number of places in the code would benefit from annotation as to which branch is likely to occur in real use. gcc > version 2 provides a nice __builtin_expect function to add this to code to help the optimiser in places. It would be great to add a SAL_LIKELY, SAL_UNLIKELY pair of macros to sal/inc/sal/macros.hxx using this function. It would also might be useful to have SAL_HOT, SAL_COLD pair that (under gcc) would add the __attribute__ (("hot")) or cold - that can also help the optimiser for performance critical functions.

Clearly these must not be used without profiling.

'''Skills:''' C, build
Comment 1 Florian Reisinger 2012-05-18 09:06:44 UTC
Deteted "Easyhack" from summary
Comment 2 Björn Michaelsen 2013-10-04 18:46:51 UTC
adding LibreOffice developer list as CC to unresolved EasyHacks for better visibility.

see e.g. http://nabble.documentfoundation.org/minutes-of-ESC-call-td4076214.html for details
Comment 3 Kohei Yoshida 2014-05-15 01:59:44 UTC
That header sal/inc/sal/macros.h these days lives in include/sal/macros.h.

BTW, there never was sal/inc/sal/macros.hxx.  It was always sal/inc/sal/macros.h.
Comment 4 Michal Strnad 2014-05-15 02:39:32 UTC
I am working on this bug.
Comment 5 Stephan Bergmann 2014-05-15 10:40:41 UTC
(In reply to comment #3)
> That header sal/inc/sal/macros.h these days lives in include/sal/macros.h.
> 
> BTW, there never was sal/inc/sal/macros.hxx.  It was always
> sal/inc/sal/macros.h.

...and such platform-dependent SAL_LIKELY functionality would more naturally fit into sal/types.h than sal/macros.h
Comment 6 Stephan Bergmann 2014-09-23 07:00:31 UTC
also note that GCC __builtin_expect (with its first parameter being of type long) is plain, simple, and generally does not work as one would naively hope:

> class C {
> private:
>     struct S;
>     typedef void (S::* B)();
> public:
>     operator B();
> };
> int f(C c) {
>     if (c) {
>         if (__builtin_expect(c, 1)) {
>             return 1;
>         } else {
>             return 2;
>         }
>     } else {
>         return 3;
>     }
> }

will cause a compilation error "cannot convert ‘C’ to ‘long int’ for argument ‘1’ to ‘long int __builtin_expect(long int, long int)’"
Comment 7 Michael Meeks 2014-09-23 09:03:05 UTC
I guess it might be worth talking to the glib developers; gmacros.h has a reasonably workable version - however the _G_BOOLEAN_EXPR stuff is not the ultimate in beauty =) The implementation in glib is all written by:

Matthias Clasen <matthiasc@src.gnome.org>

I just dug throught he commit log for glib =) I'd be inclined to simply re-use G_LIKELY and G_UNLIKELY with a SAL prefix myself.
Comment 8 Stephan Bergmann 2014-09-24 07:31:07 UTC
(In reply to comment #7)
> I just dug throught he commit log for glib =) I'd be inclined to simply
> re-use G_LIKELY and G_UNLIKELY with a SAL prefix myself.

That _G_BOOLEAN_EXPR trick would work.  An alternative would be to restrict this to C++ and instead stick an explicit conversion to bool into the __builtin_expect call.
Comment 9 Michael Meeks 2014-09-24 08:16:45 UTC
As you like; we have less and less C these days I guess, and IIRC we compile it as C++ (?) =)
Comment 10 Robinson Tryon (qubit) 2015-12-13 10:58:03 UTC Comment hidden (obsolete)
Comment 11 sheikha 2016-01-05 08:37:41 UTC
I add SAL_LIKELY, SAL_UNLIKELY ,SAL_HOT, SAL_COLD pair of macro in sal/types.h ,
before that I add 
 _SAL_BOOLEAN_EXPR which is an alternative way  of using __builtin_expect which is good for implicit conversion to bool, because __builtin_expect 
cause a compilation error it cannot convert  to long int.

see here the patch

http://ci.libreoffice.org/job/lo_gerrit_master/9846/
Comment 12 jani 2016-01-05 08:56:29 UTC
you point at the ci build job not the patch.

the patch is
https://gerrit.libreoffice.org/#/c/21070

I have added it to my review list.
Comment 13 Commit Notification 2016-01-06 09:32:27 UTC
Sheikha AL-Hinai committed a patch related to this issue.
It has been pushed to "master":

http://cgit.freedesktop.org/libreoffice/core/commit/?id=eafb1ebf74c3caf8fbecdc6a4fc7037c3c8f4964

tdf#39631: Add optimisation helpers

It will be available in 5.2.0.

The patch should be included in the daily builds available at
http://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
http://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 14 Commit Notification 2016-01-06 09:32:31 UTC
Michael Meeks committed a patch related to this issue.
It has been pushed to "master":

http://cgit.freedesktop.org/libreoffice/core/commit/?id=e75406e54c57fc3113d4f1983249eb2aec0a3bcd

tdf#39631 - branch hints: comment, and tweak variously, also use.

It will be available in 5.2.0.

The patch should be included in the daily builds available at
http://dev-builds.libreoffice.org/daily/ in the next 24-48 hours. More
information about daily builds can be found at:
http://wiki.documentfoundation.org/Testing_Daily_Builds

Affected users are encouraged to test the fix and report feedback.
Comment 15 Michael Stahl (allotropia) 2016-01-26 15:40:22 UTC
looks like Sheikha fixed this one, thanks!
Comment 16 Robinson Tryon (qubit) 2016-02-18 16:37:16 UTC Comment hidden (obsolete)