从 GCC 中的单独文件内联函数

发布于 2024-12-11 03:16:27 字数 499 浏览 0 评论 0原文

我的项目中有一个紧密的内部循环,调用了一些辅助函数。 声明为:

BOOL isValidPoint(CGPoint point) __attribute__((always_inline));

为了获得最大性能,这些简单的函数在我的 ImageCommon.h 文件中

inline BOOL isValidPoint(CGPoint point);

在 ImageCommon.m 中,并实现为:在 ImageCommon.m 中。换句话说,我总是希望内联这些函数。

如果我从 ImageCommon.m 中的其他函数调用 isValidPoint,反汇编输出会确认对 isValidPoint 的函数调用已被清除。但是,如果我从另一个源文件调用此函数,则该函数调用将保留在原处 - 它不会内联。

是否可以内联在一个实现文件中调用但在另一个实现文件中定义的函数?

I have a tight inner loop in my project that calls out to a few helper functions. For maximum performance, these simple functions are declared as:

BOOL isValidPoint(CGPoint point) __attribute__((always_inline));

in my ImageCommon.h file, and implemented as:

inline BOOL isValidPoint(CGPoint point);

in ImageCommon.m. In other words, I always want these functions inlined.

If I call isValidPoint from other functions in ImageCommon.m, the disassembly output confirms that the function call to isValidPoint has been wiped out. But if I call this function from another source file, the function call remains in place - it isn't inlined.

Is it possible to inline functions called in one implementation file but defined in another?

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

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

发布评论

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

评论(3

凯凯我们等你回来 2024-12-18 03:16:27

尝试将其声明为“静态内联”,而不仅仅是“内联”。然后,编译器会将其视为翻译单元的本地内容,并且应该确定它确实可以内联。

编辑:抱歉,我没有看到您在翻译单元中定义了此函数。该定义还需要通过“static inline”移动到 .h 才能工作。

Try declaring it 'static inline' instead of just 'inline'. The compiler will then treat it as local to the translation unit and should figure out that it really can inline.

EDIT: Sorry, I didn't see that you had this function defined in a translation unit. The definition will also need to be moved to the .h with 'static inline' to work.

如痴如狂 2024-12-18 03:16:27

如果您希望将函数内联到其他编译单元中,则该函数的定义需要对这些编译单元可见(即,它需要位于标头中)。

或者,您也可以进行链接时优化,具体取决于您使用的编译器。

If you want your function inlined into other compilation units, the definition of that function will need to be visible to those compilation units (i.e., it'll need to be in the header).

Alternatively, you may be able to fool around with link-time optimisation, depending on which compiler you're using.

安静 2024-12-18 03:16:27

我的答案是,您是否遵循 90:10 的优化规则。基本上 90% 的程序员知道要优化什么,因此他们有 10% 的机会猜测需要优化的 10% 代码(抄袭其他规则)

基本上不内联任何内容 -让编译器来做

My answer would be that are you following the 90:10 rule of optimization. Basically 90% of programmers dont know what to optimize so they have a 10% chance of guessing the 10% of code they need to optimize (a rip off from some other rule)

Basically do not inline anything - let the compiler do it

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