-
Notifications
You must be signed in to change notification settings - Fork 874
Description
Hi,
I was profiling my GIS application and trying to find potential places in the current implementation where some optimizations could be made. Mostly I tested the performance of fetching the data in bytea format. As a result, I found out that memory allocation is not efficient in some cases.
More precisely, the memory is allocated every time we read data for each column in the NpgsqlAsciiRow class.
byte[] buffer = new byte[fieldSize];
PGUtil.CheckedStreamRead(Stream, buffer, 0, fieldSize);
In the case of bytea conversion, the byte array buffer is then used to create another one in the function ByteaTextToByteArray.
// PostgreSQL 8.5+'s bytea_output=hex format
byte[] result = new byte[(byteALength - 2) / 2];
Therefore, in theory we could reuse buffer several times, namely we could somehow store the byte[] buffer in memory and resize it if the size of a new field is greater the previous one. However, this new approach requires additional variable that defines actual length of data in the buffer.
Second, we could also think about reusing the byte[] which is returned by ByteaTextToByteArray. For instance, in a GIS application one only needs to read data from an array of bytes then convert this byte array to a Geometry object. Next, the array of bytes is no longer needed and can be returned to Npgsql to be reused.
I believe that the raised points should be discussed further as the memory allocation directly influences the performance of any program which uses Npgsql.