3 ms·
I was under the impression that the way C# did generics meant that there was no need to box values. (Unlike Java, which treats all values as objects, even value
by AndrewDucker 7y ago
I was under the impression that the way C# did generics meant that there was no need to box values.
(Unlike Java, which treats all values as objects, even value types.)
- jayd16 7y agoC# has value types and many things are value types but certain things are not. Core library interfaces are implemented as with Object enumerator types for example. This causes IList.GetEnumerator() to return a boxed type. Its smart enough to use the value type when it knows it can though, such as List.GetEnumerator(). Lambdas have some allocations. I like LINQ but it can have some allocation as well. These are all tiny allocations but in a game you'd like to strive for 0 allocations.
- hinkley 7y agoTwo ways to interpret boxing here: 1 Storing a primitive as a first class object 2 packaging and repackaging data There may be situations where the former is not an issue, but the latter often is. If you don’t have a way to treat filter() as a generator/coroutine it means your traversing one collection and inserting those values into another, only to pass it to map() which iterates again and creates a third. If you’re doing that once a frame, maybe not a big deal. Three times per object per frame? That’s a lot of temporary data structures. If the other responder is right, I speculate that the borrow checker forced someone’s hand in Rust and they found that a generator keeps the borrowed objects on the call stack where they already had tools to track ownership.
- pjc50 7y agoCorrect usage of IEnumerable helps a lot, as well as the magic that Linq can do with expressions.
- dleslie 7y agoIn my experience, the trick is to remove `using Linq` from the entire project.
- hinkley 7y agoAs someone who has had a mild case of Linq jealousy for some time, I’m curious what makes you averse.
- dleslie 7y agoIt's too easy to fall into the temporary collection trap you described; on a large project with many developers, allowing Linq is a bit like leaving a toaster plugged in beside the pool.