Boy am I not understanding the python pass-by-reference issues... I have created the extremely useful "unpacker" class which I pass around to various objects that need to unpack from it, yet given how extraordinarily slow it is, I can tell it's making a copy of binaryStr each time I pass a BU object. I know this because if I break the BU into smaller chunks, it runs, literally, 100x faster (I was originally using it to hold a 16MB file I/O buffer)
So my question is, why is that member not getting passed by reference, and is there a way to force it to? I am pretty sure the BU object itself is passed by reference (since my code works), but the speed suggests the .binaryStr object is copied. Is there something more subtle that I'm missing?
class BinaryUnpacker(object):
def __init__(self, binaryStr):
self.binaryStr = binaryStr
self.pos = 0
def get(self, varType, sz=0):
pos = self.pos
if varType == UINT32:
value = unpack('<I', self.binaryStr[pos:pos+4])[0]
self.pos += 4
return value
elif varType == UINT64:
value = unpack('<Q', self.binaryStr[pos:pos+8])[0]
self.pos += 8
return value
elif varType == VAR_INT:
[value, nBytes] = unpackVarInt(self.binaryStr[pos:])
self.pos += nBytes
....
The use case for this is something along the lines of :
def unserialize(self, toUnpack):
if isinstance(toUnpack, BinaryUnpacker):
buData = toUnpack
else: # assume string
buData = BinaryUnpacker(toUnpack)
self.var1 = buData.get(VAR_INT)
self.var2 = buData.get(BINARY_CHUNK, 64)
self.var3 = buData.get(UINT64)
self.var4obj = AnotherClass().unserialize(buData)
Thanks so much for your help.