Sunny> Look what have I found! Consider a C#:
public class T { public T free; } public void NewTest() { T cache = new T(); Stopwatch timer = new Stopwatch(); timer.Reset(); timer.Start(); for(int i = 0; i < 10000000; ++i) { // Get from cache. T t; if (cache.free == null) { cache.free = new T(); } t = cache.free; // Release cache.free = t; t = null; } timer.Stop(); long cacheTicks = timer.ElapsedTicks; timer.Reset(); timer.Start(); for(int i = 0; i < 10000000; ++i) { new T(); } timer.Stop(); long newTicks = timer.ElapsedTicks; Console.WriteLine("cache: {0}, new: {1}", cacheTicks, newTicks); }
Gloomy> And?
Sunny> Tests show that new T() is almost as fast as caching! GC's "new" probably has a fast route, where it shifts free memory border in an atomic way, thus allocation takes just several cycles.
new T()
Gloomy> Well, you're probably right, there is a fast route. I, however, have a different opinion. To track references, a generational garbage collector implements field assign as a call rather than a mov. This routine, except move itself, marks touched memory page in a special card table (who said GC is cheap?); thus, I think, a reference field setter is almost as slow as the "new" call.
call
mov
Remember Me
a@href@title, b, blockquote@cite, em, i, strike, strong, sub, super, u