简体   繁体   English

安全代码中的不安全标签

[英]Unsafe tag in the safe code

There is a guy on the project I'm helping with that puts unsafe tag everywhere in the performance important code, whether it is needed, or not. 在我正在帮助的项目中,有一个人将unsafe标签放置在性能重要代码中的任何地方,无论是否需要它。 Usually this tag can be removed safely, but unfortunately it's very hard to prove him, that he should not do that. 通常,可以安全地删除此标签,但是不幸的是,很难证明他不应该这样做。 Can anybody give me a good authority article, that proves my or his point, so we could finally close the question? 谁能给我一篇很好的权威文章,证明我或他的观点,以便我们最终可以解决这个问题?

An example of the code where he used unsafe: 他使用不安全代码的示例:

            unsafe
            {
                writeableBmp.CopyPixels(new Int32Rect(1, 0, (int)(width - 1), (int)height - 1), buffer, writeableBmp.BackBufferStride, 0);
                writeableBmp.Lock();
                writeableBmp.WritePixels(new Int32Rect(0, 0, (int)width - 1, (int)height - 1), buffer, writeableBmp.BackBufferStride, 0, 0);
                writeableBmp.AddDirtyRect(new Int32Rect(0, 0, (int)width - 1, (int)height - 1));
                writeableBmp.Unlock();
            }

His point is that he read somewhere that unsafe is needed, and improves performance by giving a hint to compiler, that the code piece should be compiled differently. 他的观点是,他在某个地方读到了一些unsafe的信息,并通过提示编译器来提高性能,即应以不同的方式编译代码段。

Well according to http://msdn.microsoft.com/en-us/library/t2yzs44b.aspx unsafe only creates an unsafe context. 根据http://msdn.microsoft.com/zh-CN/library/t2yzs44b.aspx的说法,不安全只会创建一个不安全的上下文。 This is only required when using pointer arithmetic, etc. which is regarded as unsafe and therefore need an unsafe context being created. 仅在使用指针算术等(这被认为是不安全的,因此需要创建不安全的上下文)时才需要这样做。

In any other case it does not benefit at all as it does not disable anything. 在任何其他情况下,它都不会带来任何好处,因为它不会禁用任何内容。

Profile a code with and without it or check the MSIL to prove it. 使用和不使用代码分析代码,或检查MSIL进行验证。

Just remove it, if there is unsafe code, the compiler will tell you. 只要删除它,如果有不安全的代码,编译器会告诉您。

Some approach to it: 一些解决方法:

using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace CSharpTestbench
{
    class Program
    {
        static void Main(string[] args)
        {

            int x = 42;
            if (x == 42)
            {
                Console.WriteLine("it is 42");
            }
        }
    }
}

Looks in the non optimized MSIL code that way: 以这种方式查看未优化的MSIL代码:

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       30 (0x1e)
  .maxstack  2
  .locals init ([0] int32 x,
           [1] bool CS$4$0000)
  IL_0000:  nop
  IL_0001:  ldc.i4.s   42
  IL_0003:  stloc.0
  IL_0004:  ldloc.0
  IL_0005:  ldc.i4.s   42
  IL_0007:  ceq
  IL_0009:  ldc.i4.0
  IL_000a:  ceq
  IL_000c:  stloc.1
  IL_000d:  ldloc.1
  IL_000e:  brtrue.s   IL_001d
  IL_0010:  nop
  IL_0011:  ldstr      "it is 42"
  IL_0016:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_001b:  nop
  IL_001c:  nop
  IL_001d:  ret
} // end of method Program::Main

Decorating it with unsafe result in: 用不安全的方式装饰它会导致:

