-
Notifications
You must be signed in to change notification settings - Fork 5.4k
JIT and one-time source generated methods performance cost #126541
Copy link
Copy link
Open
Labels
area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMICLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIquestionAnswer questions and provide assistance, not an issue with source code or documentation.Answer questions and provide assistance, not an issue with source code or documentation.tenet-performancePerformance related issuePerformance related issueuntriagedNew issue has not been triaged by the area ownerNew issue has not been triaged by the area owner
Metadata
Metadata
Assignees
Labels
area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMICLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMIquestionAnswer questions and provide assistance, not an issue with source code or documentation.Answer questions and provide assistance, not an issue with source code or documentation.tenet-performancePerformance related issuePerformance related issueuntriagedNew issue has not been triaged by the area ownerNew issue has not been triaged by the area owner
Type
Fields
Give feedbackNo fields configured for issues without a type.
Description
When leveraging source generation, it is sometimes required to register an object or some data at application startup. To do this, we generally have to emit a static method with a [ModuleInitializer] if we want this to happen automatically without the user having to know or do anything.
I do this in TUnit, and was emitting some metadata about the test and registering on application startup. I was triggerring the source generation by looking for a
[Test]attribute, and so there would generally be 1 new emitted method for 1 test.However for users with large test suites, this would mean for example 10k tests means 10k new emitted methods. This means it of course scales when test suites grow.
I (and many others I'm sure) was under the impression that source generated code will always be faster than reflection, and while I think that's true once an application is warmed up, I've found that this approach is actually slower due to the JIT compilations that need to happen on each of these new methods. And since these will only ever be called once, could we avoid the overhead?
I'm not even sure if there's a solution here - But I thought I'd raise this for awareness and maybe start a discussion.
I don't know much about compilers and how the runtime itself works so I don't know any solutions to propose or suggest.
My first thought was could we add some sort of flag to the
[MethodImpl]attribute to tell the compiler that this is a one-time called method and could the compiler do some pre-optimisations to it to avoid the JIT costs during app start up?Data
Here's a repo for the trivial benchmarking apps: https://github.com/thomhurst/JitOverheadBenchmark/tree/main