为什么完整的“ constexpr”启用数据结构会导致编译的代码更大?

发布于 2025-01-28 23:48:09 字数 325 浏览 4 评论 0原文

at 这个启用每个数据结构(我猜这意味着使每个字段和函数constexpr)可能会导致更大的代码“因为这会导致更多的数据结构被编译到代码中,因此您在该代码中有更多数据数据细分比在运行时计算的内容相比“这句话是他在此时间邮票上实际说过的内容以及他最后所说的内容的结合,以回答有关此主题的问题的回答)。

我真的不明白这意味着什么。为什么ConstexPR数据结构会编译比非constexpr数据结构更大?有人有一个实际示例显示这一点吗?

At this moment of Jason Turner's 2016 CppCon talk "Practical Performance Practices", he mentions that full constexpr enabling of every data structure that can be (I'm guessing that means making every field and function constexpr that can be) can result in bigger code "because this causes more data structures to be compiled into your code so you have more data in the data segment than something that would be calculated at runtime" (this quote is kind of a combination of what he actually said at this time stamp and what he said at the end as an answer to a question about this topic).

I don't really understand what that means. Why would constexpr data structures compile to be bigger than non-constexpr data structures? Does anyone have an actual example that shows this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你没皮卡萌 2025-02-04 23:48:09

在微控制器上实施7位环状冗余检查(CRC)算法时,我发现提前构建256个字节查找表非常方便,并使用类似的代码:

uint8_t crc_table[256];
for (unsigned int i = 0; i < 256; i++)
{
  crc_table[i] = some_crc_function(i);
}

因此,如果您转动crc_table进入constexpr在编译时计算的东西,您的工具链将必须在可执行文件中存储一个256字节的表,从而占用空间。它也能够删除用于生成CRC表的代码,但是如果该代码的机器指令少于256个字节,那么我希望可执行文件会变得更大。

When implementing a 7-bit cyclic redundancy check (CRC) algorithm on a microcontroller, I find it handy to build a 256-byte lookup table ahead of time, with some code like this:

uint8_t crc_table[256];
for (unsigned int i = 0; i < 256; i++)
{
  crc_table[i] = some_crc_function(i);
}

So if you turn crc_table into a constexpr thing that gets computed at compile time, your toolchain would have to store a 256-byte table in your executable, which takes up space. It would also be able to remove the code for generating the CRC table, but if the machine instructions for that code take less than 256 bytes, then I'd expect the executable to get bigger.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文