Some perfomance thoughts required

Discussion in 'C++' started by Alexander Adam, Jun 20, 2008.

  1. Hi!

    I've got a pretty complex data structure that keeps instances of
    structs allocated with the new operator.
    Now my issue is that those structures are required to be allocated /
    deallocated pretty often resulting
    in a huge perfomance issue. Usually what I am doing is to create
    arrays and initialize their sizes a few
    times bigger than originally required to avoid another allocation on
    the next run. Then I do simply reset them
    and do a memcpy at the existing location. This works great with POD
    structus but my structs are complex
    in the way that they not only use inheritance but they do also use
    internal objects that do require an
    constructor / destructor. So what is the general way to handle such
    things? Am I missing something?

    thanks!
    Alex
    Alexander Adam, Jun 20, 2008
    #1
    1. Advertising

  2. On 2008-06-20 11:05, Alexander Adam wrote:
    > Hi!
    >
    > I've got a pretty complex data structure that keeps instances of
    > structs allocated with the new operator.
    > Now my issue is that those structures are required to be allocated /
    > deallocated pretty often resulting
    > in a huge perfomance issue. Usually what I am doing is to create
    > arrays and initialize their sizes a few
    > times bigger than originally required to avoid another allocation on
    > the next run. Then I do simply reset them
    > and do a memcpy at the existing location. This works great with POD
    > structus but my structs are complex
    > in the way that they not only use inheritance but they do also use
    > internal objects that do require an
    > constructor / destructor. So what is the general way to handle such
    > things? Am I missing something?


    A pool allocator might help.

    --
    Erik Wikström
    Erik Wikström, Jun 20, 2008
    #2
    1. Advertising

  3. Alexander Adam

    Guest

    On Jun 20, 10:28 am, Erik Wikström <> wrote:
    > On 2008-06-20 11:05, Alexander Adam wrote:
    >
    >
    >
    > > Hi!

    >
    > > I've got a pretty complex data structure that keeps instances of
    > > structs allocated with the new operator.
    > > Now my issue is that those structures are required to be allocated /
    > > deallocated pretty often resulting
    > > in a huge perfomance issue. Usually what I am doing is to create
    > > arrays and initialize their sizes a few
    > > times bigger than originally required to avoid another allocation on
    > > the next run. Then I do simply reset them
    > > and do a memcpy at the existing location. This works great with POD
    > > structus but my structs are complex
    > > in the way that they not only use inheritance but they do also use
    > > internal objects that do require an
    > > constructor / destructor. So what is the general way to handle such
    > > things? Am I missing something?

    >
    > A pool allocator might help.
    >
    > --
    > Erik Wikström


    Check out boost::eek:bject_pool<T>.

    T
    , Jun 20, 2008
    #3
  4. Alexander Adam

    Jerry Coffin Guest

    In article <3d400f58-1b60-40dc-81bc-01e5e692fdcb@
    59g2000hsb.googlegroups.com>, says...
    > Hi!
    >
    > I've got a pretty complex data structure that keeps instances of
    > structs allocated with the new operator.
    > Now my issue is that those structures are required to be allocated /
    > deallocated pretty often resulting
    > in a huge perfomance issue. Usually what I am doing is to create
    > arrays and initialize their sizes a few
    > times bigger than originally required to avoid another allocation on
    > the next run. Then I do simply reset them
    > and do a memcpy at the existing location. This works great with POD
    > structus but my structs are complex
    > in the way that they not only use inheritance but they do also use
    > internal objects that do require an
    > constructor / destructor. So what is the general way to handle such
    > things? Am I missing something?


    You probably want to overload operator new for the class(es) of the
    objects you're allocating/freeing so frequently. At startup, your
    operator new will grab a big chunk of memory for the data, and break it
    up into chunks, each the size of a single object. As objects are
    allocated, it'll give out the addresses of object-sized chunks, and mark
    each chunk as being in use when it does so. When an object is freed,
    it'll mark each chunk as being available again.

    You can probably find existing source code for a few different versions
    of this -- Andrei Alexandrescu's Loki library has one, and Boost has
    another, and so on.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
    Jerry Coffin, Jun 20, 2008
    #4
  5. Alexander Adam

    Fei Liu Guest

    Alexander Adam wrote:
    > Hi!
    >
    > I've got a pretty complex data structure that keeps instances of
    > structs allocated with the new operator.
    > Now my issue is that those structures are required to be allocated /
    > deallocated pretty often resulting
    > in a huge perfomance issue. Usually what I am doing is to create
    > arrays and initialize their sizes a few
    > times bigger than originally required to avoid another allocation on
    > the next run. Then I do simply reset them
    > and do a memcpy at the existing location. This works great with POD
    > structus but my structs are complex
    > in the way that they not only use inheritance but they do also use
    > internal objects that do require an
    > constructor / destructor. So what is the general way to handle such
    > things? Am I missing something?
    >
    > thanks!
    > Alex

    Apart from the other suggestions, you should think about the use of
    array as your container data structure, perhaps a node based container
    will provide better overall performance, especially when it's memory
    intensive.

    Fei
    Fei Liu, Jun 20, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gnanaprakash Rathinam

    CreateInstranceAndUnwrap slow perfomance

    Gnanaprakash Rathinam, Dec 29, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    2,098
    David Levine
    Dec 30, 2004
  2. Fredrik Melin

    Server perfomance

    Fredrik Melin, Oct 27, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    402
    Paul Glavich [MVP - ASP.NET]
    Oct 27, 2004
  3. Scott Reynolds

    data perfomance?

    Scott Reynolds, Mar 1, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    408
    Karl Seguin
    Mar 1, 2005
  4. George

    Huge HTML output perfomance

    George, Mar 28, 2005, in forum: ASP .Net
    Replies:
    10
    Views:
    658
    Robbe Morris [C# MVP]
    Mar 29, 2005
  5. James T.

    Perfomance test

    James T., Feb 26, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    395
    James T.
    Feb 26, 2006
Loading...

Share This Page