-
Notifications
You must be signed in to change notification settings - Fork 386
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
First call of Quantity.From is extremely slow compared to subsequent calls #965
Comments
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, Here is the relevant extract for the unit construction: BenchmarkDotNet=v0.13.0, OS=Windows 10.0.17763.1935 (1809/October2018Update/Redstone5)
Intel Xeon Platinum 8171M CPU 2.60GHz, 1 CPU, 2 logical and 2 physical cores
.NET SDK=5.0.203
[Host] : .NET 5.0.6 (5.0.621.22011), X64 RyuJIT
Job-BMHMKH : .NET 5.0.6 (5.0.621.22011), X64 RyuJIT
Runtime=.NET 5.0 Toolchain=netcoreapp50
|
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: BenchmarkDotNet=v0.13.0, OS=Windows 10.0.17763.1935 (1809/October2018Update/Redstone5)
Intel Xeon Platinum 8171M CPU 2.60GHz, 1 CPU, 2 logical and 2 physical cores
.NET SDK=5.0.203
[Host] : .NET 5.0.6 (5.0.621.22011), X64 RyuJIT
Job-OYANID : .NET 5.0.6 (5.0.621.22011), X64 RyuJIT
Runtime=.NET 5.0 Toolchain=netcoreapp50
|
Ah the static constructor. I forgot about that one. |
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. |
For anyone finding this later, I was able to move the cost to app bootup by adding the following to RuntimeHelpers.RunClassConstructor(typeof(Quantity).TypeHandle); |
Thanks for this. To further improve UX, you can do the following: await Task.Run(() => RuntimeHelpers.RunClassConstructor(typeof(Quantity).TypeHandle)).ConfigureAwait(false); |
As stated in the title the first call to Quantity.From is noticeably slow, which impacts the user experience when used for a textbox.
To reproduce I basically run the following code:
Result:
It's only the first call to Quantity.From(). Doesn't matter which unit and changing units after does not produce longer times.
The text was updated successfully, but these errors were encountered: