In a recent blog post we explained how to tweak a simple UDP application to maximize throughput. This time we are going to optimize our UDP application for latency. Fighting with latency is a great excuse to discuss modern features of multiqueue NICs. Some of the techniques covered here are also.
I am trying to write a class to handle Memory cache in a .net core class library. If I use not the core then I could write
Within .Net core I could not find System.Runtime.Cache. After reading the .net core In Memory Cache, I have added reference Microsoft.Extensions.Caching.Memory (1.1.0) and tried
Hope code inside the save method is ok, although most of my mycache tests are failing at the moment. Can anyone please point out what is wrong?The main question is about the constructor What should i do to set the cache instead MemoryCache.Default
MJK
MJKMJK
1 Answer
mattinsaltomattinsalto
Not the answer you're looking for? Browse other questions tagged caching.net-corememorycache or ask your own question.
I bet I could answer that myself if I knew more about tools to analyze how C#/JIT behaves but since I don't, please bear with me asking.
I have simple code like this :
As you can see I put AggressiveInlining because I feel like it should be inlined.
I think. There's no guarantee the JIT would inline it otherwise. Am I wrong?
I think. There's no guarantee the JIT would inline it otherwise. Am I wrong?
Could doing this kind of thing hurt the performance/stability/anything?
SergeSerge
4 Answers
Compilers are smart beasts. Usually, they'll automatically squeeze out as much performance as they can from anywhere they can.
Trying to outsmart the compiler doesn't usually make a big difference, and has a lot of chances to backfire. For instance, inlining makes your program bigger as it duplicates the code everywhere. If your function is used in a lot of places throughout the code, it might actually be detrimential as pointed out @CodesInChaos. If it is obvious the function should be inlined, you can bet the compiler will do so.
In case of hesitation, you can still do both and compare if there is any performance gain, that's the only certain way to now. But my bet is the difference will be neglegible, the source code will just be 'noisier'.
dagneliesdagnelies
EDIT: I realize my answer didn't exactly answer the question, while there is no real downside, from my timing results there is no real upside either. The difference between a inline property getter is 0.002 seconds over 500 million iterations. My test case may also not be 100% accurate since its using a struct because there are some caveats to the jitter and inlining with structs.
As always, the only way to really know is to write a test and figure it out. Here are my results with the following configuration:
Empty project with the following settings:
Results
Tested with this code:
Chris PhillipsChris Phillips
You are right - there is no way to guarantee that the method would be inlined - MSDN MethodImplOptions Enumeration, SO MethodImplOptions.AggressiveInlining vs TargetedPatchingOptOut.
Programmers are more intelligent than a compiler, but we work on a higher level and our optimizations are products of one man's work - our own. Jitter sees what's going during the execution. It can analyze both the execution flow and the code according to the knowledge put into it by its designers. You can know your program better, but they know better the CLR. And who will be more correct in his optimizations? We don't know for sure.
That's why you should test any optimization you make. Even if it is very simple. And take into account that the environment may change and your optimization or disoptimization can have quite an unexpected result.
Community♦
Eugene PodskalEugene Podskal
Compilers do a lot of optimizations. Inlining is one of them, whether the programmer wanted or not. For example, MethodImplOptions does not have an 'inline' option. Because inlining is automatically done by the compiler if needed.
Many other optimizations are especially done if enabled from the build options, or 'release' mode will do this. But these optimizations are kind of 'worked for you, great! Not worked, leave it' optimizations and usually give better performance.
is just a flag for the compiler that an inlining operation is really wanted here. More info here and here
To answer your question;
There's no guarantee the JIT would inline it otherwise. Am I wrong?
True. No guarantee; Neither C# has a 'force inlining' option.
Could doing this kind of thing hurt the performance/stability/anything?
In this case no, as it is said in Writing High-Performance Managed Applications : A Primer
Property get and set methods are generally good candidates for inlining, since all they do is typically initialize private data members.
Community♦
myucemyuce
protected by gnatOct 6 '16 at 21:23
Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
Would you like to answer one of these unanswered questions instead?