Re: bothering with C# for 3D
Jon Watte <[email protected]>
| Newsgroups | gmane.games.devel.sweng |
|---|---|
| Message-ID | <[email protected]> |
Caveat: Mono may be stupider about this than the MS CLRs. However, both the
desktop and compact CLRs will iterate List<> using value types.
Note that generic iterators (such as IEnumerator, rather than
IEnumerator<T>) will always box value types, which will also generate
garbage.
struct Vector3 { public float x; public float y; public float z; }
List<Vector3> myList;
// won't garbage
foreach (Vector3 v in myList) {
...
}
// will garbage for sure -- not even generic
IEnumerator en = myList.GetEnumerator();
foreach (Vector3 v in en) {
...
}
In the second case, "en" must be a boxed enumerator object, and each object
that's returned from the enumerator is returned through object, and thus
also needs to be boxed (and then immediately unboxed).
Correct C# usage requires careful attention to detail in value vs reference
types, as well as the interfaces you use. The penalty for forgetting is
"only" performance, though, not as bad as in C++ where you segfault and die.
Sincerely,
jw
--
Americans might object: there is no way we would sacrifice our living
standards for the benefit of people in the rest of the world. Nevertheless,
whether we get there willingly or not, we shall soon have lower consumption
rates, because our present rates are unsustainable.
On Wed, Jan 27, 2010 at 10:31 AM, Jon Frisby <[email protected]> wrote:
>
> On Jan 27, 2010, at 11:56 AM, Jon Watte wrote:
>
> The problem is that you're boxing the struct when you assign it to the
>> IEnumerable member. If you did not assign it, then the value type enumerator
>> does not get boxed when returned *directly* to foreach.
>> There is no way to store a value type out of scope without using the heap
>> (either an array, or a boxed value). That's the whole point of value types!
>>
>
> I'm a bit sleep-deprived so I'm a bit fuzzy on 'where' and why the boxing
> is happening -- or at least, why it's happening when iterating the
> enumerator, rather than when the element is added to the collection -- but
> I'll suss that out after some rest.
>
> Bottom line: Using a List<object> or ArrayList suddenly becomes desirable
> since the boxing apparently only happens when the struct is added to the
> collection, not when the enumerator is iterated.
>
>
> -JF
> _______________________________________________
> Sweng-Gamedev mailing list
> [email protected]
> http://lists.midnightryder.com/listinfo.cgi/sweng-gamedev-midnightryder.com
>
_______________________________________________
Sweng-Gamedev mailing list
[email protected]
http://lists.midnightryder.com/listinfo.cgi/sweng-gamedev-midnightryder.com