You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
We definitely need to look closely at memory allocation, I've also identified some areas where we're needlessly allocating buffers. We should try to look into this for version 2.3.
I agree we need a better memory allocation inside Npgsql. A lot of reading operations could be done reusing pre allocated byte arrays instead of creating new ones each time.
We would just need to check how we could do that.
There's still the question of how to deal with variable-length data, or in other words, what the size of the connector buffer should be - we can definitely look at this for 2.3...
we actually do heap allocations for every little 4-byte network read...!
There's still the question of how to deal with variable-length data, or in
other words, what the size of the connector buffer should be - we can
definitely look at this for 2.3...
—
Reply to this email directly or view it on GitHub #308 (comment).
Without having thought about it very much, it seems that thread-safety is less of an issue here, since the ADO.NET contract in general doesn't allow using a connection concurrently from multiple threads. In other words, we're not supposed to provide thread-safety for most things.
Of course, we do have some multi-threading situations which have to be managed correctly - async notifications - but I'm not sure there's much beyond that. At least in the case of async notifications a coarse lock on the connector seems appropriate, and would obviate the need to deal with thread safety on connector resources such as buffers. Do we have other cases where we need to guarantee thread-safety etc.?
If the situation is as simple as I hope it is (almost no thread-safety is needed), then we probably don't even need buffer managers or allocators, it's sufficient to simply allocate buffers on the connector itself and use them...
The buffer manager ment before lets you skip allocations even on connector
and reuses warm, possibly in CPU cache memory regions as soon as they're
makred as unused.
Additionaly, it makes one LOH allocation and manages the segments on it's
own.
Not arguing, just pointing advantages :-)
28 sie 2014 08:02 "Shay Rojansky" [email protected] napisał(a):
Without having thought it very much, it seems that thread-safety is less
of an issue here, since the ADO.NET contract in general doesn't allow
using a connection concurrently from multiple threads. In other words,
we're not supposed to provide thread-safety for most things.
Of course, we do have some multi-threading situations which have to be
managed correctly - async notifications - but I'm not sure there's much
beyond that. At least in the case of async notifications a coarse lock on
the connector seems appropriate, and would obviate the need to deal with
thread safety on connector resources such as buffers. Do we have other
cases where we need to guarantee thread-safety etc.?
If the situation is as simple as I hope it is (almost no thread-safety is
needed), then we probably don't even need buffer managers or allocators,
it's sufficient to simply allocate buffers on the connector itself and use
them...
—
Reply to this email directly or view it on GitHub #308 (comment).
Activity
roji commentedon Aug 3, 2014
See #306 (comment)
We definitely need to look closely at memory allocation, I've also identified some areas where we're needlessly allocating buffers. We should try to look into this for version 2.3.
franciscojunior commentedon Aug 3, 2014
I agree we need a better memory allocation inside Npgsql. A lot of reading operations could be done reusing pre allocated byte arrays instead of creating new ones each time.
We would just need to check how we could do that.
Maybe a custom memory allocator?
roji commentedon Aug 4, 2014
I think the answer is actually pretty simple, at least for a big number of cases - the connector can just have a permanent buffer used for read and write operations, rather than allocating buffers all the time. A great example is here: https://github.com/npgsql/Npgsql/blob/master/Npgsql/Npgsql/PGUtil.cs#L336 - we actually do heap allocations for every little 4-byte network read...!
There's still the question of how to deal with variable-length data, or in other words, what the size of the connector buffer should be - we can definitely look at this for 2.3...
jbcooley commentedon Aug 4, 2014
I read something interesting on this topic recently.
http://codeblog.jonskeet.uk/2014/08/01/object-pooling-and-thread-safety/
I think the thread-safety part is relevant here since that method is static.
On Mon, Aug 4, 2014 at 9:26 AM, Shay Rojansky [email protected]
wrote:
franciscojunior commentedon Aug 27, 2014
Hi all!
In #328, @Scooletz talks about this project https://github.com/EventStore/buffer-manager which is a buffer manager. I think we could have a look at it.
roji commentedon Aug 28, 2014
Without having thought about it very much, it seems that thread-safety is less of an issue here, since the ADO.NET contract in general doesn't allow using a connection concurrently from multiple threads. In other words, we're not supposed to provide thread-safety for most things.
Of course, we do have some multi-threading situations which have to be managed correctly - async notifications - but I'm not sure there's much beyond that. At least in the case of async notifications a coarse lock on the connector seems appropriate, and would obviate the need to deal with thread safety on connector resources such as buffers. Do we have other cases where we need to guarantee thread-safety etc.?
If the situation is as simple as I hope it is (almost no thread-safety is needed), then we probably don't even need buffer managers or allocators, it's sufficient to simply allocate buffers on the connector itself and use them...
Scooletz commentedon Aug 28, 2014
The buffer manager ment before lets you skip allocations even on connector
and reuses warm, possibly in CPU cache memory regions as soon as they're
makred as unused.
Additionaly, it makes one LOH allocation and manages the segments on it's
own.
Not arguing, just pointing advantages :-)
28 sie 2014 08:02 "Shay Rojansky" [email protected] napisał(a):
4 remaining items
roji commentedon Mar 23, 2015
Memory allocation and type handling has been totally redone for v3.0, and is now very close to zero-copy efficiency.