gcroot template performance

Regarding my earlier post about a native type safe wrapper for a managed enumerator, I received the following comment from a developer in the Visual C++ group:

One nit about these wrapper classes that you might want to be cognizant of is the small perf hit that comes from using a gcroot<>.

If you’re using Enumerators, chances are you’re iterating over the collection, which means you have a loop that calls MoveNext() once and Current() at least once on each iteration. Since both of these operations unwraps the gcroot (by calling gcroot::operator->()), you’re paying at least an 100-200 extra cycles per iteration which can’t be inlined by the compiler or JITter. Depending on your usage, that might be a lot to pay just for the syntactic nicety of not having to cast. Just something to be aware of: the gcroot isn’t completely free.