接口文件中没有什么?

发布于 2024-12-09 06:52:09 字数 780 浏览 0 评论 0原文

我的印象是 "AD 接口文件仅包含导入的内容模块需要,而不是该模块的整个实现。” 对我来说,这转化为签名 - 只需返回类型、名称和参数,以便编译器知道它是有效的,并且链接器可以稍后完成肮脏的工作。

不过,通过 dmd 运行文件几乎不会删除任何内容:

import std.stdio;

void SayHello(const string Name)
{
    writeln("Hello, ", Name, "!");
}

dmd Interface.d -o- -H

// D import file generated from 'Interface.d'
import std.stdio;
void SayHello(const string Name)
{
writeln("Hello, ",Name,"!");
}

很难说是优化的典范。

接口文件中到底删除了什么?

添加是因为它我能找到的最接近的东西。)

I was under the impression that "A D interface file contains only what an import of the module needs, rather than the whole implementation of that module." To me, that translates to signatures - just return types, names and arguments, so that the compiler knows it's valid and the linker can do the dirty work later.

Running a file through dmd, though, strips almost nothing:

import std.stdio;

void SayHello(const string Name)
{
    writeln("Hello, ", Name, "!");
}

dmd Interface.d -o- -H

// D import file generated from 'Interface.d'
import std.stdio;
void SayHello(const string Name)
{
writeln("Hello, ",Name,"!");
}

Hardly a paragon of optimization.

What, exactly, is stripped in interface files?

( added because it's the closest thing I could find.)

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

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

发布评论

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

评论(2

╰ゝ天使的微笑 2024-12-16 06:52:09

任何要内联的函数都必须在 .di 文件中拥有其完整源代码。将在 CTFE 中使用的任何函数不仅必须在 .di 文件中拥有其完整源代码,而且它直接或间接使用的每个函数的完整源代码都必须可供编译器使用。此外,由于模板的工作方式,它们的完整源代码也必须位于 .di 文件中(这与 C++ 中模板必须位于头文件中的方式相同)。因此,在很多情况下,您需要将内容放入 .di 文件中。

我不知道编译器到底在什么情况下选择剥离内容(除了模板自动完整地以 .di 文件结束的事实,因为它们必须)。它可能会根据编译器当前的实现及其所做的优化而改变。但至少,如果要进行内联,它必须保留在小函数体中。然而,大型函数体和小型虚拟函数体(无论如何都不能内联)可能会被删除。但是您的示例提供了一个小型的非虚拟函数,因此 dmd 可能会将其保留下来,以便它可以内联对其的任何调用。如果您希望看到 dmd 在生成 .di 文件时删除很多内容,那么您可能需要拥有大型函数和/或使用类。

Any function which is going to be inlined must have its full source in the .di file. Any function which is going to be used in CTFE must not only have its full source in the .di file, but the full source of every function that it uses - directly or indirectly - must be available to the compiler. Also, because of how templates work, their full source must be in the .di file as well (which is the same as how templates must be in header files in C++). So, there are a number of cases where you need stuff to be in a .di file.

Under exactly what circumstances the compiler chooses to strip stuff or not, I don't know (aside from the fact that templates automatically end up in .di files in their entirety because they have to). It could change depending on the compiler's current implementation and what optimizations it does. But at minimum, it's going to have to leave in small function bodies if it's going to do any inlining. Large function bodies and the bodies of small virtual functions (which can't be inlined anyway) will likely be stripped out however. But your example gives a small, non-virtual function, so dmd likely left it in so that it could inline any calls to it. If you want to see dmd strip a lot of stuff when generating a .di file, then you probably need to have large functions and/or use classes.

凯凯我们等你回来 2024-12-16 06:52:09

算不上优化的典范。

不,那一种优化。如果实现足够小以便稍后可以内联,则编译器会将实现保留在接口文件中。

Hardly a paragon of optimization.

No, that is an optimization. The compiler will leave the implementation in the interface file if the implementation is small enough that it can later be inlined.

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