1

Straight up question: If I run code analysis, it tells me to make methods static even in nonstatic classes. As far as I know, static methods are JITed and run on the Type-Object in the Heap. So wouldn't make a method static in a non static class mean, that the instance has to search the type object in the Heap and run the method there?

Wouldn't that mean a performance issue? Sure it would not be that big of a deal, but I'd still be interested on this.

rene
  • 41,474
  • 78
  • 114
  • 152
Matthias Müller
  • 3,336
  • 3
  • 33
  • 65
  • Potential duplicate: http://stackoverflow.com/questions/874363/what-is-better-static-methods-or-instance-methods – Philip Pittle Nov 12 '14 at 11:50
  • This is performance issue. Non-static method has hidden parameter - reference to the class instance. If reference is not used, it is better to make this method static, excluding additional method parameter. – Alex F Nov 12 '14 at 11:50
  • Also interesting: http://stackoverflow.com/questions/135020/advantages-to-using-private-static-methods – Philip Pittle Nov 12 '14 at 11:51
  • 1
    http://stackoverflow.com/a/12279898/113683 gives a thorough explanation – Jonny Cundall Nov 12 '14 at 11:52
  • If you are considering use static methods, remember, static implies thread safety!!! A static method can be called from any thread at any time and it's up to the static method to make sure it's properly handling concurrency. For that reason, I generally avoid static methods that aren't strictly necessary. – Philip Pittle Nov 12 '14 at 11:59
  • @PhilipPittle Common sense: If code analysis tells him to make a method static, it is because it never references anything in the object - that is how the rule is implemented. Which means by definition it can not change the object. – TomTom Nov 12 '14 at 12:24
  • @TomTom - There are additional race conditions that can occur that have nothing to do with the host object. If the static method calls a data service (ie database) in a non thread-safe way, this can cause race conditions. – Philip Pittle Nov 12 '14 at 12:27
  • And how would they call the database in a non thread safe way? WIthout the same applying to instance methods (like using a non thread save static db connection class)? Remember: The analysis proposes the methods to be made static because THEY HAVE NO STATE IN THE OBJECT. – TomTom Nov 12 '14 at 12:56

3 Answers3

3

No, it doesn't work like that.

A static method is actually (imperceptibly) more efficient than a non-static one because (a) it does not have a hidden "this" pointer passed to it and (b) because it's static, the framework doesn't have to do anything about it being virtual (although that last point also applies to non-virtual member methods too, of course).

Here is an in-depth article about CLR runtime type handling. In particular, look at the information there about the MethodTable and the Method Slot Table.

Here's another good article from Joe Duffy. It doesn't explicitly talk about static methods, but it does explain how method calls are made at the lowest (assembler) level, so you would be able to see why a static method call is efficient.

Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • Ok this seems right, but it has to search for the type object to execute it, right? – Matthias Müller Nov 12 '14 at 12:32
  • @MatthiasMüller It only does that during the JIT process, i.e. once only in the lifetime of the program. For full details, have a look at [this sample chapter from Jeffrey Richter's "CLR via C#" book](https://onedrive.live.com/view.aspx?cid=5A051E94BDA12163&resid=5A051E94BDA12163!1016&app=WordPdf) (Search for "when calling a static method") – Matthew Watson Nov 12 '14 at 15:04
1

Good post about performance comparsion of static methods vs instance methods: Performance of static methods vs instance methods

TLDR:

  • Mostly the performance costs of instance vs static are below negligible.
  • What costs there are will generally come where you abuse static for instance or vice-versa. If you don't make it part of your decision between static and instance, you are more likely to get the correct result.

  • There are rare cases where static generic methods in another type result in fewer types being created, than instance generic methods, that can make it sometimes have a small benefit to turn rarely used (and "rarely" refers to which types it's used with in the lifetime of the application, not how often it's called). Once you get what he's talking about in that article you'll see that it's 100% irrelevant to most static-vs-instance decisions anyway.

Community
  • 1
  • 1
Pavel Krymets
  • 6,253
  • 1
  • 22
  • 35
1

I think the point is that you're not calling a static method upon an instance, but on the class itself. Any method that does not directly rely on instance information could (and based upon code analysis also should) be marked static and then be called like this:

NonstaticClass.TheStaticMethod();

instead if this

NonstaticClass inst = new NonstaticClass();
inst.TheStaticMethod();

This is because less overhead is required to look up and run a static method than a non-static method on a class instance.

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139