ID:118361
 
I recently stumbled over this thing:
http://oreilly.com/catalog/javapt/chapter/ch04.html

Some of the practices would make sense for the DM Language too, since it runs in a VM too.

The Method of "Recycling" often used Objects might turn out neat applied to DM too, so i wrote a little test to get a feeling if there is realy a performance gain.

//Just a Cake
obj/Cake

//This is where old Cake goes
CakePool
var/list/obj/Cake/pool = new()

proc
allocate(amount as num)
for(var/i = amount, i > 0, i--)
src.pool.Add(new /obj/Cake)

grab()
if(src.pool.len > 0)
var/obj/Cake/C = src.pool[1]
src.pool.Remove(C)
return C
else
allocate(1)
return grab()

free(var/obj/Cake/C)
src.pool.Add(C)


//Global
var/CakePool/pool = new()

//A list of Cakes you own
mob
var/list/obj/Cake/cakes = new()

mob/verb

createCake(amount as num)
for(var/i = amount, i > 0, i--)
src.cakes.Add(new /obj/Cake)

destroyCake(amount as num)
for(var/i = amount, i > 0, i--)
var/obj/Cake/C = src.cakes[1]
src.cakes.Remove(C)
del C

initPool(amount as num)
pool.allocate(amount)
src << " inited [amount] Cake"


takeFromPool(amount as num)
//src << "grabbing [amount] Cakes!"
for(var/i = 0, i < amount, i++)
src.cakes.Add(pool.grab())
//src << "done"

giveBack(amount as num)
//src << "tossing [amount] Cakes!"
for(var/i = 0, i < amount, i++)
if(src.cakes.len == 0)
break
var/obj/Cake/C = src.cakes[1]
src.cakes.Remove(C)
pool.free(C)
//src << "done"

showPool()
src << list2params(pool.pool)

showMyCakes()
src << list2params(src.cakes)

testPerformance()
set background = 1

var/start = world.time
for(var/i = 10000, i > 0, i--)
createCake(50)
destroyCake(50)

src << (world.time - start)

testPerformancePool()
set background = 1

var/start = world.time
for(var/i = 10000, i > 0, i--)
takeFromPool(50)
giveBack(50)

src << (world.time - start)


It seems that there is realy a Performance Increase depending on how many objects can be reused. This snippet is very basic, it doesn't cover aspects of reinitialization of objects.
Maybe I'll do some more testing later.
As long as you're not using del() you shouldn't really be affected, because otherwise the garbage collector will just get rid of things whenever it can without using any processing power.
Actually that is one thing i am not realy certain about. Since it is impossible to say when the garbage colletor starts working i don't know when it will use ressource to free memory. Also on of the main points is to avoid the VM to allocate new Memory (creating new Objects) which takes time. And no, the Garbage Collector will use processing power, that's why people tell all thouse garbage collector jokes for java ;-)
I'm not exactly sure how much it uses, but it is definitely better than using Del() to get rid of your objects.
I guess so.
Ok, let's provide some values:

for a test without del
27, 38, 38, 39, 38

with del
26, 38, 39, 38, 39

through the pool (without initialization)
6,7, 6, 7, 7

So no significant difference between using del or not, but i might have made a mistake?

These values may just give a feeling, there might be some things i did not considder, so take them just as a base to experiment around a bit.
The irony I suppose is it doesn't matter, for DM. The same reason the GC jokes for Java are just that, jokes, not valid concerns for most applications.
Stephen001 wrote:
The irony I suppose is it doesn't matter, for DM. The same reason the GC jokes for Java are just that, jokes, not valid concerns for most applications.

You might think that, but for games I'm not going to mention in development, there are a lot of projectiles being fired around the screen which need to be gotten rid of quickly.
Stress test results:
At first I was using Del() and I was pushing 90% CPU at times.
Then I moved to the garbage collector and it dropped to around 35% CPU.
Yes, Del() is a known performance thing, and is pretty much bad practice in the general case. The joke Amsel refers to is GC itself being a performance issue.

It's a common C/C++ programmer complaint at .... well, every other language. Then they add GC to C++, and people import GC libraries to their C programs. So ....
hm