Skip to content

Optimization regarding memory allocation #308

Closed

Description

@Rungee

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.

Activity

  1. roji commented on Aug 3, 2014

    @roji
    Member

    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.

  2. added this to the milestone on Aug 3, 2014
  3. franciscojunior commented on Aug 3, 2014

    @franciscojunior
    Member

    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?

  4. roji commented on Aug 4, 2014

    @roji
    Member

    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...

  5. jbcooley commented on Aug 4, 2014

    @jbcooley
    Contributor

    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:

    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...

    —
    Reply to this email directly or view it on GitHub
    #308 (comment).

  6. franciscojunior commented on Aug 27, 2014

    @franciscojunior
    Member

    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.

  7. roji commented on Aug 28, 2014

    @roji
    Member

    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...

  8. Scooletz commented on Aug 28, 2014

    @Scooletz
    Contributor

    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).

  9. 4 remaining items

  10. roji commented on Mar 23, 2015

    @roji
    Member

    Memory allocation and type handling has been totally redone for v3.0, and is now very close to zero-copy efficiency.

  11. modified the milestones: , 3.0-beta1 on Mar 30, 2015
  12. modified the milestones: 3.0-beta1, on Aug 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions