Archive for May, 2007

Another tip for optimization if you are working with C++. Hot/Cold splitting of structures is about splitting heavily accessed ( hot ) portions of data structure from rarely accessed cold portions. An example of the concept is as follows :-

Assume you have a class C, which is 400 bytes. 300 of those 400 bytes are needed only to contruct an exception object if C::foo() throws. Is it smart to keep these 300 bytes directly as members?

An alternative would be to put those 300 bytes in an C::C_exceptiondata class, and put a C_exceptiondata * in C. The total memory usage would increase ( by sizeof(C_exceptiondata*) ), but the C_exceptiondata objects may very well end up all swapped out, and certainly would not disrupt the CPU cache. The cache would only load the remaining 100 bytes.

The 300 bytes that are rarely used are called “cold”.


The revolution

Posted: May 23, 2007 in Mobility

I couldn’t help myself in controlling my fascination towards the mobile technology and especially when I have been associated with a company which is making innovations in this arena. I have decided that I won’t limit my blog to just to tips and tricks of code optimization. Rather I would expand it to all horizons which is going to change our lives tomorrow.

 Mobile world is one such area. Today we are PC centric, I am writing this blog from my PC, but tomorrow’s world is mobile centric. Mobile is a revolution, an epidemic thats spreading faster than we would have ever imagined. Our lives will be slow with out a cellphone. So Here in this section I am going to talk about my views about “Mobility” and will try to potray the mobile based technologies that will change our future.

Danny Kalev writes :

  • Bulk I/O is a strenuous operation in terms of performance and resource allocation.
  • With the <iterator> library, the system can optimize your code as it conceptually performs an I/O operation in one shot.
  • Use of stream iterators provides more flexibility because it allows you to switch from one data source or target to another almost seamlessly (e.g. cin, cout, fstream or sstream).

He summarises as

  1. Treat data sources as input streams and targets as output streams.
  2. Create iterators that point to these streams.
  3. Use an algorithm or a container to transfer data from a source to a target

Examples of some better codes

How to write to cout using an ostream_iterator:

#include <iterator>
#include <iostream>
using namespace std;

int main()
ostream_iterator<int> os(cout); //bind iterator to cout
*os=10; //causes cout<<10
os++; //prepare for the next output operation
*os=5; //cout<<5

cin and cout with istream and ostream respectively:

#include <algorithm> //std::copy()
#include <iterator>
#include <iostream>
using namespace std;
int main()
std::copy(istream_iterator<int> (cin),
istream_iterator<int> (),
ostream_iterator<int> (cout, ” “));

Dumping a container to cout:

void func()
vector<int> vi(10,35); //fill with 10 int of the value 35
//write contents of vi to cout, each number on a new line
ostream_iterator<int> (cout, “\n”));

Writing a container to a file:

void f(const vector<int> &vi)
//open a file in write mode
ofstream vi_dump(“vi.txt”);
if (!vi_dump) //failure?
cerr<<“couldn’t open file”;
ostream_iterator<int> (vi_dump, ” “));

Writing a stream to a container:

using namespace std;
int main() {
vector<int> vi;//vector to be filled
ifstream vi_dump(“vi.txt”); //open for read
if (!vi_dump) {
cerr<<“couldn’t open file”;

copy(istream_iterator<int> (vi_dump), istream_iterator<int> (), back_inserter(vi));

Firefox going the IE way ?

Posted: May 18, 2007 in Boiling Water

Had you suddenly felt Firefox hogging up your PC memory ? Don’t worry it’s just the loads of new features thats packing into the, now very popular browser. The Wired carries a story which explains the bloating of Firefox.

“Remember when (Firefox) was the ‘light’ browser, and if you wanted to load it down that was your choice? Let’s go back to that,” summed up one reader in a Wired News poll this month aimed at identifying the most urgently needed Firefox fixes.

Rich Skrenta writes about a presentation given by Steve Souders of Yahoo :

Steve Souders of Yahoo’s “Exceptional Performance Team” gave an insanely great presentation at Web 2.0 about optimizing website performance by focusing on front end issues.

Rich has summarized the whole presentation (which he says is worth a book) into 14 rules for faster front end performance:

# Make fewer HTTP requests
# Use a CDN
# Add an Expires header
# Gzip components
# Put CSS at the top
# Move JS to the bottom
# Avoid CSS expressions
# Make JS and CSS external
# Reduce DNS lookups
# Minify JS
# Avoid redirects
# Remove duplicate scripts
# Turn off ETags
# Make AJAX cacheable and small

Download the complete presentation here