Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding -DCMAKE_C_FLAGS_RELEASE="-static" -DCMAKE_EXE_LINKER_FLAGS="-static" slows down avifenc runtime #2633

Open
FrankGalligan opened this issue Feb 21, 2025 · 6 comments
Assignees

Comments

@FrankGalligan
Copy link
Contributor

Following this guide calling avifenc is slower than compared to building with this cmake line

cmake -S libavif -B libavif/build -DBUILD_SHARED_LIBS=OFF -DAVIF_CODEC_AOM=LOCAL -DAVIF_LIBYUV=LOCAL -DAVIF_LIBSHARPYUV=LOCAL -DAVIF_JPEG=LOCAL -DAVIF_ZLIBPNG=LOCAL -DAVIF_BUILD_APPS=ON

Steps to reproduce:

  1. Download jpeg image from here
  2. Build avifenc according to the guide.
  3. Build avifenc removing the flags.
  4. Run time ./avifenc happy_dog.jpg example.avif
  5. Run time ./avifenc_no_flags happy_dog.jpg example.avif

On my machine this was the output from step 4:

Wrote AVIF: example.avif

real	0m2.421s

And output from step 5:

Wrote AVIF: example.avif

real	0m0.738s
@wantehchang
Copy link
Collaborator

The -DCMAKE_C_FLAGS_RELEASE="-static" option is problematic. The default value of CMAKE_C_FLAGS_RELEASE depends on the compiler. For GCC and Clang on Linux, CMAKE_C_FLAGS_RELEASE is -O3 -DNDEBUG. Replacing it with -static means compiler optimizations are disabled and assertions are left enabled.

Frank, could you try changing -DCMAKE_C_FLAGS_RELEASE="-static" to -DCMAKE_C_FLAGS_RELEASE="-static -O3 -DNDEBUG"?

@FrankGalligan
Copy link
Contributor Author

FrankGalligan commented Feb 22, 2025

That fixed the performance issue.

@wantehchang
Copy link
Collaborator

Yannis: You added -DCMAKE_C_FLAGS_RELEASE="-static" -DCMAKE_EXE_LINKER_FLAGS="-static" in e9db3c3f1. Do you remember why? Is it because we are not passing -DBUILD_SHARED_LIBS=OFF to the external dependencies built by cmake's FetchContent method?

If it is necessary to use the -static linker flag, we can set the following three environment variables before the first cmake command in https://github.com/AOMediaCodec/libavif?tab=readme-ov-file#build-everything-from-scratch:

export CFLAGS=-static
export CXXFLAGS=-static
export LDFLAGS=-static

But it doesn't seem useful to generate fully static binaries. If I don't use the -static linker flag, the avifenc binary I built only depends on the system libraries:

$ ldd libavif/build/avifenc
	linux-vdso.so.1 (0x00007fb6a01bb000)
	libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fb6a00b4000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fb69f417000)
	/lib64/ld-linux-x86-64.so.2 (0x00007fb6a01bd000)

@y-guyon
Copy link
Collaborator

y-guyon commented Feb 24, 2025

Yannis: You added -DCMAKE_C_FLAGS_RELEASE="-static" -DCMAKE_EXE_LINKER_FLAGS="-static" in e9db3c3f1. Do you remember why?

The previous line states

For development and debugging purposes, or to generate fully static binaries:

so it makes sense to remove compiler optimizations and to make sure it is fully static.
Fully static can be useful when generating binaries that run on distributed frameworks.

Is it because we are not passing -DBUILD_SHARED_LIBS=OFF to the external dependencies built by cmake's FetchContent method?

Maybe that but probably not only.

If I don't use the -static linker flag, the avifenc binary I built only depends on the system libraries

So it is not "fully static" then?

@wantehchang
Copy link
Collaborator

Thanks for the reply. I understand it now.

For debugging purposes, it is also helpful to include symbolic information for the debugger (e.g., the -g compiler option). So the current command line is also problematic.

We can consider splitting "For development and debugging purposes" and "to generate fully static binaries":

  • For development and debugging purposes: pass -DCMAKE_BUILD_TYPE=Debug (include symbolic debug info, enable assertions, disable compiler optimizations), or -DCMAKE_BUILD_TYPE=RelWithDebInfo (include symbolic debug info, disable assertions, enable compiler optimizations).
  • To generate fully static binaries: set the CFLAGS, CXXFLAGS, and LDFLAGS environment variables to -static before running the cmake configure command.

@y-guyon
Copy link
Collaborator

y-guyon commented Feb 25, 2025

We can consider splitting "For development and debugging purposes" and "to generate fully static binaries"

Done in #2642.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants