https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128900
Bug ID: 128900
Summary: [reflection] identifier_of costs O(length)
constant-evaluation work: reflect.cc builds the
string_view from a pointer although it knows the
length
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Keywords: compile-time-hog
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: filip.sajdak at gmail dot com
Target Milestone: ---
Host: aarch64-unknown-linux-gnu
Target: aarch64-unknown-linux-gnu
Build: aarch64-unknown-linux-gnu
Created attachment 66237
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=66237&action=edit
proposed patch against trunk 9eb44775b9
std::meta::identifier_of (and display_string_of, symbol_of) cost O(length)
constant-evaluation work per call, because reflect.cc builds the returned
string_view from a pointer alone although it has just computed the length.
eval_identifier_of in gcc/cp/reflect.cc turns the name into a string literal
with get_string_literal and then calls build_special_member_call with one
argument, the address of that literal. So the constant evaluator runs
basic_string_view(const char*), which calls char_traits<char>::length. In
constant evaluation libstdc++'s length is a loop
(__gnu_cxx::char_traits<char>::length) of several calls per character.
get_string_literal has just computed the length with strlen and drops it.
eval_display_string_of, eval_symbol_of and
eval_exception__S_exception_cvt_tofrom_utf8 build their results the same way.
Reproducer: a file with 4000 functions whose names are 40 characters long, and
a twin that calls has_identifier instead of identifier_of. The difference
between the two, divided by 4000, is the cost of one call.
$ python3 - <<'PY'
names = [("f%04d_" % i + "x" * 40)[:40] for i in range(4000)]
for kind, expr in (("id", "std::meta::identifier_of(m).size()"),
("has", "std::meta::has_identifier(m) ? 1 : 0")):
open(f"a_{kind}40.cpp", "w").write(
"#include <meta>\n#include <string_view>\nnamespace ns {\n"
+ "\n".join(f"void {n}();" for n in names)
+ "\n}\nconsteval std::size_t c() {\n std::size_t s = 0;\n"
+ " for (auto m : std::meta::members_of(^^ns,
std::meta::access_context::unchecked()))"
+ f" s += {expr};\n return s;\n}}\nconstexpr auto n = c();\nint main()
{{ return n == 0; }}\n")
PY
$ g++ -std=c++26 -freflection -fsyntax-only a_id40.cpp
Both files compile without diagnostics. The numbers are user-space instructions
retired by cc1plus alone (the command that g++ -### prints), counted with
perf_event_open, the least of three runs. With "x" * 8 and [:8] the names have
8 characters.
compiler a_has40.cpp a_id40.cpp one call, 40 chars
one call, 8 chars
16.2.0 3,414 M 9,409 M 1,499 k
381 k
trunk 9eb44775b9 3,485 M 9,474 M 1,497 k
380 k
16.2.0 + attached patch 3,421 M 3,603 M 45 k
45 k
trunk 9eb44775b9 + attached patch 3,486 M 3,669 M 46 k
44 k
Without the patch a call costs about 35 k instructions more per character of
the name; with it the cost does not grow with the length.
For a real program: one translation unit of a C++26 library that reflects over
about 2000 C functions spends 54.1 G instructions in the front end with 16.2.0
and 48.2 G with the patch (-10.8 %). The output of that library's reference
printer, which reads every identifier, is byte-identical with and without the
patch.
The attached patch (against trunk 9eb44775b9; on releases/gcc-16.2.0 it differs
only in line numbers) lets get_string_literal return the length and calls the
two-argument constructor basic_string_view(const char*, size_t) at the four
sites. The length is the strlen of the translated string, which is what
char_traits::length computed before, so every result has the same value.
Tests:
- trunk 9eb44775b9, make check-gcc-c++ RUNTESTFLAGS="dg.exp=reflect/*": 4962
passes and 38 expected failures, before and after.
- 16.2.0, make check-c++ RUNTESTFLAGS="dg.exp=reflect/* *constexpr*": 8777
passes, 34 expected failures, 13 unsupported, before and after (that tree also
had other experiments of the same study applied).
No new test is included: the result does not change, the gain shows only in the
work done. A test with a low -fconstexpr-ops-limit could catch a regression if
that is wanted.
Not checked: a build with --enable-checking=yes (release checking only);
targets other than aarch64-unknown-linux-gnu; u8identifier_of with non-ASCII
identifiers beyond what g++.dg/reflect covers; whether the four sites should
build the string_view as a CONSTRUCTOR directly (that would depend on
libstdc++'s member names, so the constructor call seemed safer).
Compilers were configured with --enable-languages=c,c++ --disable-bootstrap
--disable-multilib --enable-checking=release, host/build/target
aarch64-unknown-linux-gnu.
This report was prepared with AI assistance. The builds, the measurements and
the test runs were done on a machine of mine.