Banks and Performance

BlitzMax Forums/BlitzMax Programming/Banks and Performance

BLaBZ(Posted 2013) [#1]
It is possible to increase performance using banks?

I know this is a very general question, but I don't have much knowledge of banks and their common usage.

If I needed to calculate "floats" but only 2 or 3 bytes of precision would this be faster than using native blitz floats?


Yasha(Posted 2013) [#2]
If I needed to calculate "floats" but only 2 or 3 bytes of precision would this be faster than using native blitz floats?


No. Blitz's native floats use operations implemented in hardware. Literally anything you try to do to reimplement floats in software will be slower than what is already available, as it uses the fastest method available to the machine. Reducing the size of floats to a different number of bytes will slow things down even further by introducing misaligned accesses or similar problems.

(Technically not quit true: you might get faster floating-point performance for some operations by using SSE or the GPU, but that's definitely getting into a separate question and can't be done in BlitzMax code.)

Banks are just a wrapper around more fundamental BlitzMax data structures. They might not actually slow code down, but they definitely don't offer anything to speed it up (and they do offer ways to inadvertently slow code down, as above). They mainly exist for people coming from B3D where they were the only option for many operations.


Polan(Posted 2013) [#3]
It's possible to speed up banks. By rewriting them from scratch and changing few stuff here and there. This is what I use instead of buffers (in tests it was 50-60% faster than bank in writing tons of separated bytes). It doesn't have lockbank/buffer because "data" field is already byte ptr. It doesn't do any range check, so you need to take care of staying in line.
"allocated" is capacity
"size" is used space (it was made for networking, size is ammount of bytes you have to send).
Method Seek is for moving head (after you are done with writing, you should seek(0) to read from beggining).
Function dsCreateDataBuffer:dsDataBuffer(startsize%)
	Local buffer:dsDataBuffer = new dsDataBuffer
		buffer.allocated = startsize
		buffer.data = MemAlloc(startsize)
	Return buffer
EndFunction

Type dsDataBuffer
	Method New()
		Seek(0)
	EndMethod
	Method Delete()
		MemFree(data)
	EndMethod
	
	Field allocated%,size%
	Field data:byte ptr,pos%
	Method Seek(pos%)
		Self.pos = pos
	EndMethod
	Method Clear()
		Seek(0)
		size = 0
	EndMethod
	Method SpaceLeft%()
		Return allocated-size
	EndMethod
	Method GetSize%()
		Return size
	EndMethod
	Method GetCapacity%()
		Return allocated
	EndMethod
	Method Resize(size%)
		Local tmp:Byte ptr = memalloc(size)
		allocated = size
		MemCopy(tmp,data,size)
		MemFree(data)
		data = tmp
	EndMethod
		
	Method WriteByte(value:Byte)
		data[pos] = value
		pos :+ 1
		size = Max(pos,size)
	EndMethod
	Method WriteShort(value:Short)
		(Short ptr(data+pos))[0] = value
		pos :+ 2
		size = Max(pos,size)
	EndMethod
	Method WriteInt(value:Int)
		(Int ptr(data+pos))[0] = value
		pos :+ 4
		size = Max(pos,size)
	EndMethod
	Method WriteLong(value:Long)
		(Long ptr(data+pos))[0] = value
		pos :+ 8
		size = Max(pos,size)
	EndMethod
	Method WriteFloat(value:Float)
		(Float ptr(data+pos))[0] = value
		pos :+ 4
		size = Max(pos,size)
	EndMethod
	Method WriteDouble(value:Double)
		(Double ptr(data+pos))[0] = value
		pos :+ 8
		size = Max(pos,size)
	EndMethod
	Method WriteString(value:String)
		WriteShort(len(value))
		For Local x% = 0 to len(value)-1
			WriteShort(value[x])
		next
		size = Max(pos,size)
	EndMethod
	
	Method ReadByte:Byte()
		pos :+ 1
		Return data[pos-1]
	EndMethod
	Method ReadShort:Short()
		pos :+ 2
		Return (Short ptr(data+pos-2))[0]
	EndMethod
	Method ReadInt:Int()
		pos :+ 4
		Return (Int ptr(data+pos-4))[0]	
	EndMethod
	Method ReadLong:Long()
		pos :+ 8
		Return (Long ptr(data+pos-8))[0]
	EndMethod
	Method ReadFloat:Float()
		pos :+ 4
		Return (Float ptr(data+pos-4))[0]
	EndMethod
	Method ReadDouble:Double()
		pos :+ 8
		Return (Double ptr(data+pos-8))[0]
	EndMethod
	Method ReadString:String()
		Local str$,leng:Short
		leng = ReadShort()
		For Local x% = 1 to leng
			str :+ chr(ReadShort())
		next
		Return str
	EndMethod	
EndType



ImaginaryHuman(Posted 2013) [#4]
Don't use peek or poke... get the BankBuf() pointer and use pointers to read/write.