Mail archive
alpine-devel

Re: [alpine-devel] force compile flag for musl?

From: Jakub Jirutka <jakub_at_jirutka.cz>
Date: Wed, 25 Oct 2017 21:49:43 +0200

Uh, I’ve accidentally sent my response to just specific people and not to ML. :( So once again and sorry for mess.


>> --- a/main/musl/APKBUILD
>> +++ b/main/musl/APKBUILD
>> _at_@ -54,6 +54,8 @@ build() {
>> fi
>>
>> # note: not autotools
>> + # force -O2 compile flag for better performance
>> + CFLAGS="-O2" \
>> LDFLAGS="$LDFLAGS -Wl,-soname,libc.musl-${CARCH}.so.1" \
>> ./configure \
>> --build=$CBUILD \

This is IMO not correct, CFLAGS does not define just -Os, but more flags (-fomit-frame-pointer). Here you replace all the default flags with -O2.
I suggest to use `CFLAGS="${CFLAGS/-Os/-O2}"` instead, that’s what I already used in some abuilds.

> OTOH higher optimization levels for x86-64 usually tend to give better results than on other archs.

x86_64 is the most used platform, especially when you need performance (and can’t afford IBM’s proprietary architectures). I’d bet that it applies even to Alpine, but don’t have any numbers.


> There were similar changes in aports for various applications over recent months, but I haven’t seen even one proof behind them.
>
> Performance improvements are imporant, and they may come from simply bumping optimization level, but it should be verified, not blindly assumed.

Technically you’re right, it’d be indeed nice to have some real proof. Unfortunately it’s quite hard to make a good benchmark and probably no one of us have time to do that. :(

However, there’s another reason why to prefer -O2. It’s the default optimization level that use almost all upstream projects and even downstream (other distros). So it’s the most tested variant and if the project care about performance, they typically optimize for -O2 (or -O3). But I must mention that I’m really not expert in this field, I can only say what I see as the most used, but not whether it really makes in technical aspects.

I’m the one who has changed -Os to -O2 in _some_ specific abuilds where performance is important, e.g. x264, opus, qemu, postgresql… I actually tried to find some proof for PostgreSQL, but the only performance good comparison I found [1] compares just -O2, -O3, -O4, -march=native and -flto, not -Os.


Just a note: we already compile many aports with -O2 and most of you don’t even know about it and/or didn’t care. These are almost all aports built by CMake. CMake by default doesn’t log what it is really executing, unless you set `-DCMAKE_VERBOSE_MAKEFILE=ON` (that’s why I usually enable it). If you’d enable verbose mode, you would see that -Os is passed to gcc, but it’s followed by -O2 added by CMake. That’s what `-DCMAKE_BUILD_TYPE=Release` do (among others); it’s a profile that predefines various flags including -O and it has higher priority than CFLAGS from environment. If you want -Os, there’s a built-in profile MinSizeRel. You can look which aports use this flag… ;)

However, as I have discovered over time, MinSizeRel is not always usable. Many projects have very bad CMakeLists, they fully or partially ignore CMAKE_BUILD_TYPE or foolishly assumes that release profile is just and only Release, so build sometimes even fails with MinSizeRel.


So, to sum it up, -O2 is the default and most used optimization level in most upstream projects and even other distributions, even we already use it for many aports, whatever you’re aware of it or not. So I’d support changing the default to -O2, at least for x86_64, and change it to -Os in specific abuilds where it makes sense (especially small static binaries).

As a (partial) academic person, I must agree with Przemysław about benchmarking vs. believing, but it’s unfortunately not realistic, at least not for all aports. It’d be really great and useful if someone can measure difference at least for very core components like musl libc.


I’d like to ask Skarnet and Shiz about their opinion and expertise in this topic.


Jakub

[1]: https://blog.pgaddict.com/posts/compiler-optimization-vs-postgresql

> On 25. Oct 2017, at 17:38, Przemysław Pawełczyk <przemoc_at_zoho.com> wrote:
>
> ---- On Wed, 25 Oct 2017 16:46:14 +0200 Natanael Copa <ncopa_at_alpinelinux.org> wrote ----
>> I wonder what you think about overriding the -Os compile flag for musl,
>> and hardcode it to -O2.
>
> I would be very careful with such changes.
>
> There is misconception that the higher optimization level, the faster
> code is generated. That is the general -Olevel idea, but not what is
> seen in practice. Gains (or losses) from higher optimization levels
> vary between archs and obviously depend on the code that is being
> optimized.
>
> Smaller code, beside being smaller, is also more cache-friendly, so -Os
> can be faster than -O2 and often is. OTOH higher optimization levels
> for x86-64 usually tend to give better results than on other archs.
>
> There is no rule. It all depends on:
> - source code,
> - compiler,
> - platform.
>
>>
>> I think this makes sense since the functions in libc are so often used
>> that we want trade better performance at the cost of slightly bigger
>> binary.
>
> This makes sense if we really get better performance with -O2 on all
> platforms AL supports. And to be able to confirm that, it has te be
> measured.
>
>>
>> This means that we override whatever user as set CFLAGS to
>> in /etc/abuild.conf
>>
>> We already do this with zlib.
>
> zlib is a different beast, because it's computational software. It's
> much more natural to see gains from higher -Olevel in that kind of apps.
>
>>
>> What do you think?
>
> There were similar changes in aports for various applications over
> recent months, but I haven't seen even one proof behind them.
>
> Performance improvements are imporant, and they may come from simply
> bumping optimization level, but it should be verified, not blindly
> assumed.
>
> Regards,
> Przemek
>
>
>>
>> diff --git a/main/musl/APKBUILD b/main/musl/APKBUILD
>> index 1938bbb3ca..193002186d 100644
>> --- a/main/musl/APKBUILD
>> +++ b/main/musl/APKBUILD
>> _at_@ -54,6 +54,8 @@ build() {
>> fi
>>
>> # note: not autotools
>> + # force -O2 compile flag for better performance
>> + CFLAGS="-O2" \
>> LDFLAGS="$LDFLAGS -Wl,-soname,libc.musl-${CARCH}.so.1" \
>> ./configure \
>> --build=$CBUILD \
>>
>> -nc
>
>
>
> ---
> Unsubscribe: alpine-devel+unsubscribe_at_lists.alpinelinux.org
> Help: alpine-devel+help_at_lists.alpinelinux.org
> ---
>



---
Unsubscribe:  alpine-devel+unsubscribe_at_lists.alpinelinux.org
Help:         alpine-devel+help_at_lists.alpinelinux.org
---
Received on Wed Oct 25 2017 - 21:49:43 GMT