Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Quality: Seal classes when possible #12304

Closed
itsWindows11 opened this issue May 7, 2023 · 1 comment · Fixed by #14990
Closed

Code Quality: Seal classes when possible #12304

itsWindows11 opened this issue May 7, 2023 · 1 comment · Fixed by #14990
Labels
codebase quality Issues that are not bugs, but still might be worth improving (eg, code hygiene or maintainability)

Comments

@itsWindows11
Copy link
Contributor

itsWindows11 commented May 7, 2023

Description

Related to #4180.

Unsealed classes introduce extra overhead for running each virtual method/type checks because the compiler/JIT has to do extra inheritance checks.

For example, there are three non-sealed classes called ClassA, ClassB and ClassC, and ClassB inherits from ClassA. If ClassC doesn't inherit from ClassB (in other forms, if nothing inherits from ClassB) then we don't need the extra dispatch operations or checks. Thus making ClassB sealed removes that overhead when calling virtual methods or doing type checks.

See https://devblogs.microsoft.com/dotnet/performance-improvements-in-net-6/#peanut-butter for more in-depth details.

Concerned code

Most C# source files in the entire app solution.

Gains

  • Slightly better performance.

Requirements

  • Make classes that don't need to be inherited sealed.

Comments

No response

@itsWindows11 itsWindows11 added the codebase quality Issues that are not bugs, but still might be worth improving (eg, code hygiene or maintainability) label May 7, 2023
@0x5bfa
Copy link
Member

0x5bfa commented May 8, 2023

I will work on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
codebase quality Issues that are not bugs, but still might be worth improving (eg, code hygiene or maintainability)
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants