4

Someone suggested me to mark classes as SEALED that are not being inherited anywhere. I was using default style and was not concerned with the type of class. At present I don't have a benchmark tool to study the performance gains but read somewhere that Sealed Classes give performance benefits.

Is this performance issue applicable to other class types like: Abstract, Static, New etc. and also with access modifiers like public, protected, internal, protected-internal and virtual?

If it does make difference in performance, what type of optimization is done internally?

3 Answers 3

4

Yes, using sealed classes, in addition to significant design benefits, can offer some performance benefits.

The design benefits (which you say you aren't interested in, but I'll include here for a complete answer) are that one can avoid the Brittle base class problem. Which, briefly, means a future change to a base class can cause problems if things aren't marked sealed.

As stated in the link you posted: "Sealing the virtual methods makes them candidates for inlining and other compiler optimizations" and "virtual members...are more expensive to call due to a virtual table lookup".

The .NET framework makes every attempt to optimize virtual dispatch, so by no means are virtual function calls 'expensive', but they are somewhat slower than a direct function call, or better yet an inlined function. This extra overhead is only significant if the function you are calling is extremely short (e.g. doesn't iterate over data), and if the function call is within a loop.

If you want detailed information on approximately how much slower a virtual function call is, consider this link.

As for the other keywords:

static functions are not overridable, so they are the same performance as sealed. All the rest will not make a difference in the performance of your class. However, as the link states, field and method design choices can (e.g. volatile fields are slower).

5

In my opinion this is a question of design, not performance. If you want to prevent the class from being inherited, mark it sealed - you should only do this if you have a good reason to do so, otherwise you are potentially placing artificial restrictions on the future design of the system.

Any question of performance issues around class modifiers seem fantastically marginal to me (but see https://stackoverflow.com/questions/2134/do-sealed-classes-really-offer-performance-benefits) and unlikely to make a difference in real life.

In fact, I'd go further and say this really sounds like premature optimisation. Do you have any performance/test figures to suggest you NEED to improve performance? If not (and I assume not) then I really wouldn't worry about this - if you do and this makes sufficient difference, please let us know :-)

2
  • My post is from performance point-of-view and design issues related to scalability is not my concern. Please check the "Class Design Considerations" topic of this article: msdn.microsoft.com/en-us/library/…
    – RPK
    Aug 6, 2011 at 12:30
  • Apologies - I misread the question slightly (or completely ;-)) and thought you were just talking about sealed, rather than other access modifiers. For the case of "sealed" I think the top rated answer to the article I linked covers it very well. From a "practical" perspective I would still suggest that allowing design decisions to be influenced by performance considerations of this kind without supporting performance measurements is premature optimisation :-) Aug 6, 2011 at 14:26
0

I don't know if there is any performace difference at all making a class sealed, and I doubt that there is, and if there is any difference at all, it's certainly not significant.

The same goes for any other modifier. The static modifier for example restricts you to use static methods, and a static method doesn't need an instance reference passed to it so that would theoritically give a very slight performance gain. In practice it's not certain that there is any perforance difference at all, and if there is one, it's so small that it's very hard to even measure.

You should use the modifiers that gives you the behaviour that you want, not try to use it to affect performance. There are certainly other ways of giving the code a much larger performance gain if that's what you are after.

1
  • A call to a virtual function can be inlined if the compiler knows definitely which implementation will be used. Final methods and sealed classes help with that.
    – gnasher729
    Mar 30, 2021 at 8:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.