[Galaxy S 4 Mini Android Development] sysKin:
Main article: http://xdaforums.com/showthread.php?t=2754997

In the mid of 2014, [MENTION=5161352]JustArchi[/MENTION] recognised that all of Android has always been built with -Os option - meaning that the binaries are optimised for small size and not for execution speed. -Os is sometimes necessary (as it was on very early Android devices), but the "usual" build optimisation has always been -O2 meaning optimise for speed. Or -O3, which runs even more optimisations for speed (which might or might not help relative to O2 and will always make compilation slower).

Applying those optimisations has never been easier, because Lollipop is already built with GCC 4.8 (which was very highly recommended, but not in default toolchain, before). So all we need to do is activate -O3 by modifying some makefiles.

Does it do anything

In 0xbenchmark, several graphics-related tests (draw-arc, draw-circle) are up to six times faster. Does it matter? Dunno! But it's fun to see such a thing anyway!

What is THE most important file to modify?

Here: ~/android/system/build/core/combo/TARGET_linux-arm.mk

Line 70 defines "TARGET_arm_CFLAGS", this is the base compiler flags for *kernel* and starts with -O2. You can change that -O2 to -O3 if you want, but if you use F4k's kernel then it will do nothing in practice.

Line 77 defines TARGET_thumb_CFLAGS, this is the base compiler flags for android binaries. It clearly has -Os staring you in the face. Aha!! Change that to -O3 (or -O2) and you've just applied 90% of all compiler optimisations you can make. Build again. Your zip will be larger, this means it worked.

So I did that and it didn't finish building

Heh you're right, I lied. Unfortunately GCC is not as perfect as we'd wish and it will produce new errors or warnings with -O3 flag. Some of them make sense, some baffle me... Here's a list of what I had to change:

Error building android/system/frameworks//media/libstagefright/codecs/amrwbenc/src/isp_az.c, compiler detects array index out of bounds

It doesn't make any sense if you ask me (and I stared at the code a lot). There are two arrays f1 and f2. f1 has size [NC16k + 1] and f2 has size [NC16k], and if f1 is big enough then f2 should be big enough, but GCC disagrees.

For an off chance that GCC is right, I made f2 bigger [NC16k + 1]. Since nobody does sizeof(f2) it causes no change in logic, it marely allocates one extra (unused) word on stack. Made GCC happy.

[edit] this fix is unnecessary on gcc 4.8.5; see "Using SaberMod toolchain to build your CM12" on page 3 for how to use updated gcc[/edit]

Error building android/system/frameworks/av/media/libstagefright/codecs/m4v_h263/dec/src/deringing_[chroma|luma].cpp, cryptic "assuming when assuming" message

Another weird one. It *seems* to me that GCC unrolled a loop, and then looked at the unrolled loop index condition and concluded it's dumb after the unrolling, and complains. Modify:

android/system/frameworks/media/libstagefright/codecs/m4v_h263/dec/Android.mk

Find line: Local_CFLAGS += -Werror

Replace with: Local_CFLAGS += -Werror -Wno-error=strict-overflow

Error linking libhwcomposer, symbols "configRotator", "configMdp" and "updateSource" not found

Those three functions are defined with |inline| in the cpp files, while a compiler option tells gcc that inline implies static (dunno why it works with -Os, it really should never work). Open:

~/android/system/hardware/qcom/display-caf/msm8960/libhwcomposer/hwc_utils.cpp

Line 1238, remove "inline"

Line 1309, remove "inline"

Line 1335, remove "inline"

That is all, should build now!

Anything else uses -Os?

Yes, there's a bunch of Android.mk files that locally override the -O3 flag we just set with -Os (or -O2). You can make a global search, but here's a list of some important ones:

* every makefile under external/chromium_org/. This is the built-in web browser and unfortunately every single makefile does the override, and there's over9000 of them. If you want to change them all, you probably want to write a script of some sort. If you do, share one here lol.

* third_party/android_crazy_linker/src/Android.mk, adds -Os to LOCAL_CFLAGS. I do not know what crazy linker is, but if you want that to be bigger and faster, change that to -O3.

* third_party/zlib/zlib.target.linux-arm.mk, adds -Os to MY_CFLAGS_Release. Zlib sounds like something that should be fast, change that to -O3.

* modules/audio_processing/ lots of .mk files use -Os. How weird. You're only interested in *.target.linux-arm.mk, other targets do not apply to us.

* external/... some of the Android.mk files there use -Os. Some seem to matter.

Phew ~ next up: some more interesting optimisations that worked for me.