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.