简体   繁体   English

私有静态和私有方法的困境

[英]private static and private methods dilemma

Hi (again with philosophical question),嗨(又是哲学问题),

there is a private static method that does not have an access to instance fields and as far as my readings are concerned those methods provide a little bit of performance improvement.有一个private static方法不能访问实例字段,就我的阅读而言,这些方法提供了一点性能改进。

After you mark the methods as static, the compiler will emit non-virtual call sites to these members.将方法标记为静态后,编译器将向这些成员发出非虚拟调用站点。 Emitting non-virtual call sites will prevent a check at runtime for each call that ensures that the current object pointer is non-null.发出非虚拟调用站点将阻止在运行时对每个调用进行检查,以确保当前对象指针为非空。 This can result in a measurable performance gain for performance-sensitive code.这可以为对性能敏感的代码带来可测量的性能提升。 In some cases, the failure to access the current object instance represents a correctness issue.在某些情况下,无法访问当前对象实例表示正确性问题。

FxCop警察

Then why to use normal private method when we can place an instance field in private static method parameters?那么当我们可以在private static方法参数中放置一个实例字段时,为什么要使用普通的private方法呢? Isn't that inefficient?那不是效率低下吗?

Just to visualize what I mean:只是为了形象化我的意思:

public class A
{
    private readonly string _key = "ssss";

    public bool IsGoodKey()
    {
    }

    private static bool ValidateKeyStatic(string key)
    {
        return string.IsNullOrEmpty(key);
    }
    private bool ValidateKey()
    {
        return string.IsNullOrEmpty(_key);
    }
}

And now there are two ways of implementing IsGoodKey :现在有两种实现IsGoodKey

    public bool IsGoodKey()
    {
        return ValidateKeyStatic(_key);
    }

And

    public bool IsGoodKey()
    {
        return ValidateKey();
    }

Why don't always use private static ?为什么不总是使用private static

If performance would be the entire purpose of static , then of course you could make anything static .如果性能将是static的全部目的,那么您当然可以将任何东西设为static But actually the keyword isn´t about performance , it´s about semantics - that is does the member belong to the class (and thus all instances), or to a specific instance of your class?但实际上关键字不是关于性能,而是关于语义- 即成员属于(以及所有实例),还是属于特定实例?

Imagine you´d create multiple instances of your class.想象一下,您将创建类的多个实例。 If every instance had a _key , you surely need an instance -field, not a static one.如果每个实例都有一个_key ,那么您肯定需要一个实例字段,而不是static字段。 If on the other hand all instances share the same key, you could make it static of course.另一方面,如果所有实例共享相同的密钥,您当然可以将其static

After all performance is just a side-effect of the keyword and you should never make design-decisions on pure performance-considerations.毕竟性能只是关键字的副作用,您永远不应该根据纯粹的性能考虑做出设计决策。

After you mark the methods as static, the compiler will emit non-virtual call sites to these members.将方法标记为静态后,编译器将向这些成员发出非虚拟调用站点。 Emitting non-virtual call sites will prevent a check at runtime for each call that ensures that the current object pointer is non-null .发出非虚拟调用站点将阻止在运行时对每个调用进行检查,以确保当前对象指针为非 null This can result in a measurable performance gain for performance-sensitive code.这可以为对性能敏感的代码带来可测量的性能提升。 In some cases, the failure to access the current object instance represents a correctness issue.在某些情况下,无法访问当前对象实例表示正确性问题。

I may be wrong, but I think you are mixing up the "current object pointer is non-null" check and the check of the argument passed into the static method _key in the static call ValidateKeyStatic(_key);我可能是错的,但我认为您混淆了“当前对象指针为非空”检查和在静态调用ValidateKeyStatic(_key);传递给静态方法_key的参数的检查ValidateKeyStatic(_key);

The first refers to the pointer on which you invoke the method.第一个是指调用方法的指针。 When you write the following call to a non static method:当您编写以下对非静态方法的调用时:

foo.Bar(blah)

An automatic non null runtime check is inserted for foo and if it happens to be null , you get the infamous ReferenceNullException and your program crashes.foo插入一个自动的非空运行时检查,如果它恰好是null ,你会得到臭名昭著的ReferenceNullException并且你的程序崩溃。 This check is avoided if the method is static, hence the performance gain, because the method is not invoked on any particular instance of an object.如果方法是静态的,则可以避免此检查,从而提高性能,因为不会在对象的任何特定实例上调用该方法。

And it has nothing to do with what checks or not you may perfomr on blah insisde the method Bar .它与您可以在blah insisde 方法Bar上执行的检查或不检查无关。

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

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