Delphi 2010内联没用吗?

发布于 2024-08-12 15:58:31 字数 398 浏览 6 评论 0原文

Delphi 中的内联函数或过程是什么(特别是 v2010,但我在 Turbo Delphi 中也遇到了同样的问题)?

帮助中有一些关于它可能并不总是内联函数的警告,因为“某些标准”无论这意味着什么。

但我发现通常内联函数(即使是具有 3 或 4 行代码的非常简单的函数)会减慢代码而不是加快代码速度。

一个好主意是“内联所有内容”的编译器选项。我不在乎我的 exe 是否会增长 50% 左右以使其运行得更快。

有没有一种方法可以强制 Delphi 真正内联代码,即使编译器没有决定内联代码?这确实有帮助。否则,您需要进行“手动内联”,在代码的多个区域中复制过程代码,并添加“//内联失败”之类的注释,因此,如果您更改接下来的 5 行,请在该代码存在的其他 8 个重复点中更改它们“

这里有什么建议吗?

What is the go with inlining functions or procedures in Delphi (specifically v2010 here, but I had the same issue with Turbo Delphi)?

There is some discalimer in the help about it may not always inline a function because of "certain criteria" whatever that means.

But I have found that generally inlining functions (even very simple ones that have 3 or 4 lines of code) slows down code rather than speeds it up.

A great idea would be a compiler option to "inline everything". I don't care if my exe grows by 50% or so to get it working faster.

Is there a way I can force Delphi to really inline code even if it is not decided to be inlinded by the compiler? That would really help. Otherwise you need to do "manual inlining" of replicating the procedure code throughout multiple areas of your code with remarks like "//inlining failed here, so if you change the next 5 lines, change them in the other 8 duplicate spots this code exists"

Any tips here?

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

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

发布评论

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

评论(4

殤城〤 2024-08-19 15:58:31

有一个编译器选项可以自动内联短例程。在项目选项中,Delphi 编译器下 ->编译->代码生成,将“代码内联控制”设置为“自动”。但请注意,这只能在发布版本中进行,因为内联代码很难调试。

另外,你说你不介意让你的程序变得更大,只要它变得更快,但内联通常会使其变慢。您应该意识到这可能是相关的。编译后的代码越大,指令缓存未命中的次数就越多,这会减慢执行速度。

如果您确实想加快程序速度,请通过分析器运行它。我推荐 Sampling Profiler,它是免费的,可以与 Delphi 代码(包括 2010)一起使用,并且不'不要放慢你的执行速度。它将向您显示一份详细报告,说明您实际上花费最多时间执行的代码。一旦发现这一点,您就可以专注于瓶颈并尝试优化它们。

There's a compiler option for automatic inlining of short routines. In Project Options, under Delphi Compiler -> Compiling -> Code Generation, turn "Code inlining control" to Auto. Be aware, though, that this should only be on a release build, since inlined code is difficult to debug.

Also, you said that you don't mind making your program larger as long as it gets faster, but that often inlining makes it slower. You should be aware that that might be related. The larger your compiled code is, the more instruction cache misses you'll have, which slows down execution.

If you really want to speed your program up, run it through a profiler. I recommend Sampling Profiler, which is free, is made to work with Delphi code (including 2010) and doesn't slow down your execution. It'll show you a detailed report of what code you're actually spending the most time executing. Once you've found that, you can focus on the bottlenecks and try to optimize them.

握住你手 2024-08-19 15:58:31

在某些情况下,内联会使事情变慢。内联函数可能会增加局部变量所需的CPU寄存器数量。如果没有足够的寄存器,可用变量将位于内存中,这会使其速度变慢。

如果该函数未内联,它将拥有(几乎)所有可用的 CPU 寄存器。

我发现内联包含循环的函数通常不是一个好主意。他们将使用几个可能最终出现在内存中的变量,从而使内联代码变慢。

Inlining can make things slower in some cases. The inlined function may increase the number of CPU registers required for local variables. If there aren't enough registers available variables will be located in memory instead, which makes it slower.

If the function isn't inlined it will have (almost) all CPU registers available.

I've found that's it's typically not a good idea to inline functions containing loops. They will use a couple of variables which are likely to end up in memory, making the inlined code slower.

拥抱没勇气 2024-08-19 15:58:31

如果您想强制内联,请使用包含文件。您需要确保声明正确的变量,然后使用 {$I filename.inc}。这将始终将特定代码注入到您想要的位置,并且在您需要更改它时更容易维护。

请记住,编译器是由比大多数凡人(包括我自己)更聪明的人编写的,并且在决定是否内联时可以访问更多信息,因此当它不内联时,它可能有一个很好的理由。

If you want to force inlining then use include files. You need to make sure you declare the correct variables, and then use {$I filename.inc}. That will always inject that specific code right where you want it, and make it easier to maintain if you need to change it.

Keep in mind that the compiler is written by people way smarter then most mere mortals (including myself) and has access to more information when deciding to inline or not, so when it doesn't inline it probably has a good reason.

灼疼热情 2024-08-19 15:58:31

如果我正确地理解了 FPC 编译器开发之一(具有相同的问题),则只有当要内联的例程已经编译时才会发生内联。

IOW,如果您将具有内联函数的单元设置为“叶”单元,并将其作为项目 (.dpr) 的 use 子句中的第一个单元,那么应该没问题。请注意,对于“叶”单元,我的意思是不依赖于项目中其他单元的单元,仅依赖于已编译的单元。

我不会对 Delphi 中的情况感到惊讶,因为它共享基于相同原理的单位系统。

如果不违反单独的编译原则,它也是相当无法修复的。

If I understood one of the FPC compiler devels (which has the same issue) correctly, inlining can only happen when the routine to be inline was already compiled.

IOW if you make the unit with the inlined-to-be functions a "leaf" unit, and put it as first in the uses clause of your project (.dpr), it should be ok. Note that with "leaf" unit, I mean a unit that has no dependancy on other units in the project, iow only on already compiled units.

I wouldn't be surprised it was the same in Delphi, since it shares an unit system based on the same principles.

It is also pretty unfixable without violating separate compilation principles.

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