I recently had the experience of going through US immigration at DUB (Dublin Airport in Ireland) prior to boarding a flight to PHL. It came as a surprise to me that I should be interviewed by US immigration officials at Dublin airport.
In most respects, Dublin airport is a total mess, but the immigration-before-boarding arrangement was a pleasure.
After arriving at PHL, the entire plane was "fast-tracked" into the country (and then I stood in line for 45 minutes to pick up my suitcase and recheck it for my onward flight...

)
I started wondering if there could be some way to extend this arrangement so that every flight gets fast-tracked. Of course, it's not feasible to send one country's immigration personnel to every airport in the world... But perhaps immigration queues could be eliminated with some new technology... I'm thinking maybe passport scanners at departure airports or onboard planes with broadband connections and video conferencing to allow 99.9% of passengers to complete immigration processes before boarding or during the flight.
I heard that Australia had once piloted a scheme to allow passengers to clear immigration during their long flights to that country... Did that ever happen? Did it turn out to be too costly? Maybe it would be more feasible today?