.method private hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       32 (0x20)
  .maxstack  2
  .locals init ([0] int32 x,
           [1] bool CS$4$0000)
  IL_0000:  nop
  IL_0001:  nop
  IL_0002:  ldc.i4.s   42
  IL_0004:  stloc.0
  IL_0005:  ldloc.0
  IL_0006:  ldc.i4.s   42
  IL_0008:  ceq
  IL_000a:  ldc.i4.0
  IL_000b:  ceq
  IL_000d:  stloc.1
  IL_000e:  ldloc.1
  IL_000f:  brtrue.s   IL_001e
  IL_0011:  nop
  IL_0012:  ldstr      "it is 42"
  IL_0017:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_001c:  nop
  IL_001d:  nop
  IL_001e:  nop
  IL_001f:  ret
} // end of method Program::Main

Both are compiled with /unsafe of course. 两者都是用/ unsafe编译的。 Actually the unsafe code has more instructions which yield in a slightly larger executable but should be neglectable. 实际上,不安全的代码具有更多的指令,这些指令产生的可执行文件稍大,但应该可以忽略。 The unsafe calls are just replaced by a NOP CIL instruction that literally "does nothing" expect increasing the size of the executable ( http://en.wikipedia.org/wiki/List_of_CIL_instructions ). 不安全的调用仅由NOP CIL指令代替,该指令实际上“什么都不做”,期望增加可执行文件的大小( http://en.wikipedia.org/wiki/List_of_CIL_instructions )。

It increases the indention and makes the code harder to read as it also implies that there is unsafe code. 它增加了缩进量,并使代码更难阅读,因为这也意味着存在不安全的代码。

R# also says that is unnecessary. R#还说这是不必要的。

What proof does the developer has, that supports this claim? 开发者有什么证据可以证明这一主张?

Here's your link: http://en.wikipedia.org/wiki/Premature_optimization#When_to_optimize 这是您的链接: http : //en.wikipedia.org/wiki/Premature_optimization#When_to_optimize

Apart from the reason that he is completely and utterly wrong in assuming unsafe makes the code faster, using random keywords to maybe affect the performance is not how any professional develops code. 除了他认为不安全会完全使代码更快的原因外,使用随机关键字来影响性能并不是任何专业人员开发代码的原因。

First, write the code, with a good balance of performance to code complexity - ie if you can write cleaner coder that runs faster, do it. 首先,编写代码,并在性能和代码复杂度之间取得良好的平衡-即,如果您可以编写运行速度更快的更干净的编码器,请执行。 If you can write messier code that runs even faster, don't. 如果您可以编写运行得更快的更混乱的代码,请不要这样做。 Second, figure out if the performance is sufficient. 其次,找出性能是否足够。 If it is, you're done. 如果是这样,那么您就完成了。 If it's not, figure out which part is affecting your performance the most, fix that one, and leave the other parts alone. 如果不是,请找出哪个部分对您的性能影响最大,请修复该部分,而其他部分则不予处理。

Back to unsafe: Unsafe is not faster. 回到不安全:不安全不会更快。 It allows you to write faster less safe (aka unsafe) code if you know how to do so, by allowing you to compile code that otherwise won't be compiled. 通过允许您编译本来不会被编译的代码,它使您能够编写更快,更不安全(又称为不安全)的代码。 But putting unsafe around normal code is not going to make any positive performance difference. 但是,将不安全的代码放在普通代码周围不会带来任何积极的性能差异。

Unfortunately for you, your coworker is so far off that it will be difficult to find an article dedicated to the question "is unsafe code faster by default". 对于您来说不幸的是,您的同事相距甚远,以致于很难找到专门针对“默认情况下不安全代码会更快”这一问题的文章。 Because it is neither stated nor implied anywhere that unsafe code is faster by default, the burden of proof should be on him. 因为无论在任何地方都没有声明或暗示不安全的代码默认情况下会更快,所以举证责任应由他承担。 But without any capable seniors or managers around it may be difficult to convince him that that's the case. 但是,如果没有任何能干的前辈或管理人员,可能很难说服他就是这种情况。 So the best point of attack I can see is the premature optimization one - if his more complex code is faster he must prove it. 因此,我能看到的最佳攻击点是过早的优化-如果他的复杂代码更快,则必须证明这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM