#1 Members - Reputation: 551
Posted 25 October 2012 - 11:09 PM
I am considering what performance implications the use of LINQ/C# could have on my engine's overall performance. I don't think it's any secret that queries are a bit slower (sometimes significantly slower) that plain old C# code. But I'm wondering how big of an impact that could have in a very specific set of contexts. For instance, I use a dash of LINQ in my "GameComponent" classes for sorting out child components, like so:
[source lang="csharp"]public virtual void Update(GameTime gameTime) { if (!Enabled) return; var components = ( from component in ChildComponents orderby component.UpdateOrder ascending where component.Enabled select component).ToArray(); for (int i = 0; i < components.Length; ++i) components[i].Update(gameTime); if (Updated != null) Updated(this, EventArgs.Empty);}[/source]
The code is definitely very concise and pretty, but how about performance? I'm thinking it's not going to be a bottleneck because the number of "GameComponents" will rarely number more than a few tens at the maximum. And it's my understanding that LINQ queries have grown faster in C# 5.0. When the performance hit is very marginal, I tend to favor very "elegant" code like this; it's easy to read, modify and maintain. Another one of my programmers can take one look at this and instantly know what the code is doing. In that way I think a "dash" of LINQ here and there can be helpful in the overall structure of software.
So the question isn't will there be a performance hit... there definitely will be some penalty to pay for using a bit of LINQ here and there. But is it enough to worry about or lose any sleep over? Should I get rid of all LINQ-stuff and rewrite it in "pure" C#? My question isn't about this one, particular method, GameComponent.Update(GameTime), but about situations like these and the use of LINQ in general...
Regards,
--ATC--
CEO & Lead Developer at ATCWARE™
"Project X-1"; a 100% managed, platform-agnostic game & simulation engine
Please visit our new forums and help us test them and break the ice!
___________________________________________________________________________________
#2 Crossbones+ - Reputation: 3295
Posted 25 October 2012 - 11:12 PM
Beginner in Game Development? Read here.
Super Mario Bros clone tutorial written in XNA 4.0 [MonoGame, ANX, and MonoXNA] by Scott Haley
If you have found any of the posts helpful, please show your appreciation by clicking the up arrow on those posts ![]()
#3 Moderators - Reputation: 13471
Posted 25 October 2012 - 11:20 PM
e.g. where filters the number of objects, so it should be done before projection (i.e. select) or sorting.
#4 Crossbones+ - Reputation: 3295
Posted 25 October 2012 - 11:27 PM
Also, do you use query syntax or lambda syntax? Out of curiosity.
Edited by Alpha_ProgDes, 25 October 2012 - 11:28 PM.
Beginner in Game Development? Read here.
Super Mario Bros clone tutorial written in XNA 4.0 [MonoGame, ANX, and MonoXNA] by Scott Haley
If you have found any of the posts helpful, please show your appreciation by clicking the up arrow on those posts ![]()
#5 Staff - Reputation: 8892
Posted 25 October 2012 - 11:28 PM
I'd suggest you continue to use it for now and consider replacing it later on if you find it's a cause of performance problems.
Hope that's helpful!
- Jason Astle-Adams.
From my blog: 20 ways to advertise your game | What next? Intermediate to advanced C++
How to make games WITHOUT programming | 4 reasons you aren't a successful indie developer
#6 Members - Reputation: 414
Posted 25 October 2012 - 11:53 PM
For example, if you only iterate thru the query once, you're better of not making an array, but just do a foreach on it, such as:
var components = ( from component in ChildComponents orderby component.UpdateOrder ascending where component.Enabled select component); foreach (var component in components) component.Update(gameTime);
If you on the other hand are going to iterate thru it many times, then you better cache it into an array, otherwise it will query and sort it each time you iterate.
That said, I don't think you can write a non-LINQ version of that particular code (i.e. tens of items once every game loop) that will provide you with significantly better performance. It should perform about the same as any non-LINQ version.
Edited by Felix Ungman, 25 October 2012 - 11:59 PM.
#7 Members - Reputation: 551
Posted 26 October 2012 - 09:32 AM
I changed the queries so that the "where" filter comes before sorting. It seems logical that it will sift out components that aren't enabled and speed up the sorting that follows. If it doesn't, oh well...only had to copy and paste one line of code so no loss. Of course jbadams is right though, and only actually profiling it will tell the true tale of performance...
CEO & Lead Developer at ATCWARE™
"Project X-1"; a 100% managed, platform-agnostic game & simulation engine
Please visit our new forums and help us test them and break the ice!
___________________________________________________________________________________
#8 Moderators - Reputation: 13471
Posted 27 October 2012 - 01:56 AM
I've just been learning from examples on MSDN and using google for specific questions -- most of these searches bring up a StackExchange page where someone's already asked my question.To both of you: what LINQ resources are you using to learn LINQ. I've dabbled at it, but admittedly, I'm certainly not an expert.
Also, do you use query syntax or lambda syntax? Out of curiosity.
I'm using the Lambda syntax at the moment, because I love C# lambdas ;)
#9 Members - Reputation: 894
Posted 27 October 2012 - 07:53 AM
That said, I don't think you can write a non-LINQ version of that particular code (i.e. tens of items once every game loop) that will provide you with significantly better performance. It should perform about the same as any non-LINQ version.
Other than the really obvious improvement of just keeping your component list in sorted order, all the time. What order are they stored in normally? If it's arbitrary, then save yourself the trouble of sorting them every frame... (And you could go further and maintain two collections in the entitiy - one for enabled components, one for disabled). Move components between them on state change. Now you need zero LINQ code because you need to do zero work.
#10 Members - Reputation: 551
Posted 27 October 2012 - 04:11 PM
Other than the really obvious improvement of just keeping your component list in sorted order, all the time. What order are they stored in normally? If it's arbitrary, then save yourself the trouble of sorting them every frame... (And you could go further and maintain two collections in the entitiy - one for enabled components, one for disabled). Move components between them on state change. Now you need zero LINQ code because you need to do zero work.
It was just example code, but you're right... and I don't actually sort on every frame. Only if the component list has been changed. But you also have to check (or mark "dirty") for changes to the UpdateOrder property which will require a resort. Still, using LINQ is a quite elegant solution to the problem imho
CEO & Lead Developer at ATCWARE™
"Project X-1"; a 100% managed, platform-agnostic game & simulation engine
Please visit our new forums and help us test them and break the ice!
___________________________________________________________________________________
#11 Members - Reputation: 240
Posted 29 October 2012 - 09:54 PM
It then got to the point where I needed to find places I could speed up a few hot sections of code, and it turned out that by abandoning LINQ in favour of hand-written alternatives I was able to get quite a big performance boost in several places which made a noticeable FPS difference. This was because I was using these LINQ expressions in places that were being called every frame, which is not ideal. If it's something you're only calling every now and then, I guess it's fine. But for code getting hit every frame, you really want things to be as fast as possible, as you'll inevitably rewrite it later to get the best performance possible.
#12 Members - Reputation: 1271
Posted 29 October 2012 - 11:45 PM
I'm only a beginner with LINQ, but from what I can tell, the order in which you use LINQ statements can have a big impact on what computation is done.
e.g. where filters the number of objects, so it should be done before projection (i.e. select) or sorting.
When using the SQL-like syntax (ie. "from n in <collection> where <condition>..." etc.), wouldn't the compiler take care of that kind of thing? Is the C# compiler smart enough to notice that you're doing a filter and a projection together, and do the projection after the filter?
#13 Members - Reputation: 414
Posted 30 October 2012 - 02:07 AM
When using the SQL-like syntax (ie. "from n in <collection> where <condition>..." etc.), wouldn't the compiler take care of that kind of thing? Is the C# compiler smart enough to notice that you're doing a filter and a projection together, and do the projection after the filter?
One really elegant aspect of C#/Linq is that the sql query syntax is really just syntactic sugar. You can roll your own classes with methods having the corresponding signatures. They need not have any relationship with the standard ones and you can still use the query syntax. So the choice of syntax doesn't matter performance wise.
On the other hand, if queries agains a database linq-provider get translated into expression trees. It is up to the linq-provider to do any optimization of this query tree. Different providers do it differently. However, the Linq-to-objects provider doesn't optimize (by design).
#14 Members - Reputation: 1271
Posted 30 October 2012 - 03:21 PM
When using the SQL-like syntax (ie. "from n in <collection> where <condition>..." etc.), wouldn't the compiler take care of that kind of thing? Is the C# compiler smart enough to notice that you're doing a filter and a projection together, and do the projection after the filter?
One really elegant aspect of C#/Linq is that the sql query syntax is really just syntactic sugar. You can roll your own classes with methods having the corresponding signatures. They need not have any relationship with the standard ones and you can still use the query syntax. So the choice of syntax doesn't matter performance wise.
So in other words, the compiler doesn't optimize the translation from the query syntax to the lambda syntax?
#15 Members - Reputation: 1864
Posted 30 October 2012 - 03:51 PM
So in other words, the compiler doesn't optimize the translation from the query syntax to the lambda syntax?
The compiler would need to prove that the lambda functions used in the query do not have side effects, and it would also need to understand whether the select/where functions are associative or not (even for user-provided ones). Considering C#'s compile speed is still ridiculously fast, I suspect that might be a little bit beyond what it bothers with...
It would be interesting to examine the output IL to get some proof, though!






