January 17, 2024

std::vector<std::string> list; // populate my list with strings, suppose this is in a loop std::string s = ...; list.push_back(s);then later on decide to pass this list to a function:
void dosomething(std::vector<std::string> list) { // does something }
...but nobody will tell them that each call of dosomething() will execute a std::string copy constructor for each item in the list, allocating (and subsequently freeing) memory for each string. There are just so many ways to get bitten here. Yes, I know, make list 'const &' etc, but it's very easy not to know that (or somewhat less easy but still possible to forget), and multiply this by 1,000 times (I know one person who was sufficiently burned by pre-increment vs post-increment having massive performance implications that they always use pre-increment in for loops, even if it's a basic type -- not that there's anything wrong with that, I guess post-increment is an odd thing to see as normal anyway)..
And we wonder why modern software generally sucks?
Recordings:
Yes, Exactly, Yes! - 1 - Sandwich Terrier (I) -- [4:47]
Yes, Exactly, Yes! - 2 - Sandwich Terrier (II) -- [3:45]
Yes, Exactly, Yes! - 3 - Sandwich Terrier (III) -- [3:38]
Yes, Exactly, Yes! - 4 - Virgins Again (Bad at Math) -- [3:27]
Yes, Exactly, Yes! - 5 - Prelude to the River -- [7:47]
Yes, Exactly, Yes! - 6 - The River -- [4:36]
Yes, Exactly, Yes! - 7 - Vertical Integration (I) -- [5:51]
Yes, Exactly, Yes! - 8 - Vertical Integration (II) -- [5:07]
Yes, Exactly, Yes! - 9 - May 29 Track 6 -- [6:34]
Yes, Exactly, Yes! - 10 - Las Vegas -- [3:57]
Yes, Exactly, Yes! - 11 - Terms of Service -- [5:32]
Yes, Exactly, Yes! - 12 - Hindenburg -- [8:19]
Yes, Exactly, Yes! - 13 - Chosen by the Few -- [5:20]
Yes, Exactly, Yes! - 14 - Self Imposed (I) -- [7:40]
Yes, Exactly, Yes! - 15 - Self Imposed (II, slower) -- [7:23]
5 Comments
extern int gv, bar(); template<class T> class saver { public: T save, *ptr; saver(T *p) : ptr(p) { save = *p; } ~saver() { *ptr = save; } }; void foo_c() { int save = gv; gv = 1; bar(); gv = save; } void foo_cxx() { saver<int> s(&gv); gv = 1; bar(); }When compiled with -O2 -fomit-frame-pointer -fno-exceptions, would foo_c() and foo_cxx() differ meaningfully? Output (x86-64 gcc 4.7.4, newer gcc versions are all similar):
foo_c(): push rbx mov ebx, DWORD PTR gv[rip] mov DWORD PTR gv[rip], 1 call bar() mov DWORD PTR gv[rip], ebx pop rbx ret foo_cxx(): push rbx mov ebx, DWORD PTR gv[rip] mov DWORD PTR gv[rip], 1 call bar() mov DWORD PTR gv[rip], ebx pop rbx ret
So yeah, the optimizer does perfectly.
Recordings:
Yes, Exactly, Yes! - 1 -- [8:53]
Yes, Exactly, Yes! - 2 - Private Life -- [7:20]
Yes, Exactly, Yes! - 3 - Watch Your Step -- [2:46]
Yes, Exactly, Yes! - 4 - Watch Your Step (II) -- [2:05]
Yes, Exactly, Yes! - 5 - Self Imposed -- [4:39]
Yes, Exactly, Yes! - 6 - Dogs Will Rule the World -- [3:04]
Yes, Exactly, Yes! - 7 - Virgins Again -- [2:53]
Yes, Exactly, Yes! - 8 - Virgins Again (Again) -- [3:14]
Yes, Exactly, Yes! - 9 - The River -- [4:31]
Yes, Exactly, Yes! - 10 - Cosmic Background -- [3:32]
Yes, Exactly, Yes! - 11 -- [5:20]
Yes, Exactly, Yes! - 12 - Hindenburg -- [6:42]
Yes, Exactly, Yes! - 13 - Chosen by the Few -- [3:48]
Yes, Exactly, Yes! - 14 - Las Vegas -- [4:04]
Yes, Exactly, Yes! - 15 - No Big Benefactor -- [4:06]
Yes, Exactly, Yes! - 16 - Now We Understand -- [4:14]
Yes, Exactly, Yes! - 17 - Cast Iron Candy Bandit -- [3:10]
Yes, Exactly, Yes! - 18 - Dogs Will Rule the World (reprise) -- [3:37]
Yes, Exactly, Yes! - 19 - Find Me -- [5:01]
Yes, Exactly, Yes! - 20 - Old as Coal -- [4:33]
Yes, Exactly, Yes! - 21 -- [11:36]
Yes, Exactly, Yes! - 22 - Charlie (feat Cory Choy) -- [4:35]
Comment...