From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 298FD3858C5F; Thu, 8 Feb 2024 17:11:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 298FD3858C5F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1707412297; bh=FrJqL3mVGNzNTOr3+gA9vXD6GR9LqSb/yiANCBTBruk=; h=From:To:Subject:Date:From; b=iH0e5Yh32MB2AVFIJ7hSwqu2ZeRDEtqjZRKSaqp3I0ZDOgQVpX6pemMR663lifch0 TLoDxFh+yHf7cNcCfW7WQRIfkaMYbJU5xRTG4z/b0hMexzL34n4/m/XAZpAMUfWWV9 p5B57Wj1v/BvNt618odwejQkR6zpOANfOBVGRpsI= From: "pobrn at protonmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/113835] New: compiling std::vector with const size in C++20 is slow Date: Thu, 08 Feb 2024 17:11:36 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 13.2.1 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: pobrn at protonmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D113835 Bug ID: 113835 Summary: compiling std::vector with const size in C++20 is slow Product: gcc Version: 13.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: pobrn at protonmail dot com Target Milestone: --- Consider the following code: #include const std::size_t N =3D 1'000'000; std::vector x(N); int main() {} Then: $ hyperfine 'g++ -std=3Dc++20 -O2 x.cpp' 'g++ -std=3Dc++17 -O2 x.cpp' Benchmark 1: g++ -std=3Dc++20 -O2 x.cpp Time (mean =C2=B1 =CF=83): 4.945 s =C2=B1 0.116 s [User: 4.6= 76 s, System: 0.229 s] Range (min =E2=80=A6 max): 4.770 s =E2=80=A6 5.178 s 10 runs Benchmark 2: g++ -std=3Dc++17 -O2 x.cpp Time (mean =C2=B1 =CF=83): 491.3 ms =C2=B1 24.0 ms [User: 440= .9 ms, System: 46.3 ms] Range (min =E2=80=A6 max): 465.6 ms =E2=80=A6 538.0 ms 10 runs Summary g++ -std=3Dc++17 -O2 x.cpp ran 10.07 =C2=B1 0.55 times faster than g++ -std=3Dc++20 -O2 x.cpp If you remove the `const` from `N`, the runtime will be closer to C++17 lev= els. `-ftime-report` suggests that "constant expression evaluation" is the reaso= n. I imagine this is related to C++20 making std::vector constexpr.=