Chrome: Tit for Tat

I haven’t seen anyone explicitly say this yet, so I will do so:

Google Chrome is not a web browser, it just happens to be shaped like one. Chrome is Google’s entry into the web application platform wars, currently being waged by Adobe (FLEX) and Microsoft (Silverlight). The horse Google is backing is the (oft maligned) one currently in the lead, DOM/Javascript, Chrome is an attempt to ensure that DOM/Javascript doesn’t lose that lead for reasons of speed or stability.

Chrome + Gears can deliver stand-alone and web-enabled apps. Next out of the gate from Google, I expect some debugging / IDE tools to support DOM/Javascript development in Chrome, ala Firebug+.

What I did on my Summer Holidays

I “just got back” (actually, last week) from a week at the Alders Beach Resort, a lovely place about 200km north up Vancouver Island. The Alders isn’t so much a “resort” in the conventional sense (well, in the conventional sense of 50 years ago), but rather a collection of mildly run-down cabins, full of families and kids, located on a tidal beach, tucked into the woods, comfortably far from pretty much anything stressful.

I had lots of time on my hands, and one can only watch children play with sand for so long, before the mood strikes, so what I did with my holiday was try to figure out how to build sand sculptures.

The Alders has been around for 50 years, and there were people at the resort last week who had been going to 15 years straight, whose kids grew up there. It is going to be a family holiday staple for us, for many years to come.

More Mapping Insights

From Andrew Turner, the Urban Spoon spatial-temporal map of restaurant seeking. Among the insights you can glean from this data presentation:

  • People look for restaurants in cities full of people, not in the wilderness.
  • People don’t look for restaurants when they are asleep.

Perhaps a zoomed in view of San Francisco would be more interesting.

The Geography of Manufacturing

It’s a shame I’m not a geographer, because there are probably some interesting sociological nuggets hiding in the way manufacturing has dispersed across the USA over the past 40 years. This weakly reasoned article in Salon includes the point that auto manufacturers are locating their US plants in the South. Why so?

Union avoidance probably has something to do with it. But the price efficiency of a dependent labour force has to balanced against the inefficiency of being separated from a concentrated nexus of talent. Jane Jacobs, in the “Economy of Cities”, talks about the self-reinforcing nature of the skills networks that develop in cities, and you don’t have to look far to see them in action. Silicon Valley, Hollywood, the Research Triangle, etc, etc.

Oddly, the USA has some world class geographic nexuses of intellectual talent, while the manufacturing base has dispersed. Intellectual work is highly portable (I’m writing in a coffee shop, linked into a client’s VPN, and compiling a library on their machine) and virtual. Manufacturing work is heavy, with physical objects to move around. Why aren’t all the auto parts manufacturers, secondary assembly plants and final auto assembly still located in Michigan?

Clearly the cost of moving things around has fallen so much in recent years that factory sites can be chosen on other bases (prevailing wages, tax holidays) instead. I would also guess that the automation of parts of the assembly line has lowered the required skill specialization of line workers, so that having access to a pool of qualified talent in a factory hub has become less important too.

I wonder if increases in the cost of transport will cause some manufacturing to geographically re-integrate, so Boeing (for example) will stop shipping wings around the world, and their parts suppliers will find it more economical to set up satellite production closer to final assembly in Seattle.

Maybe if I beg, FortiusOne will dig up some cool maps on this topic. Please? Pretty please?

Valgrinding PostGIS

UPDATE 2021/09/07: So, notwithstanding the below, running a PostgreSQL backend through valgrind will only very rarely tell you anything useful. The only reason I found it useful, back in 2008, is that memory errors I was tracking down were generated by GEOS, which allocates using the system memory allocator.

PostGIS/PostgreSQL on the other hand, allocates using a “heirarchical memory manager” inside PgSQL, which is why the code is full of allocations using palloc() and not malloc(). The PgSQL memory manager allocates big “memory context” blocks, and then when cleaning up after a query or other big piece of work, drops the whole context at once. This results in all sorts of good things for PgSQL: memory locality, reduction in system calls to things like free(), faster allocations of small objects.

However, since only the allocation of the context and the freeing of the context use system memory calls, valgrind has no visibility into the small allocations your application code might be making. It only sees these big allocations / frees, and will never see the specific allocations / frees in your application code.

Clever developers might be thinking: “I know, I’ll just over-ride palloc() and pfree() in the PgSQL code, so that all allocations go via the system, and then valgrind will have to pick them up”. Except, because of the memory context system, lots of code doesn’t bother to pfree() all allocations, since the system will just free the whole context at the end.

As a result, if you try to push all allocations to malloc() and free() and then run valgrind, valgrind will say that PgSQL leaks like a sieve, and any mistakes you might want to find in your application code will be drowned out in the avalanche of un-freed memory valgrind is now finding.


So, you want to be a super-hero? How about tracking down memory leaks and otherwise using valgrind to make PostGIS a cleaner and better system? However, getting PostGIS into valgrind can be a bit tricky.

First of all, what is valgrind? It’s a tool for finding memory leaks and other memory issues in C/C++ code. It only runs under Linux, so you do need to have sufficiently portable code to run it there. Many memory checking tools rely on “static code analysis”, basically looks at what your code says it does and seeing if you have made any mistakes.

These kinds of tools have to be very clever, since they not only need to understand the language, they have to understand the structure of your code. Valgrind takes the opposite tack – rather than inspect your code for what it says it does, it runs your code inside an emulator, and sees what it actually does. Running inside valgrind, every memory allocation and deallocation can be tracked and associated with a particular code block, making valgrind a very effective memory debugger.

In order to get the most useful reports, you have to compile your code with minimal optimization flags, and with debugging turned on. To grind both GEOS and PostGIS simultaneously, compile GEOS and PostGIS with the correct flags:

# Make GEOS:
CXXFLAGS="-g -O0" ./configure
make clean
make
make install

# Make PostGIS:
CFLAGS="-g -O0" ./configure --with-pgconfig=/usr/local/pgsql/bin/pg_config
make clean
make
make install

Once you have your debug-enabled code in place, you are ready to run valgrind. Here things get interesting! Usually, PostgreSQL is run in multi-user mode, with each back-end process spawned automatically as connections come in. But, in order to use valgrind, we have to run our process inside the valgrind emulator. How to do this?

Fortunately, PostgreSQL supports a “single user mode”. Shut down your database instance (pg_ctl -D /path/to/database stop) first. Then invoke a postgres backend in single-user mode:

echo "select * from a, b where st_intersects(a.the_geom, b.the_geom)" | \
  valgrind \
    --leak-check=yes \
    --log-file=valgrindlog \
    /usr/local/pgsql/bin/postgres --single -D /usr/local/pgdata postgis

So, here we are echoing the SQL statement we want tested, and piping it into a valgrind-wrapped instance of single-user PostgreSQL. Everything will run much slower than usual, and valgrind will output a report to the valgrindlog file detailing where memory blocks are orphaned by the code.