Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9253929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:20:27+00:00 2026-06-18T11:20:27+00:00

Currently with the library I have written, my small objects (which are not polymorphic)

  • 0

Currently with the library I have written, my small objects (which are not polymorphic) are allocated, within an object-pool, in a vector with unique_ptr‘s. Now obviously I want to change this because there’s obviously a lot of over-head in calling new so many times. I’m curious if it is more efficient to: cache the objects in the pool (store it in a vector, i.e. vector<Object>), or create the object when it is needed, via it’s ID. And note that there is a lot of objects that are created.

What I mean is, should I be doing this:

Create the object when needed? (Note these objects are small, 64-128 bits, as all is contained is an ID and a reference/pointer to a parent object)

Object ObjectFactory::create()
{
    return Object(nextId(), getParent());
}

Object ObjectFactory::get(unsigned id)
{
    return Object(id, getParent());
}

or:

Object& ObjectFactory::create()
{
     // get the next id of the object
     unsigned id = nextId();

     // resize (if necessary)
     if(_objects.size() <= id)
     {
        _objects.resize(id + 1);
        _objects[id]._parent = getParent();
     }

     return _objects[id];
}

Object& ObjectFactory::get(unsigned id)
{ return _objects[id]; }

What I’m specifically concerned about is: would the re-creation of the Object‘s cause much over-head?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:20:28+00:00Added an answer on June 18, 2026 at 11:20 am

    @LokiAstari is right, you clearly have a problem with your pointers on resize.

    There’s something I don’t understand; you say you’re using an object pool but that you have problems with too many new statements. If you’re using an object pool, I would say it’s precisely to avoid new statements, no?

    Here are my suggestions, although I’m no expert and there might be better solutions involving the implementation of your own allocator typically (dark side of the force..). You could use a container like std::deque, which ensures the validity of pointers/references on resize.

    You would begin with an initial resize of a lot of Objects (your pool), and you can either handle manually the subsequent resize when needed (extending the capacity with chunks of predefined size), or accept the new statements then if you know there shouldn’t be a lot, and use the emplace_back method.

    I also don’t know if you’re doing a lot of insertion/deletion of objects with your IDs. If so, you might consider using std::unordered_map.

    Here is an example using std::deque:

    #include <iostream>
    #include <deque>
    
    #define POOL_RESERVE 1000
    
    
    // Data storage for your object
    struct MyObjectData
    {
        double some_data;
    };
    
    
    // Container returned by the factory
    struct MyObject 
    {
        typedef MyObjectData data_type;
    
        unsigned   id; 
        data_type* data;
    
        MyObject(): id(0), data(0) {}
        MyObject( const unsigned& id_, data_type* data_ ): id(id_), data(data_) {}
    
        void set( const unsigned& id_, data_type* data_ )
            { id = id_; data = data_; }
    };
    
    
    // MyObject Pool
    class MyObjectPool
    {
    public:
    
        typedef MyObjectData data_type;
    
        MyObjectPool(): count(0) { pool.resize(POOL_RESERVE); }
    
        void get( const unsigned& id, MyObject& obj )
            {
                // Check requested index
                if ( id >= count )
                    obj.set( 0, 0 );
                else
                    obj.set( id, &pool[id] );
            }
    
        void create( MyObject& obj )
            {
                // Create new data container if needed
                if ( count++ >= pool.size() ) pool.emplace_back();
    
                // Return next available object
                obj.set( count-1, &pool[count-1] );
            }
    
    private:
    
        unsigned count;
        std::deque<data_type> pool;
    };
    
    
    // MyObject factory
    class MyObjectFactory
    {
        typedef MyObjectFactory self;
        static MyObject local;
    
        static MyObjectPool& get_instance()
            {
                static MyObjectPool pool; 
                return pool;
            }
    
    public:
    
        static MyObject get( const unsigned& id )
            {
                self::get_instance().get(id,local);
                return local;
            }
    
        static MyObject create()
            {
                self::get_instance().create(local);
                return local;
            }
    };
    
    // Define static variable
    MyObject MyObjectFactory::local = MyObject();
    
    
    // Usage example
    int main()
    {
        MyObject a,b,c;
    
        a = MyObjectFactory::create();
        b = MyObjectFactory::create();
        c = MyObjectFactory::get(1);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a WCF Service Library which will be started through a Console
I currently have a .NET class library written in C# that exposes its functionaility
I have a PHP module written in C++, which relies on a C++ library
I currently have a library written in C++, building with the GNU autotools, and
I have a DirectShow filter written Delphi 6 using the DSPACK component library. Currently
i have written a small codeigniter test app that is currently running on my
I have written a Python extension library in C and I am currently using
I am currently using Doctrine's DBAL library for MySQL interfacing and have currently built
I currently have a project, written in C++, it uses multiple libraries, including an
I've written a abstract base class TCPIP sever in its own namespace/library. Currently I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.