C# 中的 __fastcall 约定

发布于 2024-10-16 04:08:44 字数 197 浏览 5 评论 0原文

考虑到:

微软特定

__fastcall 调用约定 指定函数的参数 将在寄存器中传递,当 可能的。下面的列表显示了 这个调用的实现 约定。

而且寄存器中的读/写时间比堆栈中的读/写时间快得多,我们在 C# 中是否有等效的 __fastcall ?

Considering that:

Microsoft Specific

The __fastcall calling convention
specifies that arguments to functions
are to be passed in registers, when
possible. The following list shows the
implementation of this calling
convention.

And that the read/write time in a register is way faster than in a stack, do we have any __fastcall equivalent in C#?

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

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

发布评论

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

评论(3

同尘 2024-10-23 04:08:44

不是直接使用,C# 主要使用与 MSVC++ 的 __stdcall 约定等效的内容。然而,它可以被“修复”,尽管是以相对肮脏的方式(请注意示例为 __cdecl)。

不过,这样可能是最好的。在像 C# 这样的高级语言中(哎呀,甚至在大多数 C++ 程序中),这是最好留给编译器的优化。强制调用约定通常会让事情变得更糟。即使它有帮助,通常也不会给你带来太多好处,至少在我使用它的 C 和 C++ 程序中是这样。

Not directly, C# mostly uses what would be equivalent to MSVC++'s __stdcall convention. It can however be "fixed", though in a relatively dirty way (note that example is for __cdecl).

It's probably best this way, though. In a high-level language like C# (heck, even in most C++ programs) this is an optimization best left for the compiler. A forced calling convention can often make things worse. And even when it helps, it usually doesn't buy you much, at least in the C and C++ programs where I have used it.

め可乐爱微笑 2024-10-23 04:08:44

__fastcall 会自动使用,但仅在某些条件下使用。这是一篇关于这个主题的有趣文章:

2.一个方法中的参数不能超过七个。

其背后的原因是在.net中
前两个参数更快
比最后两个参数。让我
解释得更清楚。在 C# 中
每当一个方法被调用时
参数被压入堆栈,
然后由该方法使用。现在
Microsoft 的编译器(在 X86 中)有一个
先进的优化技术称为
__FASTCALL,其中前两个
参数被发送为
寄存器。现在据说这些
注册。好吧之后
注册,变量或
参数有快速通道提升
拥有独家特权
存储在最快的处理器中
缓存。请注意,这通常是为了
我们在循环期间使用的变量“i”
或迭代,因此它的访问
并且使用确实变得非常快。
因此,在编译过程中该方法
被编译为本机代码
.Net 运行时具有 __FASTCALL 操作,因此
一种数量较少的方法
参数比
参数太多。

来源

__fastcall is used automatically but only in certain conditions. Here is an interesting article about this subject :

2.Not more than seven parameters should be there in a method.

The reason behind it is that in .net
the first two parameters are faster
than the last two parameters.Let me
explain it more clearly. In C#
whenever a method is called the
parameters are pushed into the stack ,
which are then used by the method. Now
Microsoft’s compilers(in X86) have an
advanced optimization technique called
the __FASTCALL, wherein the first two
parameters are sent across as
registers. These are now said to have
become enregistered. Well after
registration ,the variable or
parameter has fast track promotion
with exclusive privilege of being
stored in the processor’s fastest
cache. Do note this is usually done to
the variable “i” we use during looping
or iteration, due to which its access
and usage become really fast indeed.
Thus, during compilation the method
are compiled into native code by the
.Net runtime with __FASTCALL action so
a method with less number of
parameters is much more optimized than
that with too many parameters.

Source

终陌 2024-10-23 04:08:44

LadaRaider,在 32 位 Arch 上,这意味着“最大寄存器的最大大小为 4 字节”,如果你传递一个需要 8 字节的“Long Long”,它将使用 2 个 4 字节的寄存器,这就是编译器处理它的方式。假设你只能使用 3 个 4 字节的寄存器,因此,你不能传递 2 个“Long Long”变量……有些数据必须进入内存,这要慢得多。

LadaRaider, on 32-bit Arch which means "Maximum size of the biggest registers is 4 Bytes" if u pass an "Long Long" which takes 8 Bytes it will use 2 registers of 4 Bytes, that's how the compiler deals with it. Let's say u get to use only 3 registers of 4 Bytes, so, u can't pass 2 "Long Long" variables for example... Some data will have to go into the memory which is a lot more slower.

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