Tweaking compilation for performance with GCC -O3

I am trying to squeeze some processing, security and possibly later storage space tweaking the compilation

I understand that the firmware built "as is" downloadable in openwrt domains is built with -O2. I've managed to compile with O3 as flag; however any other significant changes to the kernel end up breaking the build midway through

Old threads about performance optimization are from 2012, and it seems that those are already integrated into the stock openwrt firmware

with that said

  • is it safe trying out any compiled firmware that succeeds? Or should I just test it out somewhere else (e.g a VM?) If it "bricks" during installation, would it be possible to recover?

  • Which tweaks to GCC itself could affect performance, besides the main ones (march, mtune, etc) and the builtin profiles that we see in make menuconfig?

  • can telephony be excluded if the router will only need PPoE?

thank you in advance

O3 ignores parentheses in math and overshoots in memcpy ñnd makes code bigger.
What is your device?

2 Likes

TP-Link Archer AX23

factory.bin with -O3 ended up being 6.9MiB vs 6.6MiB from the default settings
would these optmizations have any effect on performance to justify the extra 300KiB?

I'm building from v23.05.5 tag

Better hope with 24.10 enabling crypto accelerator speeding up some ipsec and cryptsetup. It will not significantly improve firewall forwarding (400Mbps) clamped at CPU, or soft offload (1.xGbps) clamped at SoC memory speed. Just add some inaccuracies in critical parts of system.

1 Like

then I think I wiull try out 24.10rc

1 Like

There's two things with this:
binaries the kernel runs
and the kernel.

To test it out with binaries the kernel runs, you just change your openwrt build config, I mean it generally should be fine, just the size of the binaries will be a little bigger/use more memory and in general any program that would benefit a lot from it probably already has it set e.g openssl This won't effect the kernel at all, it will still be compiled Os or O2. So no real change to basic network performance at all.

To do it for the kernel you need actual patches and that's a more risky thing then on whether it will boot or run as you expect, it's generally ok though, but again it will be larger/use more memory and who knows what real gains you will see. Generally not worth it.

1 Like

IIRC official OpenWrt is built with -Os, not O2.

You can edit the -mcpu flag to include additional available extensions, enable Link-Time Optimisation, GCC Graphite, and use mold linker, if supported. Improvements are usually negligible, however.

Base your build on the default config, and only select the packages you need. Packages not selected will not be built, you usually don't need to exclude feeds manually.

Consider OpenSSL instead of mbedtls. Consider patching ustream-ssl to use AES by default, if AES instructions are available(extremely miniscule luci https performance improvement).

Check whether your device includes dedicated crypto accelerator. This will offload Wi-Fi crypto from the CPU.

Use official pre-compiled recovery and u-boot, and you shouldn't have issue debricking your devices.

1 Like

Bit of a nightmare setting since gcc 14. It's why I think i'll be sticking to gcc 13 for arm64.

1 Like

Theres kernel extra CFLAGS in advanced configuration from the build system, wouldn't setting -03 there override the gcc flag the kernel is built on?

I'm tweaking only a few things per build before testing, so the first one will be dnsmasq-full and openssl

is uencrypt-openssl a drop in replacemenet for mbedtls, or should I include both in the build for it to work?

Can you recover device if your miscompiled openwrt does not boot?

since we don't modify uboot, just the firmware, it should be fine booting into the failsafe mode

but I'm not really sure

Will the unmodified bootloader flash openwrt, if your artesanal kernel crashes?

OpenSSL and mbedtls are libraries used by other programs, install them directly have no effects.

uencrypt-openssl is an encryption utility.

To use OpenSSL, replace wpad-basic-mbedtls with wpad-basic-openssl or equivalent, and libustream-mbedtls with libustream-openssl.

I think AX23 has eip93 hardware crypto, enable OpenSSL devcrypto to try it out. I never used af_alg myself.

lease times stop showing in the gui for me when using dnsmasq-full so I don't use it, maybe because I forgot to untick something in the menuconfig

Failsafe is an OpenWrt thing, so do make sure you can recover from the bootloader or you end up needing to rewrite your flash chip with an SPI programmer.

After that you can play around as much as you'd like.

1 Like