ndk icon indicating copy to clipboard operation
ndk copied to clipboard

Android NDK Clang produces 23% slower binaries than GCC

Open bruenor41 opened this issue 9 years ago • 91 comments


Switching from android gcc to clang produces slower binaries.

Description

I am working on application what emulates old operating system from 90'. I build c++ binaries with android ndk r10e with gcc 4.8. But, because google drops support for gcc, I want switch to clang. After updating ndk to r15 and successful build, I ran benchmarks(in r15 is default compiler clang, instead gcc like it was in ndk r10e). Result is that emulated system runs 23% slower, even without benchmark it is very noticeable when playing more cpu demanding games.

I did none changes to Android.mk and I use -O3 for optimizations. I only updated ndk. Simply said, I only switch between toolchains with : NDK_TOOLCHAIN_VERSION=4.9 or without it

In ndk r15 is still possible switch to gcc 4.9. I did it and I got back lost performance. And even more, seems gcc 4.9 makes slightly better optimizations then gcc 4.8.

From what I read on web I expected that clang will produce faster binaries or the same. And I didn't find special magic clang flags what I must enable for Android.mk.

For a test I returned back to ndk r10e and switched to clang 3.8. Compiled application is again around 20%+ slower. Seems it is not related to ndk r15, but is there long time.

Environment Details

In my application.mk I use this setup

APP_ABI := armeabi armeabi-v7a x86 APP_OPTIM := release APP_STL := stlport_static APP_PLATFORM := android-8 LOCAL_LDLIBS += -lz

APP_PLATFORM is set to 8 but I see in log that minimum was set to 14 automatically

Android.mk APP_CPPFLAGS = -O3 LOCAL_ARM_MODE := arm LOCAL_CFLAGS += -DHAVE_NEON=1 LOCAL_ARM_NEON := true

I tested on windows 10 and ubuntu 16.04, results are same Tested on device with android 6.0 and 7.0

bruenor41 avatar Aug 29 '17 05:08 bruenor41

This isn't really that actionable without a repro case. 20%+ slower seems outside of the bounds of anything we have ever seen (in 7+ years of working on this), but it's hard to do more without further information.

stephenhines avatar Aug 29 '17 05:08 stephenhines

I understand. I can create application what you can test, it takes me 1-2 days. But I don't like make sources public. I can provide them via email to someone from team. Or is it enough to have apks compiled with gcc and with clang?

bruenor41 avatar Aug 29 '17 07:08 bruenor41

Isn't -O3 a bit adventurous (https://github.com/android-ndk/ndk/issues/110#issuecomment-226261466)?

kneth avatar Aug 29 '17 09:08 kneth

Unfortunately I tried all these flags, but none brings back performance https://stackoverflow.com/questions/15548023/clang-optimization-levels

bruenor41 avatar Aug 29 '17 13:08 bruenor41

have you tried adding -Bsymbolic? that's one of the main differences between the default clang flags and the default [Android] GCC flags.

enh avatar Aug 29 '17 15:08 enh

nope, today is feast in our country, but will try tomorrow immedately at morning. Thank you very much for idea

bruenor41 avatar Aug 29 '17 15:08 bruenor41

I created benchmark application. Please check readme.txt in root

bruenor41 avatar Aug 30 '17 09:08 bruenor41

A couple of quick suggestions. I am not sure whether APP_CPPFLAGS (where you set -O3) gets cleared by $(CLEAR_VARS). I will also note that there are several .c source files in this example, which will not get the -O3, since that would require setting APP_CFLAGS instead (which applies to both .c and .cpp files). Instead. these files will be compiled at the Clang default (-O0), which is bound to have performance problems. Since I don't really work on the NDK build side of things, this is my first experience with any of these special make variables, so I am really guessing that not all the flags you expect are being passed to your actual built libraries. LOCAL_CFLAGS is much more familiar to me, and those do get cleared by $(CLEAR_VARS).

stephenhines avatar Aug 31 '17 01:08 stephenhines

Stephen, thank you very much for suggestions! I replaced APP_CPPFLAGS with LOCAL_CFLAGS. It was definitely good idea, seems binaries made by both compilers profits from this change and performs slightly better. Each performance gain is for me important. Unfortunately app compiled by gcc still performs much better :(

bruenor41 avatar Aug 31 '17 06:08 bruenor41

You need to update every place after $(CLEAR_VARS) to add a LOCAL_CFLAGS with your optimization level to have it affect that compilation. Otherwise, I am pretty convinced at this point you are getting -O0 compilation for many of the sources files with Clang.

stephenhines avatar Aug 31 '17 07:08 stephenhines

Here is how I updated them

bruenor41 avatar Aug 31 '17 07:08 bruenor41

Could you just upload that as a github project or at least a gist so we don't have to keep redownloading it?

DanAlbert avatar Aug 31 '17 07:08 DanAlbert

APP_CFLAGS (or any other APP_* variables) does nothing in Android.mk. Application-wide flags need to be set in Application.mk.

DanAlbert avatar Aug 31 '17 07:08 DanAlbert

here

bruenor41 avatar Aug 31 '17 07:08 bruenor41

Which ABI is the one you're having issues with?

DanAlbert avatar Aug 31 '17 08:08 DanAlbert

armeabi-v7a. I don't own x86 based device, I can test it in emulator with haxm enabled, but I didn't. And old armeabi I have only due possible fallback, it is not very used.

bruenor41 avatar Aug 31 '17 08:08 bruenor41

Please has someone idea what might be a problem? I like to switch to clang as soon as possible

bruenor41 avatar Sep 04 '17 06:09 bruenor41

I'll do what I can to minimize a test case for the compiler folks.

DanAlbert avatar Oct 03 '17 01:10 DanAlbert

Thank you very much, do not hesitate contact me if you need something

bruenor41 avatar Oct 03 '17 05:10 bruenor41

How do I build your project?

DanAlbert avatar Oct 12 '17 22:10 DanAlbert