d12frosted / homebrew-emacs-plus

Emacs Plus formulae for the Homebrew package manager
MIT License
2.24k stars 175 forks source link

[low prio] [feature inquiry] Performance CFLAGS (-O2, -march=native, etc.) #614

Open stradicat opened 9 months ago

stradicat commented 9 months ago

Hi!

I've noticed that emacs-plus is built by default with -Os, which tries to minimize code size while optimizing towards -O2 level.

However, since the hardware where Homebrew runs has large-ish cache sizes (oldest officially BigSur-compatible machines have 256kb of L2 & 3Mb of L3 caches, like the 2013 11-inch MacBook Air), is there a specific reason not to go with -O2? (At least when building --with-native-compilation) I was also wondering if adding -march=native would make sense, since the formula is compiled on the destination machine anyway.

For comparison, the following options are enabled on Linux builds of Emacs: (Ubuntu case, kelleyk PPA)

CFLAGS="-pipe -O2 -flto=auto -ffat-lto-objects -fstack-protector-strong" CPPFLAGS="-Wdate-time -D_FORTIFY_SOURCE=2" LDFLAGS="-Wl,-Bsymbolic-functions -Wl,-z,relro"

d12frosted commented 9 months ago

Hey @stradicat

Thanks for raising this question. To be honest, Emacs+ doesn't do anything special with O flags. You can check the source code of the formula. So we are using whatever Emacs build tools are using.

is there a specific reason not to go with -O2?

There is no specific reason 😄 So it would be a nice contribution.

I was also wondering if adding -march=native would make sense, since the formula is compiled on the destination machine anyway.

Not aware of this flag. What does it do? If my intuitive understanding is correct, it's OK to add this for cocoa flag (some people use Emacs+ on Lunux).

stradicat commented 9 months ago

Thanks for looking into this!

In Linux packages, -O2 is a commonly used optimization flag for Emacs compilation, which optimizes space-speed tradeoff; the resulting compiled executable runs a bit faster than -Os (optimized for code size, same as emacs-plus' defaults), and it's more noticeable on older machines (I'm still using an Intel Mac with OpenCore Legacy Patcher, and I also compile Emacs from source on some i386 PCs). https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

Since emacs-plus is compiled from source and the host machine is its own target, throwing in -march=native enables/disables optimization flags following the host's own CPU architecture, more often than not improving execution speed by a margin.

Taking into account that Emacs is an interpreter, above all, this can give it a small performance edge, noticeable in terms of latency related to user input.

What do you think?

stradicat commented 9 months ago

Better yet: I'll run some measurements and come back with a PR.

d12frosted commented 9 months ago

What do you think?

What you say makes total sense. So I will be happy to see your PR 😸

P.S. Thanks for explanations 🙏