You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
First call of Quantity.From is extremely slow compared to subsequent calls #965
Although the first one seems a bit high in reality I think it's related to something normal where the Jit for the first calls will emit un optimized machine code and will optimize in subsequent calls only. It's just the way the runtime is designed.
For a better benchmarking I suggest you use BenchmarkDotNet. It has some facilities to evaluate different scenarios and situations.
It would regardless be interesting to go look at the code for Quantity.From to see if reflection is involved for the first call.
Went to see the code and there's a kickass big switch case involved in the Quantity.From method using pattern matching. Pretty sure my first explanation is the right one : first call is executed with the "lazy" code the Jit emitted which is not optimized.
You could resolve this by prr compiling your assemblies with the AOT (ahead of time compiler). It was called ngen before but now I don't know.
Hey guys,
I've started work (way back when..) on a V2 of the Benchmarks PR. I haven't yet committed the customized benchmarks page (as there are a couple more things to fix) but you can browse through the updated results on my branch.
Here is the relevant extract for the unit construction:
The first call to Quantity triggers the static constructor, which goes about constructing all the QuantityInfos (partly lazy). I don't exactly remember the order of things, but Quantity.From does have a one-time initialization cost (however I could not figure out how to measure it). There are also these one-time initialization costs that you might also like to know about:
I expect some JIT performance hit on the first usage of Units.NET, due to the crazy number of types we have plus some relatively heavy initial setup of abbreviation cache and unit converters.
I'm sure this can all be optimized, so if someone feels the itch to hack at it, please go ahead :-D
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Activity
ebfortin commentedon Aug 28, 2021
Although the first one seems a bit high in reality I think it's related to something normal where the Jit for the first calls will emit un optimized machine code and will optimize in subsequent calls only. It's just the way the runtime is designed.
For a better benchmarking I suggest you use BenchmarkDotNet. It has some facilities to evaluate different scenarios and situations.
It would regardless be interesting to go look at the code for Quantity.From to see if reflection is involved for the first call.
ebfortin commentedon Aug 28, 2021
Went to see the code and there's a kickass big switch case involved in the Quantity.From method using pattern matching. Pretty sure my first explanation is the right one : first call is executed with the "lazy" code the Jit emitted which is not optimized.
You could resolve this by prr compiling your assemblies with the AOT (ahead of time compiler). It was called ngen before but now I don't know.
lipchev commentedon Aug 28, 2021
Hey guys,
I've started work (way back when..) on a V2 of the Benchmarks PR. I haven't yet committed the customized benchmarks page (as there are a couple more things to fix) but you can browse through the updated results on my branch.
Here is the relevant extract for the unit construction:
lipchev commentedon Aug 28, 2021
The first call to Quantity triggers the static constructor, which goes about constructing all the QuantityInfos (partly lazy). I don't exactly remember the order of things, but Quantity.From does have a one-time initialization cost (however I could not figure out how to measure it). There are also these one-time initialization costs that you might also like to know about:
ebfortin commentedon Aug 28, 2021
Ah the static constructor. I forgot about that one.
angularsen commentedon Aug 30, 2021
I expect some JIT performance hit on the first usage of Units.NET, due to the crazy number of types we have plus some relatively heavy initial setup of abbreviation cache and unit converters.
I'm sure this can all be optimized, so if someone feels the itch to hack at it, please go ahead :-D
stale commentedon Oct 30, 2021
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Sibusten commentedon Dec 1, 2021
For anyone finding this later, I was able to move the cost to app bootup by adding the following to
Main:jl-mobitech commentedon Sep 21, 2022
Thanks for this. To further improve UX, you can do the following: