Skip to content

Massive binary bloat in std::print #163002

@vitaut

Description

@vitaut

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

#include <print>

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} (fmt-print.cc)

#include <fmt/base.h>

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:

template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563).
_LIBCPP_HIDE_FROM_ABI inline void vprint_unicode(string_view __fmt, format_args __args) {
std::vprint_unicode(stdout, __fmt, __args);
}

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    libc++libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions