llvm / llvm/llvm-project

Massive binary bloat in std::print

Open
#163,002 4 comments 0 reactions 0 assignees View on GitHub
format libc++
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Consider the following trivial program (`std-print.cc`):

```c++
#include

int main() {
std::print("{}\n", 42);
}
```

Compiling it with libc++ and Apple clang version 17.0.0 (clang-1700.3.19.1) on macOS results in a ~300kB object file:

```
% c++ -c -std=c++23 std-print.cc
% ls -l std-print.o
-rw-r--r-- 1 viz staff 297384 Oct 11 07:07 std-print.o
```

For comparison, an equivalent program using [{fmt}](https://github.com/fmtlib/fmt) (`fmt-print.cc`)

```c++
#include

int main() {
fmt::print("{}\n", 42);
}
```

compiles to less than 1kB.
```
% c++ -c -std=c++23 fmt-print.cc -I include
% ls -l fmt-print.o
-rw-r--r-- 1 viz staff 968 Oct 11 07:08 fmt-print.o
```

So the implementation of `std::print` in libc++ generates ~300 times (!) more binary code and is ~5.5 times slower to compile. This is likely due to unnecessary templatization and defining in the header of `vprint_unicode`:

https://github.com/llvm/llvm-project/blob/7eee67202378932d03331ad04e7d07ed4d988381/libcxx/include/print#L387-L390

`std::print` was explicitly designed such that `vprint*` and other similar functions were defined in library source files and not headers specifically to avoid such bloat and slow compile times.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.