Hello,
I have +1024 goroutines routines running, which occasionally have to compress and decompress their private data. (usually this happens around the same time)
To avoid having a large memory footprint I’ve implemented our encode function as follows
Using a mutex, I’m able to reuse the zlibWriter and the encodeBuffer, this reduced the used memory footprint drastically.
//global vars
encodeBuffer = bytes.NewBuffer(nil)|
zlibWriter, _ = zlib.NewWriterLevelDict(encodeBuffer, zlib.BestSpeed, dict)|
func EncodeData(input byte) byte {
encodeMutex.Lock()
encodeBuffer.Reset()
zlibWriter.Reset(encodeBuffer)
zlibWriter.Write(input)
zlibWriter.Flush()
zlibWriter.Close()
ret := bytes.Clone(encodeBuffer.Bytes())
encodeMutex.Unlock()
return ret
}
I’m trying to do the same for my decode function
//global vars
decodeBuffer = bytes.NewBuffer(nil)
func DecodeData(buffer byte) byte {
decodeMutex.Lock()
decodeBuffer.Reset()
zlibReader, _ := zlib.NewReaderDict(bytes.NewBuffer(buffer), dict)
decodeBuffer.ReadFrom(zlibReader)
zlibReader.Close()
ret := bytes.Clone(decodeBuffer.Bytes())
decodeMutex.Unlock()
return ret
}
Unfortunately, I do not see how I can reuse the zlibReader. It’s lacking a Reset() function to start.
Is this even possible?