haskell-hvr / regex-tdfa

Pure Haskell Tagged DFA Backend for "Text.Regex" (regex-base)
http://hackage.haskell.org/package/regex-tdfa
Other
36 stars 9 forks source link

Re-evaluate `ghc-options: -O2` #7

Closed hvr closed 3 years ago

hvr commented 4 years ago

As cabal check wisely tells us

Warning: 'ghc-options: -O2' is rarely needed. Check that it is giving a real benefit and not just imposing longer compile times on your users.

And it's not clear to me how much -O2 actually improves and whether this actually benefits the vast majority of uses of regex-tdfa and it's proven surprisingly difficult to come across a reasonable benchmark for the regex-base package family.

(UPDATE: I performed a couple of simple benchmarks and the verdict appears to be that compiling regex-tdfa using -O2 instead of -O1 provides gains around 5% to 10%.)

Moreover, it's trivial to control optimization levels on a per-package granularity via cabal.project configuration (see https://cabal.readthedocs.io/en/latest/nix-local-build.html#configuring-builds-with-cabal-project for more details) unless there's a ghc-options: -O2 in place:

package regex-tdfa
    optimization: 2

but the presence of a hardocoded ghc-options: -O2 directive in the .cabal package descriptions harms cabal's ability to control the optimization level.


Quantified compile-time cost of -O2 vs -O1

Compiling regex-tdfa with -O2 has an overhead of about 3x memory usage and 3x the duration; and about 3x the produced object code (i.e. ~4M vs ~11M).

This has known to cause problems on some platforms with limited resources (e.g. ARM via llvm) or with limitations in their object/linker format (e.g. on IBM AIX).

Compiling regex-tdfa with -O2

<<ghc: 104043415488 bytes, 2847 GCs, 142532972/391980968 avg/max bytes residency (43 samples), 1074M in use, 0.001 INIT (0.001 elapsed), 58.295 MUT (61.807 elapsed), 28.058 GC (28.039 elapsed) :ghc>>
10907130 ./dist-newstyle/build/x86_64-linux/ghc-8.4.4/regex-tdfa-1.3.1.0/opt/build/libHSregex-tdfa-1.3.1.0-inplace.a

Compiling regex-tdfa with -O1

<<ghc: 28758028344 bytes, 1120 GCs, 55607452/131720384 avg/max bytes residency (21 samples), 330M in use, 0.001 INIT (0.001 elapsed), 20.151 MUT (21.314 elapsed), 6.150 GC (6.144 elapsed) :ghc>>
 3977534 ./dist-newstyle/build/x86_64-linux/ghc-8.4.4/regex-tdfa-1.3.1.0/build/libHSregex-tdfa-1.3.1.0-inplace.a
andreasabel commented 3 years ago

Closing. -O2 is controlled by a flag and not on by default.