OT: optimization question

W

woodbrian77

Recently I noticed the last line in an if matched the last
line in an else here:

void
cmwAmbassador::mediateResponse (transactions_t::iterator itr)
{
bool safe_sending = true;
try {
localsendbuf.sock_ = itr->sock;
bool reqResult;
remoteMessages.Receive(cmwBuf, reqResult);


if (reqResult) {
changeDirectory(itr->path.c_str());
::std::vector<File> outputFiles;
remoteMessages.Receive(cmwBuf, outputFiles);

save_lastruntime(itr->filename, itr->time_in_seconds);
safe_sending = false;
localMessages.Marshal(localsendbuf, true);
localsendbuf.Flush();
} else {
::std::string errorMsg;
remoteMessages.Receive(cmwBuf, errorMsg);
safe_sending = false;
localMessages.Marshal(localsendbuf, false, errorMsg);
localsendbuf.Flush();
}
} catch:):std::exception const& ex) {
#ifdef SYSLOG_AVAILABLE
syslog(LOG_ERR, "mediateResponse: %s", ex.what());
#endif
if (safe_sending) {
localMessages.Marshal(localsendbuf, false, ex.what());
localsendbuf.Flush();
}
}
}

.... and I factored it out. I felt certain the new version
would be the same size as it is such a simple thing. I was
surprised when the size of the executable decreased by 88
bytes using gcc 4.7.0 20120314. Using gcc 4.8.0 20120429
the size decreased by 56 bytes. I'm using -O3 in both
cases. This isn't some tricky thing and these are some
recent compilers. Shouldn't compilers be able to handle
this?

Shalom,
Brian
Ebenezer Enterprises
http://webEbenezer.net
 
M

Marc

Recently I noticed the last line in an if matched the last
line in an else here: [...]
... and I factored it out. I felt certain the new version
would be the same size as it is such a simple thing. I was
surprised when the size of the executable decreased by 88
bytes using gcc 4.7.0 20120314. Using gcc 4.8.0 20120429
the size decreased by 56 bytes. I'm using -O3 in both
cases. This isn't some tricky thing and these are some
recent compilers. Shouldn't compilers be able to handle
this?

They do, at least in part.
Note that your idea of having the same last instruction and the
compilers' idea are not the same (destructors?). Detecting that they
can permute things until there is some common code that can be taken
out of the loop is already much harder.

If you want to understand what compilers do, you'll have to start
looking a bit deeper than the size of the object file (-fdump-tree-all
to start with?).
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top