Manual Type Caching
This is just a little experiment to query costs of batching dynamic allocations at runtime. Which it appears, that by the pre-caching of types, you can indeed roll a faster way to allocate new (well reuse) types buffers in a linked list. It'd work better in a types array though. Warning this is not intended for beginners.
requires PlayBASIC V1.64K or higher.
[pbcode]
; Define a type we're use for our object structures
type tPos
name$
x#,y#
DeathTime
EndType
; make two lists. ME will use cached allocation, ME2 will use native allocations
Dim me as tpos list
Dim me2 as tpos list
// Size of cache (just a normal integer array)
Size=1000
Dim CacheAlloc(size)
// temp array to hold a type in. We create the type, then
// grab it's bnka handle, then replace it manually.
Dim TempCache(0) as tpos
For lp=1 to size
TempCache(0) = new tpos
CacheAlloc(lp)=TempCache(0)
ptr =getarrayptr(TempCache())+ PBArraystruct_size
Pokeint Ptr,0
next
undim TempCache()
// Size of the allocation cache
CacheAlloc(0)=size
SetFPS 60
Do
Cls 0
Frames++
DeathTime=Timer()+1000
t=timer()
for lp =1 to size
Buffer=GetCachedObject()
if Buffer
me = Buffer
me.name$="name"
me.deathTime=DEathTime
endif
next
tt1#+=(timer()-t)
SizeAfterAddition =GetListSize(me())
print tt1#/frames
print lp
for each me()
ReleaseCacheObject()
next
print "listSize:"+str$(SizeAfterAddition)
print "listSize:"+str$(GetListSize(me()))
t=timer()
for lp =1 to size
me2= new tpos
me2.name$="name"
me2.deathTime=DEathTime
next
tt2#+=(timer()-t)
print tt2#/frames
SizeAfterAddition =GetListSize(me2())
for each me2()
me2 = null
next
print lp
print "listSize:"+str$(SizeAfterAddition)
print "listSize:"+str$(GetListSize(me2()))
Sync
loop
Psub GetCachedObject()
ThisBuffer=0
Index=CacheAlloc(0)
if Index>0
ThisBuffer=CacheAlloc(Index)
CacheAlloc(0)=Index-1
endif
EndPsub ThisBuffer
Psub ReleaseCacheObject()
Dim Ptr as integer pointer
ptr =getarrayptr(me())+ PBArraystruct_size
Index=GetLIstPos(Me())
Ptr =Ptr+Index
; Release from list
Index=CacheAlloc(0)+1
CacheAlloc(Index)=*Ptr
CacheAlloc(0)=Index
*Ptr =0
me = null
EndPsub
[/pbcode]