在C#类内存对齐? [英] Memory alignment of classes in c#?

查看:350
本文介绍了在C#类内存对齐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(顺便说一句这指的是32位操作系统)

(btw. This refers to 32 bit OS)

一些更新:


  • 这绝对是一个定位的问题。

  • This is definitely an alignment issue

有时对齐(不管是什么原因?)是如此糟糕,进入双超过50倍比最快的访问速度慢。

Sometimes the alignment (for whatever reason?) is so bad that access to the double is more than 50x slower than its fastest access.

运行在64位计算机将切断问题的代码,但我认为它仍然是两个时间之间交替(中我可以通过改变双为浮点32位机器上得到了类似的结果)

Running the code on a 64 bit machine cuts down the issue, but I think it was still alternating between two timing (of which I could get similar results by changing the double to a float on a 32 bit machine)

下单展品运行代码没有问题 - 微软,任何机会,你可以从这些家伙Novell的???

Running the code under mono exhibits no issue -- Microsoft, any chance you can copy something from those Novell guys???

复制的东西有没有办法来调整内存类在C#中的分配?

Is there a way to memory align the allocation of classes in c#?

下面演示了(我想!)没有正确对齐双打的捣蛋。它存储在一个班,定时每次运行一个双一些简单的数学,分配一个新的,老毛病又犯了之前运行的变量5定时运行。

The following demonstrates (I think!) the badness of not having doubles aligned correctly. It does some simple math on a double stored in a class, timing each run, running 5 timed runs on the variable before allocating a new one and doing it over again.

基本上,结果看起来像你要么有一个快速,中速或慢速内存中的位置。

Basically the results looks like you either have a fast, medium or slow memory position (on my ancient processor, these end up around 40, 80 or 120ms per run)

我(对我的古代处理器,这些约40,每运行80或120毫秒结束)试图与StructLayoutAttribute玩,但没有喜悦 - 也许别的东西是怎么回事

I have tried playing with StructLayoutAttribute, but have had no joy - maybe something else is going on?

class Sample
{
    class Variable { public double Value; }

    static void Main()
    {
        const int COUNT = 10000000;
        while (true)
        {
            var x = new Variable();
            for (int inner = 0; inner < 5; ++inner)
            {
                // move allocation here to allocate more often so more probably to get 50x slowdown problem
                var stopwatch = Stopwatch.StartNew();

                var total = 0.0;
                for (int i = 1; i <= COUNT; ++i)
                {
                    x.Value = i;
                    total += x.Value;
                }
                if (Math.Abs(total - 50000005000000.0) > 1)
                    throw new ApplicationException(total.ToString());

                Console.Write("{0}, ", stopwatch.ElapsedMilliseconds);
            }
            Console.WriteLine();
        }
    }
}



所以,我看到很多网站的关于结构的互操作的校准页,那么怎么样的班对齐?

So I see lots of web pages about alignment of structs for interop, so what about alignment of classes?

(或者是我的假设错了,还有另外一个问题与上面?)

(Or are my assumptions wrong, and there is another issue with the above?)

谢谢,
保罗。

Thanks, Paul.

推荐答案

有趣的外观在齿轮运行机器。我有一个有点问题的解释为什么有多个不同的值(我有4)当一个双能对齐只有两种方式。我认为调整到CPU缓存行发挥了作用,以及,虽然只增加了3个可能的时间安排。

Interesting look in the gears that run the machine. I have a bit of a problem explaining why there are multiple distinct values (I got 4) when a double can be aligned only two ways. I think alignment to the CPU cache line plays a role as well, although that only adds up to 3 possible timings.

好了,没有什么可以做这件事时,CLR只承诺对齐,这样在32位机器原子更新,保证4个字节的值。这不只是使用托管代码问题,请 C / C ++有这样的问题太多。貌似芯片厂商需要解决这一个。

Well, nothing you can do about it, the CLR only promises alignment for 4 byte values so that atomic updates on 32-bit machines are guaranteed. This is not just an issue with managed code, C/C++ has this problem too. Looks like the chip makers need to solve this one.

如果这是至关重要的,那么你可以用分配Marshal.AllocCoTaskMem(非托管内存),并使用不安全的指针,你可以排列恰到好处。这样的事情你不得不如果你使用SIMD指令代码分配内存做的,他们需要一个16字节对齐。认为这是一个绝望的,虽然移动

If it is critical then you could allocate unmanaged memory with Marshal.AllocCoTaskMem() and use an unsafe pointer that you can align just right. Same kind of thing you'd have to do if you allocate memory for code that uses SIMD instructions, they require a 16 byte alignment. Consider it a desperation-move though.

这篇关于在C#类内存对齐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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