Sunday, June 04, 2023

program compiled with clang from base runs 4 times slower than compiled with gcc-11.2.0p6 from ports

I am puzzled with performance of a C program compiled with clang from base.

The program in question is enchive [1]
Most of the time I use it on macos or linux, but recently I had to install it on openbsd.
I compiled it with default clang from base, and the first thing that struck me was long time it
took to extract archive. The same operation takes less than 2 seconds on macos and 12 seconds
on openbsd.

I asked the author on github [2], and he mostly pointed at the compiler.

Following his advise, I did my own testing.

I compiled enchive with default cc (which is clang 13)
make clean
make

I created a test zero file:

$ dd if=/dev/zero of=zero bs=1M count=512
512+0 records in
512+0 records out
536870912 bytes transferred in 1.393 secs (385381071 bytes/sec)

Then I archived the zero file:

$ time enchive a <zero >/dev/null
0m55.08s real 0m49.55s user 0m03.38s system

Next, I installed gcc-11.2.0 from ports and recompiled enchive:

make clean
make CC=egcc

Then I ran the same test:

$ time enchive a <zero >/dev/null
0m14.37s real 0m12.91s user 0m00.35s system

The program uses only libc:

$ ldd enchive
enchive:
Start End Type Open Ref GrpRef Name
00000f5c9cdbe000 00000f5c9ce0e000 exe 1 0 0 enchive
00000f5ed3724000 00000f5ed381a000 rlib 0 1 0 /usr/lib/libc.so.97.0
00000f5f8523a000 00000f5f8523a000 ld.so 0 1 0 /usr/libexec/ld.so

Why gcc produces a binary that runs 4 times faster than binary compiled with clang?

Am I missing any compiler flags for clang? Makefile defines
CFLAGS = -ansi -pedantic -Wall -Wextra -O3 -g3

This is all on 7.3-release system.

Thanks for any pointers to the gaps in my knowledge of compilers.

-Kastus

1. https://github.com/skeeto/enchive
2. https://github.com/skeeto/enchive/issues/31

No comments:

Post a Comment