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 169133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:34:34+00:00 2026-05-11T12:34:34+00:00

I’ve been comparing a STL implementation of a popular XmlRpc library with an implementation

  • 0

I’ve been comparing a STL implementation of a popular XmlRpc library with an implementation that mostly avoids STL. The STL implementation is much slower – I got 47s down to 4.5s. I’ve diagnosed some of the reasons: it’s partly due to std::string being mis-used (e.g. the author should have used ‘const std::string&’ wherever possible – don’t just use std::string’s as if they were Java strings), but it’s also because copy constructors were being constantly called each time the vector outgrew its bounds, which was exceedingly often. The copy constructors were very slow because they did deep-copies of trees (of XmlRpc values).

I was told by someone else on StackOverflow that std::vector implementations typically double the size of the buffer each time they outgrow. This does not seem to be the case on VisualStudio 2008: to add 50 items to a std::vector took 177 calls of the copy constructor. Doubling each time should call the copy constructor 64 times. If you were very concerned about keeping memory usage low, then increasing by 50% each time should call the copy constructor 121 times. So where does the 177 come from?

My question is: (a) why is the copy constructor called so often? (b) is there any way to avoid using the copy constructor if you’re just moving an object from one location to another? (In this case and indeed most cases a memcpy() would have sufficed – and this makes a BIG difference).

(NB: I know about vector::reserve(), I’m just a bit disappointed that application programmers would need to implement the doubling trick when something like this is already part of any good STL implementation.)

My test program:

#include <string> #include <iostream> #include <vector>    using namespace std;   int constructorCalls; int assignmentCalls; int copyCalls;   class C {     int n;  public:     C(int _n) { n = _n; constructorCalls++; }     C(const C& orig) { copyCalls++; n = orig.n; }     void operator=(const C &orig) { assignmentCalls++; n = orig.n; } };    int main(int argc, char* argv[]) {     std::vector<C> A;      //A.reserve(50);     for (int i=0; i < 50; i++)         A.push_back(i);     cout << 'constructor calls = ' << constructorCalls << '\n';     cout << 'assignment calls = ' << assignmentCalls << '\n';     cout << 'copy calls = ' << copyCalls << '\n';     return 0; } 
  • 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. 2026-05-11T12:34:35+00:00Added an answer on May 11, 2026 at 12:34 pm

    The STL does tend to cause this sort of thing. The spec doesn’t allow memcpy’ing because that doesn’t work in all cases. There’s a document describing EASTL, a bunch of alterations made by EA to make it more suitable for their purposes, which does have a method of declaring that a type is safe to memcpy. Unfortunately it’s not open source AFAIK so we can’t play with it.

    IIRC Dinkumware STL (the one in VS) grows vectors by 50% each time.

    However, doing a series of push_back’s on a vector is a common inefficiency. You can either use reserve to alleviate it (at the cost of possibly wasting memory if you overestimate significantly) or use a different container – deque performs better for a series of insertions like that but is a little slower in random access, which may/may not be a good tradeoff for you.

    Or you could look at storing pointers instead of values which will make the resizing much cheaper if you’re storing large elements. If you’re storing large objects this will always win because you don’t have to copy them ever – you’ll always save that one copy for each item on insertion at least.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 120k
  • Answers 120k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This sounds like an incredibly bad idea. Environment.Exit(0), will obviously… May 12, 2026 at 12:02 am
  • Editorial Team
    Editorial Team added an answer You can get an array of Screens that you have… May 12, 2026 at 12:02 am
  • Editorial Team
    Editorial Team added an answer Turns out Visual Studio requires the Web.config to part of… May 12, 2026 at 12:02 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.