c#编译器是否决定使用stackalloc本身? [英] Is c# compiler deciding to use stackalloc by itself?

查看:381
本文介绍了c#编译器是否决定使用stackalloc本身?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现一个博客条目,表明有时c#编译器可能会决定将数组放在堆栈而不是堆:



通过堆栈分配提高性能(.NET内存管理:第2部分)



这个人声称:


编译器有时会决定把东西放在栈上。我做了一个TestStruct2的实验,其中我分配了一个不安全和正常的上下文。在不安全的上下文中,数组被放在堆上,但是在正常的上下文中,当我查看内存时,数组实际上已经分配在堆栈上。


有人可以确认吗?

我试图重复他的例子,但每次我尝试在堆上分配数组。



如果c#编译器可以做这样的技巧,而不使用'unsafe'关键字我特别intereded在它。我有一个代码,工作在许多小字节数组(8-10字节长),所以使用堆每个新字节[...]是浪费时间和内存(特别是每个对象在堆上有8字节开销需要垃圾收集器)。



编辑:我只是想描述为什么它对我很重要:

I' m写作库,它与Gemalto.NET智能卡通信,可以有.net代码工作。当我调用一个方法返回的东西,智能卡返回8字节,描述我的确切的返回值的类型。这8个字节通过使用md5哈希和一些字节数组连接计算。

问题是,当我有一个不知道我的数组,我必须扫描所有类型在应用程序加载和每个我必须计算这8个字节,直到我找到相同的数组。

我不知道其他方式来找到类型,所以我尽量加快它尽可能。

解决方案

从您的行中:


正在处理许多小字节数组(8-10个字节长)的代码


我个人更感兴趣的是,备用缓冲区,代码的不同部分可以重复使用(同时处理同一个块)。然后你没有任何创作/ GC担心。在大多数情况下(其中缓冲区用于非常谨慎的操作)使用scratch-buffer,你甚至可以总是假定它是all yours - 即每个需要它的方法都可以假设它们可以从零开始写。 / p>

我在一些二进制序列化代码中使用这种单缓冲方法(而编码数据);它是性能的一大提升。在我的情况下,我在序列化层之间传递一个上下文对象(封装临时缓冲区,输出流(有一些额外的本地缓冲区)和一些其他异常)。


I found a blog entry which suggests that sometimes c# compiler may decide to put array on the stack instead of the heap:

Improving Performance Through Stack Allocation (.NET Memory Management: Part 2)

This guy claims that:

The compiler will also sometimes decide to put things on the stack on its own. I did an experiment with TestStruct2 in which I allocated it both an unsafe and normal context. In the unsafe context the array was put on the heap, but in the normal context when I looked into memory the array had actually been allocated on the stack.

Can someone confirm that?
I was trying to repeat his example, but everytime I tried array was allocated on the heap.

If c# compiler can do such trick without using 'unsafe' keyword I'm specially intrested in it. I have a code that is working on many small byte arrays (8-10 bytes long) and so using heap for each new byte[...] is a waste of time and memory (especially that each object on heap has 8 bytes overhead needed for garbage collector).

EDIT: I just want to describe why it's important to me:
I'm writing library that is communicating with Gemalto.NET smart card which can have .net code working in it. When I call a method that returns something, smart card return 8 bytes that describes me the exact Type of return value. This 8 bytes are calculated by using md5 hash and some byte arrays concatenations.
Problem is that when I have an array that is not known to me I must scan all types in all assemblies loaded in application and for each I must calculate those 8 bytes until I find the same array.
I don't know other way to find the type, so I'm trying to speed it up as much as possible.

解决方案

From your line:

I have a code that is working on many small byte arrays (8-10 bytes long)

Personally, I'd be more interested in allocating a spare buffer somewhere that different parts of your code can re-use (while processing the same block). Then you don't have any creation/GC to worry about. In most cases (where the buffer is used for very discreet operations) with a scratch-buffer, you can even always assume that it is "all yours" - i.e. every method that needs it can assume that they can start writing at zero.

I use this single-buffer approach in some binary serialization code (while encoding data); it is a big boost to performance. In my case, I pass a "context" object between the layers of serialization (that encapsulates the scratch-buffer, the output-stream (with some additional local buffering), and a few other oddities).

这篇关于c#编译器是否决定使用stackalloc本身?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